robot that should turn left on its own and keep itself going strait

hey guys im building a robot that we are going to race with mostley left habd turns. i am having a few issues with the code and would like to get your opinions. the main thing is it dont respond quick enough and runs into walls and well flat out i havent gotten it to turn yet heres the code. i have a ping sensor as my distance finder and im sweeping it on a servo

        #include <Servo.h> 
          Servo myservo;  // create servo object to control a servo  
          const int pingPin = 8;  //ping sensor pin number
          int pos = 90;    // variable to set the initial position of the servo 
          const int ping = 8; //ping pin number
          int reset = 2;// motordriver reset pin number
          
          void setup()
        {
          Serial.begin(9600); //sets serial communication bps
          pinMode (reset, OUTPUT);
          digitalWrite(reset,LOW);
          delay(50);
          digitalWrite(reset,HIGH);
          delay(50);
          myservo.attach(10);  // attaches the servo on pin 10 to the servo object 
          myservo.write(90); //center servo
          delay(500); //delay 1000ms  
          
        }
        void loop()
        {
            long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140 
        
          myservo.write(90); //move servo to pos 0
          delay(5); 
              pinMode(ping, OUTPUT);
               digitalWrite(ping, LOW);
               delayMicroseconds(2);
               digitalWrite(ping, HIGH);
               delayMicroseconds(5);
               digitalWrite(ping, LOW);
              
                pinMode(ping, INPUT);
              duration = pulseIn(ping, HIGH);
            
              inches = microsecondsToInches(duration);
         
          delay(500);
           
          myservo.write(20); //move servo to pos 20
          delay(5); 
                 
                 pinMode(ping, OUTPUT);
                 digitalWrite(ping, LOW);
                 delayMicroseconds(2);
                 digitalWrite(ping, HIGH);
                 delayMicroseconds(5);
                 digitalWrite(ping, LOW);
                
                  pinMode(ping, INPUT);
                duration = pulseIn(ping, HIGH);
              
                inches2 = microsecondsToInches(duration);
         
          delay(500);
          
          
           myservo.write(90); //move servo to pos 0
          delay(5); 
          
              pinMode(pingPin, OUTPUT);
              digitalWrite(pingPin, LOW);
              delayMicroseconds(2);
              digitalWrite(pingPin, HIGH);
              delayMicroseconds(5);
              digitalWrite(pingPin, LOW);
               pinMode(pingPin, INPUT);
              duration = pulseIn(pingPin, HIGH);
               inches = microsecondsToInches(duration);
           delay(500);
           
           myservo.write(140); //move servo to pos 0
           delay(5); 
                pinMode(pingPin, OUTPUT);
              digitalWrite(pingPin, LOW);
              delayMicroseconds(2);
              digitalWrite(pingPin, HIGH);
              delayMicroseconds(5);
              digitalWrite(pingPin, LOW);
               pinMode(pingPin, INPUT);
              duration = pulseIn(pingPin, HIGH);
               inches = microsecondsToInches(duration);
           
          delay(500);
          
           myservo.write(90); //move servo to pos 0
          delay(5); 
              
              pinMode(pingPin, OUTPUT);
              digitalWrite(pingPin, LOW);
              delayMicroseconds(2);
              digitalWrite(pingPin, HIGH);
              delayMicroseconds(5);
              digitalWrite(pingPin, LOW);
               pinMode(pingPin, INPUT);
              duration = pulseIn(pingPin, HIGH);
               inches3 = microsecondsToInches(duration);
           delay(500);
         
         if(inches > 50 ){
                   //left motor
                unsigned char buff1[6];
                buff1[0]=0x80; //start byte
                buff1[1]=0x00; //device type
                buff1[2]=0x01; //motor number and directiobyte; motor one =00,01
                buff1[3]=0x7F; //motor speed 0 to 128 in hex (ex 100 is 64 hex)
                for (int i=0; i<4; i++) {Serial.print(buff1[i],BYTE);}
                //right motor
               unsigned char buff2[6];
               buff2[0]=0x80;
               buff2[1]=0x00;
               buff2[2]=0x03; //motor and direction byte; motor two=02.03
               buff2[3]=0x7E;//7E is 126 in hex; 7D is 125 in hex set to smaller value to compensate for uneven wheel speed???
               for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
         
         
        }
           if (inches2 < 12 ){
             //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x40;
                  for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
           }
                
          if ( inches  < 50 && inches3 < 12 ){
                       //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x40;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                 for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
        
        if (inches3 < 12){
            //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x7F;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
        }
          long microsecondsToInches(long microseconds)
        {
           return microseconds / 74 / 2;  
        }
 delay(500);

Doesn't respond very quickly, you say?

You're doing 5 range readings in a row with 500ms delays in between. That's over 2.5 seconds where the Arduino will be unable to do anything else. Of course it's going to run into stuff.

First, do NOT use delay(). It's bad. The Arduino will do nothing else at all for that 500ms. Look at the Blink without Delay example for how to time events without blocking any other processing.

Also, your sensor should be pointed straight ahead while moving forward, not sweeping. You only sweep if it detects an obstacle in your way to find a new path to go in. And when you're sweeping, the robot should be stopped.

ok i will take out the delays, and so should i put my if statements in with the loop where im sweeping the servo or put them outside the loop?
and ill try keeping it forward when we are going strait

ok i re wrote a few things see what you think heres the new code.

#include <Servo.h> 
  Servo myservo;  // create servo object to control a servo  
  const int pingPin = 8;  //ping sensor pin number
  int pos = 90;    // variable to set the initial position of the servo 
  const int ping = 8; //ping pin number
  int reset = 2;// motordriver reset pin number
  
  void setup()
{
  Serial.begin(9600); //sets serial communication bps
  pinMode (reset, OUTPUT);
  digitalWrite(reset,LOW);
  delay(50);
  digitalWrite(reset,HIGH);
  delay(50);
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object 
  myservo.write(90); //center servo
  delay(500); //delay 1000ms  
}
 
  void forward()
{
      //left motor
                unsigned char buff1[6];
                buff1[0]=0x80; //start byte
                buff1[1]=0x00; //device type
                buff1[2]=0x00; //motor number and directiobyte; motor one =00,01
                buff1[3]=0x7F; //motor speed 0 to 128 in hex (ex 100 is 64 hex)
                for (int i=0; i<4; i++) {Serial.print(buff1[i],BYTE);}
                //right motor
               unsigned char buff2[6];
               buff2[0]=0x80;
               buff2[1]=0x00;
               buff2[2]=0x02; //motor and direction byte; motor two=02.03
               buff2[3]=0x7E;//7E is 126 in hex; 7D is 125 in hex set to smaller value to compensate for uneven wheel speed???
               for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}
    void rightWall(){
       //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x40;
                  for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
           }
   void leftWall(){
                    //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x7F;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
     void reverse(){
        //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x01;
                  buff3[3]=0x45;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x03; //motor and direction byte; motor two=02.03
                 buff2[3]=0x45;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
        void leftTurn(){
          
             //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x45;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x7D;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
        
        void rightTurn(){
          
           //left motor
                  unsigned char buff3[6];
                  buff3[0]=0x80;
                  buff3[1]=0x00;
                  buff3[2]=0x00;
                  buff3[3]=0x7F;
                 for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
                  //right motor
                 unsigned char buff2[6];
                 buff2[0]=0x80;
                 buff2[1]=0x00;
                 buff2[2]=0x02; //motor and direction byte; motor two=02.03
                 buff2[3]=0x45;
                for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
        }
        void loop(){
          
                 {
            long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140 
        
          myservo.write(90); //move servo to pos 0
          delay(5); 
              pinMode(ping, OUTPUT);
               digitalWrite(ping, LOW);
               delayMicroseconds(2);
               digitalWrite(ping, HIGH);
               delayMicroseconds(5);
               digitalWrite(ping, LOW);
              
                pinMode(ping, INPUT);
              duration = pulseIn(ping, HIGH);
            
              inches = microsecondsToInches(duration);
              
              
              myservo.write(20); //move servo to pos 20
          delay(5); 
                 
                 pinMode(ping, OUTPUT);
                 digitalWrite(ping, LOW);
                 delayMicroseconds(2);
                 digitalWrite(ping, HIGH);
                 delayMicroseconds(5);
                 digitalWrite(ping, LOW);
                
                  pinMode(ping, INPUT);
                duration = pulseIn(ping, HIGH);
              
                inches2 = microsecondsToInches(duration);
                
                 myservo.write(90); //move servo to pos 0
          delay(5); 
              
              pinMode(pingPin, OUTPUT);
              digitalWrite(pingPin, LOW);
              delayMicroseconds(2);
              digitalWrite(pingPin, HIGH);
              delayMicroseconds(5);
              digitalWrite(pingPin, LOW);
               pinMode(pingPin, INPUT);
              duration = pulseIn(pingPin, HIGH);
               inches3 = microsecondsToInches(duration);
               
                 }       
                }
          long microsecondsToInches(long microseconds)
        {
           return microseconds / 74 / 2;  
        }
   void wall()  
        }
      if(inches > 50 ){
        forward();
      }
      if (inhes2 < 12){
        rightWall();
      }
      if (inches3 < 12){
        leftWall();
      }
      if (inches < 50 && inches3 < 12){
        lerftTurn();
      }
      if (inches < 50 && inches2 < 12){
        rightTurn();
      }
   }

and heres the report i get when i compile it
sketch_may26c:175 error: expected initializer before '}' token
sketch_may26c:175 error: expected declaration before '}' token

Perhaps if you used random indenting, and random amounts of white space, you could mask where the curly braces don't match better.

There is a reason that code is indented, and that people make the effort to match indent levels for { and } (and put them on lines by themselves).

There's even a tool in the IDE to format the code for you.

PaulS is much like sandpaper. He starts off in the 40-60 grit range and, if you can stand the "rub", eventually works his way to applying a 80-120 grit.

In other words - he's a but rough at first but if you're patient things'll eventually smooth out and you learn things along the way.

lol ok ill just do that then. i rewrote my code tryng to use functions and subroutines like a good feller. and like everyone has been saying to do in the post i have read. it compiles fine but wont start up my motordriver everything else works great te ping ans sweep do exactley what want them to do. but no motors :frowning: pls help i am currently trying again and race for class is next week :frowning: i know motordriver works i can get it to work on its own and with the sweep and ping and respond to the ping but would like to have the subroutines and the servo dont like not hving the delays between the pings

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo  
const int ping = 8;  //ping sensor pin number
int pos = 90;    // variable to set the initial position of the servo 
int reset = 2;// motordriver reset pin number

void setup()
{
Serial.begin(9600); //sets serial communication bps
pinMode (reset, OUTPUT);
digitalWrite(reset,LOW);
delay(50);
digitalWrite(reset,HIGH);
delay(50);
myservo.attach(10);  // attaches the servo on pin 10 to the servo object 
myservo.write(90); //center servo
delay(400); //delay 1000ms  
}


void loop()
{

long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140 

myservo.write(90); //move servo to pos 0
delay(5); 
pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);

pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);

inches = microsecondsToInches(duration);
delay (400);

myservo.write(20); //move servo to pos 20
delay(5); 

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);

pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);

