I finally got the robot to avoid obstacle, thanks to the forum. Now I'm needing to vary the speed of the motors. The left motor does not move, however the right motor does. The analogwrite commands are on line 57 and 60 for speed control. My ultimate goal is to control the remote by Bluetooth or xbee, or perhaps recommendations from others. The remote device needs to offer speed control. I'm still learning the Aurduino and appreciate suggestions for speed control in this sketch and remote controllers.
Is MIT inventors kit a good place to start for Bluetooth control?
//Since we are using servos and ultrasonic sensors in the robot we will include some libraries written to make their use easier
1#include <Servo.h>
2#include <NewPing.h>
//Below are the symbolic constants. Instead of having to type in a non-sensical pin number each time we want to do something we can write an easy to understand name which represents the pin, the compiler will then replace the names with the numbers
2 #define LeftMotorForward 8
3 #define LeftMotorBackward 9
4 #define LeftMotorSpeed 10
5 #define RightMotorForward 6
6 #define RightMotorBackward 7
7 #define RightMotorSpeed 5
8 #define USTrigger 3
9 #define USEcho 2
10 #define MaxDistance 100
11 #define LED 13
//Here we have created two 'objects', one for the servo and one for the ultrasonic sensor
12 Servo servo;
13 NewPing sonar(USTrigger, USEcho, MaxDistance);
//Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values
14 unsigned int duration;
15 unsigned int distance;
16 unsigned int FrontDistance;
17 unsigned int LeftDistance;
18 unsigned int RightDistance;
19 unsigned int Time;
20 unsigned int CollisionCounter;
21 void setup() //This block happens once on startup
22 {
23 Serial.begin(9600); //I have included the serial initialize but commented it out, if you want to debug and print information to the serial monitor just uncomment
//Here we are setting the pin modes. As we will sending out signals from the pins we set them as outputs
24 pinMode(LeftMotorForward, OUTPUT);
25 pinMode(LeftMotorBackward, OUTPUT);
26 pinMode(LeftMotorSpeed, OUTPUT);
27 pinMode(RightMotorForward, OUTPUT);
28 pinMode(RightMotorBackward, OUTPUT);
29 pinMode(RightMotorSpeed, OUTPUT);
30 pinMode(LED, OUTPUT);
31 servo.attach(11); //The servo is attached to pin 4
32 }
33 void loop() //This block repeats itself while the Arduino is turned on
34 {
35 servo.write(90); //Rotate the servo to face the front
36 scan(); //Go to the scan function
37 FrontDistance = distance; //Set the variable FrontDistance to the value of the distance returned from the scan function
38 Serial.println("Front distance = ");
39 Serial.print(distance);
40 if(FrontDistance > 40 || FrontDistance == 0) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then...
41 {
42 moveForward(); //Go to the moveForward function
43 }
44 else //Else (if there is something infront of the robot within 40cm) then...
45 {
46 CollisionCounter = CollisionCounter + 1;
47 moveStop(); //Go to the moveStop function
48 navigate();
49 }
50 }
51 void moveForward() //This function tells the robot to go forward
52 {
53 Serial.println("");
54 Serial.println("Moving forward");
//Turn on left motor
55 digitalWrite (LeftMotorBackward, LOW);
56 digitalWrite(LeftMotorForward, HIGH);
//set speed
57 analogWrite(LeftMotorSpeed, 50);
// turn on right motor
58 digitalWrite(RightMotorBackward, LOW);
59 digitalWrite(RightMotorForward, HIGH);
// set speed
60 analogWrite (RightMotorSpeed, 100);
}
void moveBackward() //This function tells the robot to move backward
{
Serial.println("");
Serial.println("Moving backward");
digitalWrite(LeftMotorSpeed, HIGH);
digitalWrite (RightMotorSpeed, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
}
void moveLeft() //This function tells the robot to turn left
{
Serial.println("");
Serial.println("Moving left");
digitalWrite(LeftMotorSpeed, HIGH);
digitalWrite (RightMotorSpeed, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(RightMotorForward, HIGH);
}
void moveRight() //This function tells the robot to turn right
{
digitalWrite(LeftMotorSpeed, HIGH);
digitalWrite (RightMotorSpeed, HIGH);
Serial.println("");
Serial.println("Moving right");
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
}
void moveStop() //This function tells the robot to stop moving
{
Serial.println("");
Serial.println("Stopping");
digitalWrite(LeftMotorSpeed, HIGH);
digitalWrite (RightMotorSpeed, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void scan() //This function determines the distance things are away from the ultrasonic sensor
{
Serial.println("");
Serial.println("Scanning");
Time = sonar.ping();
distance = Time / US_ROUNDTRIP_CM;
delay(500);
}
void navigate()
{
Serial.println("There's an obstacle!");
servo.write(167); //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
delay(1000); //Wait half a second for the servo to get there
scan(); //Go to the scan function
LeftDistance = distance; //Set the variable LeftDistance to the distance on the left
Serial.println("Left distance = ");
Serial.print(distance);
servo.write(0); //Move the servo to the right
delay(1000); //Wait half a second for the servo to get there
scan(); //Go to the scan function
RightDistance = distance; //Set the variable RightDistance to the distance on the right
Serial.println("Right distance = ");
Serial.print(distance);
if(abs(RightDistance - LeftDistance) < 5)
{
moveBackward(); //Go to the moveBackward function
delay(200); //Pause the program for 200 milliseconds to let the robot reverse
moveRight(); //Go to the moveRight function
delay(100); //Pause the program for 200 milliseconds to let the robot turn right
}
else if(RightDistance < LeftDistance) //If the distance on the right is less than that on the left then...
{
moveLeft(); //Go to the moveLeft function
delay(100); //Pause the program for half a second to let the robot turn
}
else if(LeftDistance < RightDistance) //Else if the distance on the left is less than that on the right then...
{
moveRight(); //Go to the moveRight function
delay(100); //Pause the program for half a second to let the robot turn
}
}