Advertisement
ossipee

Robot Rebels Firstbit Code

Apr 24th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.68 KB | None | 0 0
  1.  // This is some example code to get your first robot, wandering around your house and not hopefully not damaging Mothers furniture
  2.     // Lots more robots, building and making over at www.robotrebels.org
  3.     // I welcome all ideas to improve ossipee
  4.     // I have used the pins that corelate for my motor driver board, yours will be differant so adjust your pin numbers as needed
  5.     // The blink/led 13 is commented out but it is there to orientate you a bit
  6.    
  7.      
  8.     // add some libraries for ultra sonic sensor and sweep servo
  9.     #include <Servo.h>
  10.     #include <NewPing.h>
  11.  
  12.     // Lets label some arduino pins as inputs and outputs you will have to set up for the pins your robot uses/needs
  13.      
  14.     #define USTrigger 9       // picks ping trigger pin
  15.     #define USEcho 10         // picks ping recievor pin, signal in
  16.     #define MaxDistance 100   // sets pings maximum range
  17.     //#define LED 13          // picks pin 13 for included led on board not used right now
  18.     #define lMotor1 3         // left motor direction control #1 is on pin 3
  19.     #define lMotor2 4         // left motor direction control #2 is on pin 4
  20.     #define LPWM 5            // left motor pulsed width modulation, speed control
  21.     #define RPWM 6            // the next 3 are the same but for the right motor
  22.     #define rMotor1 7
  23.     #define rMotor2 8
  24.  
  25.     Servo servo;                            // Create sweep Servo object, lets arduino program know it has a servo to sweep ping
  26.     NewPing sonar(USTrigger, USEcho, MaxDistance);         // Create ultrasonic object, arduino now know's it's got a ping
  27.      
  28.     // 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
  29.     // if you sign them they can have positive and negitive values, thanks 6677, Birdmun also says that just using positive ones saves program space
  30.    
  31.     unsigned int duration;                // this is going to be time between sending and getting signal back from ping, gonna use it to make a distance
  32.     unsigned int distance;                // this is going to be the distance in cm we get from ping
  33.     unsigned int FrontDistance;           // this is the number we get from a ping reading looking straight ahead
  34.     unsigned int LeftDistance;            // this is the number we get from a ping reading looking to the left, check servo is really looking left
  35.     unsigned int RightDistance;           // same as above but looking right
  36.     unsigned int Time;
  37.     byte lSpeed1=255;                     // use this number to adjust your left motor speed out of the box the 2 motor might not be the same speed
  38.     byte rSpeed=255;                      // 255 is full speed
  39.      
  40.     void setup()                          // This part of the program runs 1 time at start up
  41.     {
  42.       pinMode(lMotor1,OUTPUT);            // sets our motor pins as outputs and low
  43.       pinMode(lMotor2,OUTPUT);
  44.       pinMode(LPWM, OUTPUT);
  45.       pinMode(RPWM,OUTPUT);
  46.       pinMode(rMotor1,OUTPUT);
  47.       pinMode(rMotor2, OUTPUT);
  48.      
  49.       servo.attach (11);                 // puts ping servo on pin 11
  50.     }
  51.      
  52.      
  53.     void loop()                                           // This block repeats itself while the Arduino is turned on
  54.     {
  55.       servo.write(90);                                    // position the ping servo to face ahead could have done above but like it here                
  56.       scan();                                             // Go to the scan function
  57.       FrontDistance = distance;                           // Set the variable FrontDistance to the value of the distance returned from the scan function
  58.       Serial.println("Front distance = ");
  59.       Serial.print(distance);
  60.       if(FrontDistance > 20 || FrontDistance == 0){       // If there is nothing infront of the robot within 20cm or the distance value is 0 (which for the newping libary means no ping was returned) then.
  61.      
  62.      
  63.        moveForward();                                     // Go to the moveForward function
  64.       }
  65.       else                                                // Else (if there is something infront of the robot within 40cm) then...
  66.       {
  67.         moveStop();                                       // Go to the moveStop function
  68.         navigate();                                       // Go to navigate function
  69.       }
  70.     }
  71.  
  72.     void motorSpeed(int thisLeft,int thisRight){             // sets our motors speed
  73.       analogWrite(LPWM,thisLeft);
  74.       analogWrite(RPWM,thisRight);
  75.     }
  76.      
  77.     void moveBackward(){                                     // this will tell both motors to backup your robot, you will need to adjust this
  78.       digitalWrite(lMotor1,LOW);
  79.       digitalWrite(lMotor2,HIGH);
  80.       digitalWrite(rMotor1,LOW);
  81.       digitalWrite(rMotor2,HIGH);                        
  82.     }
  83.      
  84.     void moveForward(){                                      // this will tell both motor to go forwards
  85.       digitalWrite(lMotor1,HIGH);
  86.       digitalWrite(lMotor2,LOW);
  87.       digitalWrite(rMotor1,HIGH);
  88.       digitalWrite(rMotor2,LOW);
  89.      
  90.     }
  91.      
  92.     void moveRight(){                                       // turns robot right, might be worth trying just stopping a motor as well
  93.       digitalWrite(lMotor1,HIGH);
  94.       digitalWrite(lMotor2,LOW);
  95.       digitalWrite(rMotor1,LOW);
  96.       digitalWrite(rMotor2,HIGH);
  97.     }
  98.      
  99.     void moveLeft(){                                        // turns robot left just opposite motor directions adjust as needed
  100.       digitalWrite(lMotor1,LOW);
  101.       digitalWrite(lMotor2,HIGH);
  102.       digitalWrite(rMotor1,HIGH);
  103.       digitalWrite(rMotor2,LOW);  
  104.     }
  105.      
  106.     void moveStop(){                                        // all pins low stops both motors
  107.       digitalWrite(lMotor1,LOW);
  108.       digitalWrite(lMotor2,LOW);
  109.       digitalWrite(rMotor1,LOW);
  110.       digitalWrite(rMotor2,LOW);  
  111.     }
  112.      
  113.     void scan(){                                            // This function determines the distance things are away from the ultrasonic sensor{
  114.       Serial.println("");
  115.       Serial.println("Scanning");
  116.       Time = sonar.ping();
  117.       distance = Time / US_ROUNDTRIP_CM;
  118.       delay(500);
  119.     }
  120.     void navigate()
  121.     {
  122.         Serial.println("There's an obstacle!");           //
  123.         servo.write(130);                                 // Move the servo to the left (my little servo didn't like going to 180 so I played around with the value until it worked nicely)
  124.         delay(1000);                                      // delay(1000) = 1000 millaseconds, 1 second
  125.         scan();                                           // Go to the scan function
  126.         LeftDistance = distance;                          // Set the variable LeftDistance to the distance on the left
  127.         Serial.println("Left distance = ");
  128.         Serial.print(distance);
  129.         servo.write(50);                                  // Move the servo to the right
  130.         delay(1000);                                      // Wait a second for the servo to get there
  131.         scan();                                           // Go to the scan function called above
  132.         RightDistance = distance;                         // Set the variable RightDistance to the distance on the right
  133.         Serial.println("Right distance = ");
  134.         Serial.print(distance);
  135.         if(abs(RightDistance - LeftDistance) < 5)
  136.         {
  137.           moveBackward();                                  // Go to the moveBackward function you can adjust the times to get the amount of reverse you need
  138.           delay(200);                                      // Pause the program for 200 milliseconds to let the robot reverse
  139.           moveRight();                                     // Go to the moveRight function you can adjust the time to get a 90 degree turn
  140.           delay(100);                                      // Pause the program for 100 milliseconds to let the robot turn right
  141.         }
  142.         else if(RightDistance < LeftDistance)              // If the distance on the right is less than that on the left then...
  143.         {
  144.          moveLeft();                                       // Go to the moveLeft function
  145.          delay(100);                                       // Pause the program for half a second to let the robot turn
  146.         }
  147.         else if(LeftDistance < RightDistance)              // Else if the distance on the left is less than that on the right then...
  148.         {
  149.          moveRight();                                      // Go to the moveRight function
  150.          delay(100);                                       // Pause the program for half a second to let the robot turn
  151.         }
  152.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement