i dont get it

Don’t put semicolons on #defines unless you really know what you’re doing

Don't put semicolons on #defines unless you really know what you're doing

or equal signs?

Use const int or whatever instead of #define.

Korman

i changed the code to…

///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4     
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
int state = 1;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;
 
 
/////setting pinmode//////


void setup() 
{
  myservo.attach(9);
  Serial.begin(9600);                             
  pinMode(motor_ONE_pin_ONE,OUTPUT);     
  pinMode(motor_ONE_pin_TWO,OUTPUT);    
  pinMode(motor_TWO_pin_ONE,OUTPUT);     
  pinMode(motor_TWO_pin_TWO,OUTPUT);     
}


//////looping funtoinn//////


void loop()
{
  switch(state)
  {
    case 1:
    ping_read();
    drive_forward();
    if(cm < 10 )
    {
      state = 2;
    }
    break;
    case 2:
    ping_read_right();
    ping_read_left();
    state =3;
    break;
    case 3:
    if(distance_right > distance_left )
    {
      state =4;
    }
    else if (distance_right < distance_left )
    {
      state =5;
    }
    else if ((distance_right && distance_left ) < 10 )
    {
      state =6;
    }
    
    else
    {
      state = 1 ;
    }
    case 4:
    drive_right();
    break;
    case 5:
    drive_left();
    break;
    case 6:
    drive_360();
    break;
    default:  ;
  }
}
    



//////ping functions///////

void ping_read()
{
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);        
  Serial.print("cm");      
  Serial.println();
}
void ping_read_left()
{
  myservo.write(10);
  delay(150);
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_left = microsecondsToCentimeters(duration);
  Serial.print(distance_left);       
  Serial.print("cm");     
  Serial.println();
}
void ping_read_right()
{
  myservo.write(180);
  delay(150);
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_right = microsecondsToCentimeters(duration);
  Serial.print(distance_right);       
  Serial.print("cm");     
  Serial.println();
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}


void drive_stop() 
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_right()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_left()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_360()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

now i ahve noo errors BUT
even noiw the drive forward condition is not occuring when an obstacle is detedted the servo is set to 0 degrees and then the left motor is rotaing continiously :frowning:

the format for # define is "#define symbol value" with no punctuation.

&&

Don't put semicolons on #defines unless you really know what you're doing

well i am really sorry ii didnt notice any equal to signs or colons any where i am really sorry for wasting your time :-[ well now i have seen what i posted and i could see that it was clearly indicated as to wheere the error was but i didnt notice any equalto or colon anywhere >:(

((distance_right && distance_left ) < 10 )

you’re going to drive me nuts til you fix that.

put serial prints at the top and bottom of the loop routine with the states and distances please.

i changed it

put serial prints at the top and bottom of the loop routine with the states and distances please.

:-?

put serial prints at the top and bottom of the loop routine with the states and distances please.

I guess this is what you meant

///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4     
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
int state = 1;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;
 
 
/////setting pinmode//////


void setup() 
{
  myservo.attach(9);
  Serial.begin(9600);                             
  pinMode(motor_ONE_pin_ONE,OUTPUT);     
  pinMode(motor_ONE_pin_TWO,OUTPUT);    
  pinMode(motor_TWO_pin_ONE,OUTPUT);     
  pinMode(motor_TWO_pin_TWO,OUTPUT);     
}


//////looping funtoinn//////


void loop()
{
  switch(state)
  {
    state = 1 ;
    Serial.print("in state 1 ");
     Serial.println();
    case 1:
    ping_read();
    drive_forward();
    if(cm < 10 )
    {
      state = 2;
      Serial.print("switching to state two");
      Serial.println();
    }
    break;
    case 2:
    ping_read_right();
    ping_read_left();
    state =3;
    Serial.print("in state three");
    Serial.println();
    break;
    case 3:
    if(distance_right > distance_left )
    {
      state =4;
      Serial.print("switching to state 4 ");
      Serial.println();
    }
    else if (distance_right < distance_left )
    {
      state =5;
      Serial.print("switching to state 5 ");
      Serial.println();
    }
    else if ( (distance_right<10) && (distance_left<10) ) 
    {
      state =6;
      Serial.print("switching to state 1 ");
      Serial.println();
    }
    
    else
    {
      state = 1 ;
      Serial.print("switching to state 1 ");
      Serial.println();
    }
    case 4:
    drive_right();
    break;
    case 5:
    drive_left();
    break;
    case 6:
    drive_360();
    break;
    default:  ;
  }
}
    



//////ping functions///////

void ping_read()
{
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);        
  Serial.print("cm");      
  Serial.println();
  return;
}
void ping_read_left()
{
  myservo.write(10);
  delay(150);
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_left = microsecondsToCentimeters(duration);
  Serial.print(distance_left);       
  Serial.print("cm");     
  Serial.println();
  return;
}
void ping_read_right()
{
  myservo.write(180);
  delay(150);
  pinMode(pingPin, OUTPUT);             
  digitalWrite(pingPin, LOW);           
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);          
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);           
  pinMode(pingPin, INPUT);                    
  duration = pulseIn(pingPin, HIGH);          
  distance_right = microsecondsToCentimeters(duration);
  Serial.print(distance_right);       
  Serial.print("cm");     
  Serial.println();
  return;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}


