2014-01-18 2 views
0

Я пытаюсь использовать мой ардуино и потенциометр, чтобы двигатель вращается в одну сторону, когда потенциометр прошел 0 и вращает другой, когда потенциометр проходит 0 в другом направлении. Код работает на стороне SensorValue < 512, но не на стороне> 507.Управление двигателем с потенциометром

const int analogInPin = A1; // Analog input pin that the potentiometer is attached to 
const int analogOutPin = 9; // 
const int analogOutPin_2 = 11; // 
int sensorValue = 0;  // value read from the pot 
int outputValue = 0;  // value output to the PWM (analog out) 


void setup() { 
    // initialize serial communications at 9600 bps: 
    Serial.begin(9600); 

} 

void loop() { 

    sensorValue = analogRead(analogInPin);    

    if (sensorValue < 507) {  

    analogWrite(analogOutPin, LOW); 
    outputValue = map(sensorValue, 0, 512, 0, 255); 

    analogWrite(analogOutPin_2, outputValue);  
    } 
    if (512 > sensorValue) {  

    analogWrite(analogOutPin_2, LOW); 
    outputValue = map(sensorValue, 512, 1023, 0, 255); 

analogWrite(analogOutPin, outputValue);  
    } 
    else { 


} 

    delay(2);      

} 
+0

Кроме выяснить, что правильные значения карты, я думаю, что это правильно, может быть, мой чип сломан – Rufus

ответ

0

Для решения таких вопросов. Я рекомендую несколько стратегических принты и для цикла, чтобы симулировать диапазон данных, показывает следующий код с несколькими настройками работает лучше

const int analogInPin = A1; // Analog input pin that the potentiometer is attached to 
const int analogOutPin = 9; // 
const int analogOutPin_2 = 11; // 
int sensorValue = 0;  // value read from the pot 
int outputValue = 0;  // value output to the PWM (analog out) 

void setup() { 
    // initialize serial communications at 9600 bps: 
    Serial.begin(9600); 
    delay(1000); 
    Serial.println("Begin1"); 
} 

void loop() { 
// sensorValue = analogRead(analogInPin);    
    for (sensorValue = 0; sensorValue < 1024; sensorValue++) { 
    Serial.print("sensorValue = "); 
    Serial.print(sensorValue); 

    if (sensorValue < 507) {  
     outputValue = map(sensorValue, 0, 512, 255, 0); 
     Serial.print(" Forward : "); 
     Serial.print(outputValue); 
     analogWrite(analogOutPin, LOW); 
     analogWrite(analogOutPin_2, outputValue);  
    } else if (512 < sensorValue) {  
     outputValue = map(sensorValue, 512, 1023, 0, 255); 
     Serial.print(" Reverse : "); 
     Serial.print(outputValue); 
     analogWrite(analogOutPin_2, LOW); 
     analogWrite(analogOutPin, outputValue);  
    } 
    Serial.println(); 

    } 
    while(1); 
    delay(1000);      
} 
Смежные вопросы