Ping))) sensor problem

I'm new to arduino, and I'm trying to get a 2WD platform with a PING))) sensor to go through a maze. I come from an automotive repair background and I have a good foundations on building circuits, but the board programming I'm clueless. I've found some bits of code online and taking stuff from the examples, I've got it pieced together something that works most of the time. The main problem I'm having is after it runs up to a wall, it will turn, and then the ping sensor won't read clear in front of it. As you can see in the video it doesn't start reading again. It will stutter forward, sweep the sensor again, stutter, sweep etc., then go forward like normal after a couple attempts.

Here is a short clip of what happens.

The code.

#include <Servo.h>
Servo servo1; // This is the servo
#include <AFMotor.h>
AF_DCMotor motor4(4, MOTOR12_1KHZ); //right motor
AF_DCMotor motor3(3, MOTOR12_1KHZ); //leftmotor

const int numOfReadings = 180;      
int distance[numOfReadings]; 
int arrayIndex = 0; 
double totalL = 0; 
double totalR = 0; 
double averageL = 0; 
double averageR = 0;
int pos2 = 0;


const int pingPin = 9; // input of the PING signal
long duration, inches, cm;

void setup()  {
  Serial.begin(9600);
  servo1.attach(10); //servo input
  motor3.setSpeed(200);
  motor4.setSpeed(210
  );
  for(int thisReading = 0; thisReading < numOfReadings; thisReading++)
  {
    distance[thisReading] = 0;
  }
} 
 
 
void loop() {
  for(int pos = 0; pos < 180; pos = pos += 30)  // goes from 0 degrees to 180 degrees in 15deg segments
  {                                   
    servo1.write(pos);    // tell servo to go to position in variable 'pos' 
    delay(350);                       // waits 15ms for the servo to reach the position 
    distance[pos] = ping();
     delay(350);
  } 
 
  servo1.write(74);      // set servo straight ahead
  
 for(int i = 0; i<3 ; i++)
{
int pos = 0;
totalL = totalL + distance[pos];
pos = pos + 30;
}

for(int i = 0; i<3 ; i++)
{
int pos = 120;
totalR = totalR + distance[pos];
pos = pos + 30;
}

averageL = totalL / 3
;
averageR = totalR / 3;
  
  if(distance[90] > 20)
  {
    go();    
  }
  else if(distance[90] <= 20 && averageL > averageR)  //if distance in front it less then 20cm turn left
  {
    turnL();
  }
  else if(distance[90] <= 20 && averageL < averageR) // if front is less then 20cm turn right
  {
    turnR();
  }
}

