Go Down

Topic: Driving a robot... help please. (Read 1 time)previous topic - next topic

husein06

Feb 02, 2013, 12:44 amLast Edit: Feb 02, 2013, 02:20 am by husein06 Reason: 1
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?

Code: [Select]
` 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;     }`

PeterH

#1
Feb 02, 2013, 01:56 am
I would suspect that your code is not being re-run after the value of turn changes. But of course that's only a guess, because you haven't posted your whole code.

JimboZA

#2
Feb 02, 2013, 05:58 am
I'd agree that you need to post all the code... where is "turn" set?

You could use Serial to debug, by putting a Serial.println(turn) just above the first if and see exactly what the current value is...
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)