Pages: [1]   Go Down
Author Topic: Driving a robot... help please.  (Read 267 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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;
     }

« Last Edit: February 01, 2013, 08:20:45 pm by husein06 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: