Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Taken from ChickenParmis fantastic site http://www.duino-robotics.com/
- // add some libraries for ping and servo
- #include <Servo.h>
- #include <NewPing.h>
- #define USTrigger 10
- #define USEcho 11
- #define MaxDistance 100
- #define LED 13
- Servo leftServo; //Create Left Servo object
- Servo rightServo; //Create Right Servo object
- Servo servo; //Create sweep servo for ping
- NewPing sonar(USTrigger, USEcho, MaxDistance); //Create ultrasonic object
- //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
- //Not sure what this actualy means need to do some more googles
- unsigned int duration;
- unsigned int distance;
- unsigned int FrontDistance;
- unsigned int LeftDistance;
- unsigned int RightDistance;
- unsigned int Time;
- unsigned int CollisionCounter;
- void setup()
- {
- leftServo.attach(9); //Left servo connected to pin 9
- leftServo.write(90); //Write the neutral position to that servo
- rightServo.attach(8); //Right servo connected to pin 8
- rightServo.write(90); //Write the neutral position to that servo
- servo.attach (6); // puts ping servo on pin 6
- }
- void loop() //This block repeats itself while the Arduino is turned on
- {
- servo.write(90); //Rotate the servo to face the front
- scan(); //Go to the scan function
- FrontDistance = distance; //Set the variable FrontDistance to the value of the distance returned from the scan function
- Serial.println("Front distance = ");
- Serial.print(distance);
- 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...
- {
- moveForward(); //Go to the moveForward function
- }
- else //Else (if there is something infront of the robot within 40cm) then...
- {
- CollisionCounter = CollisionCounter + 1;
- moveStop(); //Go to the moveStop function
- navigate();
- }
- }
- void moveBackward(){
- rightServo.write(180);
- leftServo.write(0);
- }
- void moveForward(){
- rightServo.write(0);
- leftServo.write(180);
- }
- void moveRight(){
- rightServo.write(0);
- leftServo.write(0);
- }
- void moveLeft(){
- rightServo.write(180);
- leftServo.write(180);
- }
- void moveStop(){
- rightServo.write(90);
- leftServo.write(95);
- }
- 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(130); //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(50); //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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement