So, my code is supposed to do some calculations and it saves the value into "turn" and if "turn" is a certain value I want the robot to drive in a certain direction. But the code is not working, not sure why, this is what happens: When the value is 128, it drives forward but when the value changes it keeps going forward when in reality its supposed to stop going forward and go right or left. But, if I reset it and the value is for example 135 from the beginning and it turns right the appropriate direction but it stays turning there even if the value changes to 120 and in reality 120 is left, it keeps going right till I reset it. Whats am i doing wrong?
void loop(){
I do some calculations and it saves into value "turn".
if ( turn == 128 ){
drive_forward();
analogWrite(PWMR, 200);// speed control
analogWrite(PWML, 200);// speed control
}
if ( 132 > turn > 129 ){
drive_Left();
analogWrite(PWMR, 100);// speed control
}
if ( 175 > turn > 132 ){
drive_Left();
analogWrite(PWMR, 150);// speed control
}
if ( turn > 175 ){
drive_Left();
analogWrite(PWMR, 200);
}
if ( 127 > turn > 120 ){
drive_right();
analogWrite(PWML, 100);// speed control
}
if ( 120 > turn > 100 ){
drive_right();
analogWrite(PWML, 150);// speed control
}
if ( 100 > turn ){
drive_right();
analogWrite(PWML, 200);// speed control
}
}
void drive_forward()
{
digitalWrite(RFWD,HIGH);// right wheel foward h-bridge
digitalWrite(RRVS,LOW);
digitalWrite(LFWD,HIGH);//left wheel foward h-bridge
digitalWrite(LRVS,LOW);
return;
}
void drive_Left()
{
digitalWrite(RFWD,HIGH);// right wheel foward h-bridge
digitalWrite(RRVS,LOW);
digitalWrite(LFWD,LOW);//left wheel foward h-bridge
digitalWrite(LRVS,LOW);
return;
}
void drive_right()
{
digitalWrite(RFWD,LOW);// right wheel foward h-bridge
digitalWrite(RRVS,LOW);
digitalWrite(LFWD,HIGH);//left wheel foward h-bridge
digitalWrite(LRVS,LOW);
return;
}
void drive_stop()
{
digitalWrite(RFWD,LOW);// right wheel foward h-bridge
digitalWrite(RRVS,LOW);
digitalWrite(LFWD,LOW);//left wheel foward h-bridge
digitalWrite(LRVS,LOW);
return;
}