void drive_stop() 
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_right()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_left()
{
  digitalWrite(motor_ONE_pin_ONE,LOW);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,HIGH);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

void drive_360()
{
  digitalWrite(motor_ONE_pin_ONE,HIGH);
  digitalWrite(motor_TWO_pin_ONE,LOW);
  digitalWrite(motor_ONE_pin_TWO,LOW);
  digitalWrite(motor_TWO_pin_TWO,LOW);
  return;
}

even now the same thing occurs and this is what i get on the serial monitor after it put my hnad infront the ping

9cm
switching to state two
13cm
14cm
in state three
switching to state 5

and is it now driving left?

and is it now driving left?

Yes ! but the problem is the servo stays to the left position and i sillt cant see the drive forward occuring :-? i modified and

case 4: drive_right(); state = 1 ; break; case 5: drive_left(); state = 1; break; case 6: drive_360(); state = 1; break; default: ;

Bur still i cant see it drive forward

at the beginning of loop

 switch(state)
  {
    state = 1 ;
    Serial.print("in state 1 ");
     Serial.println();
    case 1:
    ping_read();
    drive_forward();

should probably be

 switch(state)
  {
     case 1:
    Serial.print("in state 1 ");
    Serial.println();
   ping_read();
   drive_forward();

and do you need a delay after drive_left() in state 5 to give it a chance to make a turn?

and shouldn't state 1 recenter the servo? before it reads it?

and do you need a delay after drive_left() in state 5 to give it a chance to make a turn?

and shouldn't state 1 recenter the servo? before it reads it?

fixed it ! But still cant get it to go forward :-?

and another problem ! ::slight_smile:
as soon as the osbtacle is detected the servo is set to 10 degrees and the leftwheel starts rotating without looking to thte right

fixed it ! But still cant get it to go forward

can you describe the behaviour now?

and another problem ! as soon as the osbtacle is detected the servo is set to 10 degrees and the leftwheel starts rotating without looking to thte right

do you need a delay after commanding the servo to move to give it a chance to get there? Looking at this: http://www.arduino.cc/playground/ComponentLib/Servo

they have a delay of 15-20ms.

the behaviour didnt change a bit ! by fixed it i eamnt thatr i set the code accordingly ! it dosent move forward ! only when a obstacle is detected it moves the servo and even be fore it looks to the right it starts rotaitng the left wheel ! >:( and i havew checked all the drive functiois they run as they sould when tried out individually

At the beginning of loop, before the cases, please print millis(), the current state, and the fwd, left, and right distances.

At the beginning of loop, before the cases, please print millis(), the current state, and the fwd, left, and right distances.

i'm prteey much confused a example ? ::)

Serial.println(" “);Serial.print(millis()); Serial.print(”:"); Serial.print(state); Serial.print(" "); Serial.print(cm); and so on

did you put a delay after the servo commands and do you now see it moving?

did you put a delay after the servo commands and do you now see it moving?

Yes the servo is working as it should :) ill add those my problem is that only when a obstacle is detected it moves the servo and even be fore it looks to the right it starts rotaitng the left wheel !