What wrong with my code?

#include <Servo.h>

Servo myservoL;
Servo myservoR;
int myservo = 2;
int myAngle;
int pulseWidth;

void setup()
{
pinMode(myservo, OUTPUT);
myservoL.attach(9);
myservoR.attach(10);
}

void servoPulse(int myservo, int myAngle)
{
pulseWidth = (myAngle *10) + 600;
digitalWrite(myservo, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(myservo, LOW);
}

void loop()
{
for (myAngle = 15; myAngle <= 165; myAngle++)
{
servoPulse(myservo, myAngle);
delay(20);
}
for (myAngle = 165; myAngle >= 15; myAngle--)
{
servoPulse(myservo, myAngle);
delay(20);
}


int distance = analogRead(0);
if (distance <= 451) // about 2 in.
{
myservoL.write(90); // stop
myservoR.write(87); // move back to turn right
delay(1000);
}
else
{
myservoL.write(0); // move forward
myservoR.write(180); // move forward

}
}

 

All this does is make my servo that attach to the sensor scan back and forth and left wheel will go back slow while right wheel goes forward slow. Then left wheel will stop after my servo scan for 15degree to 165degree and back to 15degree while my right wheel will start turning back.

You read the analog sensor

You read the analog sensor only 1 time per loop and always at the same position where your servo stopps. You need to read it after each servo movement (inside the for loops).

I would suggest to store the result of each sensor reading in an array and make a decision witch way to go after verifiing each array entry.

Hope you understand my humbled english.

So placing If (distance <=

So placing

If (distance <= 451 && myAngle is <= 87) // distance is 2 in or less and angle between 0 - 87 degree

{

myservoL.write(90); //make it stop

myservoR.write(0); // make it go back to turn right

delay(2000); // wait 2 sec

}

else if (distance <= 451 && myAngle is >= 93) // distance less then 2 in and angle between 180-93 degree

{

myservoL.write(180); // make it go back to turn left

myservoR.write(90); // make it stop

delay(2000);

}

else

{

myservoL.write(0); // go forward

myservoR.write(180); // go forward

}

}

 

 

So is this still wrong? What can I do to fix this. I never did get good with array so don’t know much about it. If someone could help me out that would be really awesome. Trying to get this done in the next few hours so I can show my family when I go see them.

I can’t give you a working

I can’t give you a working example from scratch, only an idea how this task can be done. So here is my “program” more or less in pseudocode (only main loop and some variable and defines, rest is from your program):

 

#define THRESHOLD 451 // distance sensor threshold
#define SERVO_WAIT 100 // 100ms wait time for Servo turn 15degree (depends on servo check datasheet

int distance[9] // array for the distance values (0…2 left, 3…5 center, 6…8 right sector)

int last_sweep_right = 0; // flag for the last sensor sweep we have done last loop round (0=left, 1=right)

loop()
{
int i;

if (last_sweep_right) //last round we sweep to right?

{
last_sweep_right = 0;

i=0; // from left to right

for (myAngle = 30; myAngle <= 150; myAngle+=15)
{
servoPulse(myservo, myAngle);
delay(SERVO_WAIT);
distance[i++] = analogRead(0); // read sensor in array

}

}

else

{

last_sweep_right = 1;
i=8; // from right to left

for (myAngle = 150; myAngle >= 30; myAngle-=15)
{
servoPulse(myservo, myAngle);
delay(SERVO_WAIT);
distance[i–] = analogRead(0); // read sensor in array

}

 

if (distance[3] > THRESHOLD && distance[4] > THRESHOLD && distance[5] > THRESHOLD) // center free?
{

// go forward

}

else if (distance[0] > THRESHOLD && distance[1] > THRESHOLD && distance[2] > THRESHOLD) // left free?
{

// turn left

}

else if (distance[6] > THRESHOLD && distance[7] > THRESHOLD && distance[8] > THRESHOLD) // right free?
{

// turn right

}

else // wall ahead

{

// go backward for 1 second or two, turn right or left

}

}

 

#include <Servo.h>#define

#include <Servo.h>
#define range 451 // distance sensor threshold
#define SERVO_WAIT 100 // 100ms wait time for Servo turn 15degree (depends on servo check datasheet

Servo myservoL;
Servo myservoR;
int myservo = 2;
int myAngle;
int pulseWidth;
int last_sweep_right = 0; // flag for the last sensor sweep we have done last loop round (0=left, 1=right)
int distance[9] // array for the distance values (0…2 left, 3…5 center, 6…8 right sector)

void setup()
{
pinMode(myservo, OUTPUT);
myservoL.attach(9);
myservoR.attach(10);
}

void loop()
{
int i;

if (last_sweep_right) //last round we sweep to right?

{
last_sweep_right = 0;

i=0; // from left to right

for (myAngle = 30; myAngle <= 150; myAngle+=15)
{
servoPulse(myservo, myAngle);
delay(SERVO_WAIT);
distance[i++] = analogRead(0); // read sensor in array

}

}

else

{

last_sweep_right = 1;
i=8; // from right to left


for (myAngle = 150; myAngle >= 30; myAngle-=15)
{
servoPulse(myservo, myAngle);
delay(SERVO_WAIT);
distance[i–] = analogRead(0); // read sensor in array


}

 

if (distance[3] > range && distance[4] > range && distance[5] > range) // center free?
{

// go forward

}

else if (distance[0] > range && distance[1] > range && distance[2] > range) // left free?
{

myservoL.write(180);
myservoR.write(90); // turn left
delay(2000);

}

else if (distance[6] > THRESHOLD && distance[7] > THRESHOLD && distance[8] > THRESHOLD) // right free?
{

myservoL.write(90);
myservoR.write(180);
delay(2000); // turn right


}

else // wall ahead

{

myservoL.write(180);
myservoR.write(0);
delay(2000);
myservoL.write(90);
myservoR.write(0);// go backward for 1 second or two, turn right or left

}

}

 

Thats pretty close to what your saying. The last part to get it to choose left and right I will have to work on. I’m getting and error: expected initializer before ‘void’ In function ‘void loop()’: What does that mean, it keep going up to the space between #define Servo_Wait 100 and Servo myservoL; thanks a bunch for helping me out. Sorry this is my first robot and programming something like this.

Edit: I deleted the space and now its highlighting the #define Servo_Wait 100 line. Something I’m not understanding.

There is a semicolon missing

There is a semicolon missing at the line

int distance[9];

Thank you so much
Thank you so much RobotFreak. Your code help me alot by adding it with mine. I had to change a few things but now it avoid walls. I just have to figure out how to choose a random direction if something is in front of it. Still you was able to get the code working for me so now when I go see my dad and step mom they are going to freak out lol. Also get to tell them they are having a grandson. Thanks again for helping me out.

Yea I figure that what it
Yea I figure that what it might of been, also was missing a } to. those things are easily over looked lol.

Why chose a random

Why chose a random direction? Why not look to each side, and chose the best direction? Or perhaps remember the most interesting path based on what happened just before?

:slight_smile:

 

Yea that would be very cool
Yea that would be very cool to do but I’m not that good at programming yet. You have any tips how I would go about coming up with a code for it. Thanks and also I will be adding my robot up in a few days.