inches2 = microsecondsToInches(duration);
delay (400);

myservo.write(90); //move servo to pos 0
delay(5); 

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
inches = microsecondsToInches(duration);
delay (400);

  myservo.write(140); //move servo to pos 0
           delay(5); 
 pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
 pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
 inches3 = microsecondsToInches(duration);
 delay (400);
 
 myservo.write(90); //move servo to pos 0
delay(5); 

pinMode(ping, OUTPUT);
digitalWrite(ping, LOW);
delayMicroseconds(2);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
pinMode(ping, INPUT);
duration = pulseIn(ping, HIGH);
inches = microsecondsToInches(duration);
delay(100);

if(inches > 50 )
forward();

if (inches2 < 12)
rightWall(100);


if (inches3 < 12)
leftWall(100);

if (inches < 50 && inches3 > 52)
leftTurn(2000);

if (inches < 50 && inches2 > 52)
rightTurn(2000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;  
}
void wall() {
  long duration, inches, inches2, inches3; // establish variables for duration of ping, inches = 90; inches2 = 20; inches3 = 140 
{

}
}

void forward(){
//left motor
unsigned char buff1[6];
buff1[0]=0x80; //start byte
buff1[1]=0x00; //device type
buff1[2]=0x00; //motor number and directiobyte; motor one =00,01
buff1[3]=0x7F; //motor speed 0 to 128 in hex (ex 100 is 64 hex)
for (int i=0; i<4; i++) {Serial.print(buff1[i],BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7E;//7E is 126 in hex; 7D is 125 in hex set to smaller value to compensate for uneven wheel speed???
for(int i=0; i<4; i++)  {Serial.print(buff2[i],BYTE);}
}
void rightWall(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x40;
for(int i=0; i<4; i++){Serial.print(buff3[i], BYTE);} 

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);} 
}