void stp()
{
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void go()
{
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  {
    
    distance[pos2] = ping();
  delay(100);  
  if(distance[pos2] < 20)
  {
    stp();
  }
  else
  {
    go();    
  }
  }
  stp();
}

void turnR()
{
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
  delay(300
  );
  stp();
}

void turnL()
{
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
  delay(300);
  stp();
}

long ping()
{
  long duration, cm;

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


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


cm = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}

I don't know if it will help but this is very likely a mistake...

for(int pos = 0; pos < 180; pos = pos += 30) // goes from 0 degrees to 180 degrees in 15deg segments

...and should be removed.

This is a problem, too:

 for(int i = 0; i<3 ; i++)
{
int pos = 0;
totalL = totalL + distance[pos];
pos = pos + 30;
}

Every time through the loop, pos will be set to 0, distance[0] will be added to totalL, and pos will be set to 30.

I'm guessing that is not what you want to have happen.

You have an array that can hold 180 values, but you are only putting values at 0, 30, 60, 90l 120, and 150. Why all the wasted space?

Forgive my noobness but I can't even tell you what that means, I have assumed that it was taking the reading from servo position 0,30, & 90 deg to get the right side distance average. Being that i'm only trying to make left or right hand turns. Also the same for 120,150,180.

Are you saying that I should sweep the servo at 1 deg intervals and change that to pos + 1 to get a more in depth scan?

I think I have made some improvements, by adding a delay(1000) after servo write 74 its giving it enough time to move before starting the ping() again.

I also decresed the sweep to every 15 deg and adjusted the left/right to use up the some "wasted space."

I've been trying to get the servo to sweep about a 60 deg angle (74deg is straight ahead so 74 + & - 30 deg) as its in the go forward mode. I having trouble with it running into object and it not knowing its stuck.

#include <Servo.h>
Servo servo1; // This is the servo
#include <AFMotor.h>
AF_DCMotor motor4(4, MOTOR12_1KHZ); //right motor
AF_DCMotor motor3(3, MOTOR12_1KHZ); //leftmotor

const int numOfReadings = 180;      
int distance[numOfReadings]; 
int arrayIndex = 0; 
double totalL = 0; 
double totalR = 0; 
double averageL = 0; 
double averageR = 0;
int pos2 = 0;


const int pingPin = 9; // input of the PING signal
long duration, inches, cm;

void setup()  {
  Serial.begin(9600);
  servo1.attach(10); //servo input
  motor3.setSpeed(200);
  motor4.setSpeed(215);
  for(int thisReading = 0; thisReading < numOfReadings; thisReading++)
  {
    distance[thisReading] = 0;
  }
} 
 
 
void loop() {
  for(int pos = 0; pos < 180; pos += 15)  // goes from 0 degrees to 180 degrees in 15deg segments
  {                                   
    servo1.write(pos);    // tell servo to go to position in variable 'pos' 
    delay(350);                       // waits 15ms for the servo to reach the position 
    distance[pos] = ping();
     delay(350);
  } 
 
 servo1.write(74);  // set servo straight ahead
  delay(1000);
  
 for(int i = 0; i<3 ; i++)
{
int pos = 0;
totalL = totalL + distance[pos];
pos = pos + 15;
}

for(int i = 0; i<3 ; i++)
{
int pos = 105;
totalR = totalR + distance[pos];
pos = pos + 15;
}

averageL = totalL / 6;

averageR = totalR / 6;
  
  if(distance[90] >= 20)
  {
    go();    
  }
  else if(distance[90] <= 25 && averageL > averageR)  //if distance in front it less then 20cm turn left
  {
    turnL();
  }
  else if(distance[90] <= 25 && averageL < averageR) // if front is less then 20cm turn right
  {
    turnR();
  }
}

void stp()
{
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void go()
{
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  {    
    distance[pos2] = ping(); 
   delay(100); 
  if(distance[pos2] < 25)
  {
    stp();
  }
  else
  {
    go();    
  }
  }
  stp();
}

void turnL()
{
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
  delay(300);
  stp();
}

void turnR()
{
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
  delay(300);
  stp();
}

long ping()
{
  long duration, cm;

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


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


cm = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}
for(int i = 0; i<3 ; i++)
{
int pos = 0;
totalL = totalL + distance[pos];
pos = pos + 15;
}

Is just another way of saying:

totalL += distance[0] * 3;

You need to take "pos" out of the local scope.

for(int i = 0, int pos = 0; i<3 ; i++, pos += 15)
{
    totalL += distance[pos];
}

(or as PaulS has already pointed out, you need to rethink your algorithm. You're reserving 360 bytes of valuable RAM, and using a tiny part of it.)

The IDE has an auto format feature under the tools drop-down - try using it, it helps make your code more legible.

after reading up on the += in the getting started book I understand that, but I'm now getting an error compiling at line

for(int i = 0, int pos = 0; i<3 ; i++, pos += 15)

this is the error code.

Maze_Solver_pde2.cpp: In function 'void loop()':
Maze_Solver_pde2:44: error: expected unqualified-id before 'int'
Maze_Solver_pde2:44: error: name lookup of 'pos' changed for new ISO 'for' scoping
Maze_Solver_pde2:33: error: using obsolete binding at 'pos'
Maze_Solver_pde2:49: error: expected unqualified-id before 'int'

After trying to find out why this error is happening, I removed the "int i = 0" and it compiles. I'm guessing here that defining two int in the () is causing a problem, or is something else going on?

for(int i = 0, [glow]int [/glow]pos = 0; i<3 ; i++, pos += 15)

All the variables in the initialization section must be the same type. The int keyword may not be repeated after the ,.

Thank you for the help, and the education. I'm starting to get the hang of programing this simple thing.

Oopsie - insufficient attention.
My fault. :-[