i have this code but it has erors i don't know enough about programing arduino to get it to work
#include <Servo.h>
/*
* My First Robot
*
* This robot drive around and avoids objects with the Ping sonar.
*
*/
int motor_left[] = {3, 4};
int motor_right[] = {5, 6};
int pingPin = 9;
int servoPin = 2;
int servoRange[] = {500, 2500}; // The min/max pulse lengths to send to the servo
int servoRotateTime = 500; // How long it takes to turn the servo 180 degrees
int dangerDist = 300; // How far should something be before we react?
int servoTime = 700; // How long it takes to turn the servo
int turnTime = 250; // How long it takes to turn 90 degrees with the motors
Servo servo;
// This is called when the Arduino starts up
void setup(){
Serial.begin(9600);
printString("Start\n");
// Setup motor
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
// Servo
servo.attach(2);
servo.setMinimumPulse(500);
servo.setMaximumPulse(2500);
turn_servo(100);
}
// Arduino calls this infinite loop after setup()
void loop(){
int dist = read_ping(); // Read distance ahead
// There's nothing right in front of us
if(dist > dangerDist){
Serial.println("All Good\n");
drive_straight();
delay(50);
}
// Object ahead, find a better route
else{
Serial.println("Object Ahead\n");
change_direction();
delay(50);
}
}
// Something is in front of us, so find the best alternate route.
// Look left and right and decide which has more distance to drive.
void change_direction(){
motor_stop();
// Look left
turn_servo(180);
int left = read_ping_avg();
// Look right
turn_servo(10);
int right = read_ping_avg();
// Neither are good options, turn around
if(left < dangerDist && right < dangerDist){
turn_right();
delay(turnTime * 2);
drive_straight();
}
// Right is better
else if(left < right){
turn_right();
delay(turnTime);
drive_straight();
}
// Left is better
else {
turn_left();
delay(turnTime);
drive_straight();
}
}
// Read the distance from the Ping sensor
int read_ping(){
int timecount = 0;
/*
* Send trigger pulse to ping to tell it to start
*/
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(pingPin, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(pingPin, LOW); // Holdoff
/*
* Listen for echo pulses
*/
// Wait for input
int val = digitalRead(pingPin); // Append signal value to val
pinMode(pingPin, INPUT); // Switch signalpin to input
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(pingPin);
}
// Count pulses
while(val == HIGH) { // Loop until pin reads a low value
val = digitalRead(pingPin);
timecount = timecount +1;
}
delay(10);
return timecount;
}
// Get the average distance from 3 pings
int read_ping_avg(){
int total = 0;
total += read_ping();
total += read_ping();
total += read_ping();
int avg = total/3;
return avg;
}
// Turn the servo to a degree between 1 - 180.
void turn_servo(int degree){
int servoStopAt = 0;
// Set stop time
if(servoStopAt == 0){
servoStopAt = millis() + servoRotateTime;
}
servo.write(degree);
// Refresh until the servo is done moving
// (using approximate timing set by servoRotateTime)
while(servoStopAt > millis()){
delay(20);
Servo::refresh();
}
}
/*
* Motor movements
*/
void motor_stop(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], HIGH);
}
void drive_straight(){
turn_servo(100); // Look straight ahead
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_left(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
any help greatily apreciated