void leftWall(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x7F;
for(int i=0; i<4; i++) { Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}
void reverse(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x01;
buff3[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x03; //motor and direction byte; motor two=02.03
buff2[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

void leftTurn(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x7D;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

void rightTurn(int duration)
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80;
buff3[1]=0x00;
buff3[2]=0x00;
buff3[3]=0x7F;
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}

//right motor
unsigned char buff2[6];
buff2[0]=0x80;
buff2[1]=0x00;
buff2[2]=0x02; //motor and direction byte; motor two=02.03
buff2[3]=0x45;
for(int i=0; i<4; i++) {Serial.print(buff2[i],BYTE);}
}

Do yourself a favour - shorten your code with a single range function, instead of typing the same code over and over again:

long getRangeInInches (int ping)
{
	pinMode(ping, OUTPUT);
	digitalWrite(ping, LOW);
	delayMicroseconds(2);
	digitalWrite(ping, HIGH);
	delayMicroseconds(5);
	digitalWrite(ping, LOW);

	pinMode(ping, INPUT);
	long duration = pulseIn(ping, HIGH);

	return microsecondsToInches(duration);
}

With shorter code, it may make it easier for you to see where you are overwriting results.

Do the rest of us a favour by typing ctrl-T in the IDE before posting your code.

Humans are really bad at repetition; computers are really good at it.

void forward()
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x7E);
}

void rightWall(int duration)
{
  moveMotor (0, 0x40);
  moveMotor (2, 0x7D);
}

void leftWall(int duration)
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x7D);
}

void reverse(int duration)
{
  moveMotor (1, 0x45);
  moveMotor (3, 0x45);
}

void leftTurn(int duration)
{
  moveMotor (0, 0x45);
  moveMotor (2, 0x7D);
}

void rightTurn(int duration)
{
  moveMotor (0, 0x7F);
  moveMotor (2, 0x45);
}

void moveMotor (unsigned char motor, unsigned char speed)
{
  unsigned char buff[4];
  //left motor
  buff[0]=0x80;
  buff[1]=0x00;
  buff[2]=motor;
  buff[3]=speed;
  for(int i=0; i<4; i++) {
    Serial.print(buff[i], BYTE);
  }
}

Reducing source size gives bugs fewer dusty corners to hide in.

Hey awol thx for the response ill put that together tonight. I have a working one but its pretty long and repedative. But it works and the race for class is on thursday the 2nd so i really apreciate the help thx again
I