UltimatePotato

Engineering Design II Lab 11

Apr 15th, 2021 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. /*Lab 11
  2.  *
  3.  * Authour: [REDACTED]
  4.  *
  5.  * The purpose of this program is to make a robot roam using ir tech, avoiding walls and corners in the process.
  6.  *
  7.  */
  8.  
  9. #include <Servo.h>                                                              // include servo library  
  10. Servo servoLeft;                                                                // declare left and right servos
  11. Servo servoRight;
  12.  
  13. void setup()                                                                    // built-in initialization block
  14. {
  15.   pinMode(13, INPUT);  pinMode(12, OUTPUT);                                     // left IR LED & Receiver
  16.   pinMode(3, INPUT);  pinMode(2, OUTPUT);                                       // right IR LED & Receiver
  17.   pinMode(8, OUTPUT); pinMode(7, OUTPUT);                                       // indicator LEDs
  18.  
  19.   tone(4, 3000, 1000);                                                          // play tone for 1 second
  20.   delay(1000);                                                                  // delay to finish tone
  21.  
  22.   servoLeft.attach(11);                                                         // attach left signal to pin 13
  23.   servoRight.attach(10);                                                        // attach right signal to pin 12
  24. }  
  25.  
  26. void loop()                                                                     // main loop auto-repeats
  27. {
  28.  
  29.   int irLeft = irDetect(12, 13, 44000);                                         // check for object on left
  30.   int irRight = irDetect(2, 3, 44000);                                          // check for object on right
  31.   digitalWrite(8, !irLeft);                                                     // LED states opposite of IR
  32.   digitalWrite(7, !irRight);
  33.   if((irLeft == 0) && (irRight == 0))                                           // if both sides detect
  34.   {
  35.     maneuver(-200, -200, 20);                                                   // backward 20 milliseconds
  36.   }
  37.   else if(irLeft == 0)                                                          // if only left side detects
  38.   {
  39.     maneuver(200, -200, 20);                                                    // right for 20 ms
  40.   }
  41.   else if(irRight == 0)                                                         // if only right side detects
  42.   {
  43.     maneuver(-200, 200, 20);                                                    // left for 20 ms
  44.   }
  45.   else                                                                          // otherwise, no IR detects
  46.   {
  47.     maneuver(200, 200, 20);                                                     // forward 20 ms
  48.   }
  49. }
  50.  
  51. int irDetect(int irLedPin, int irReceiverPin, long frequency)
  52. {
  53.   tone(irLedPin, frequency, 8);                                                  // IRLED 38 kHz for at least 1 ms
  54.   delay(1);                                                                      // wait 1 ms
  55.   int ir = digitalRead(irReceiverPin);                                           // IR receiver -> ir variable
  56.   delay(1);                                                                      // down time before recheck
  57.   return ir;                                                                     // return 1 no detect, 0 detect
  58. }  
  59.  
  60. void maneuver(int speedLeft, int speedRight, int t)
  61. {
  62.   // speedLeft, speedRight ranges: Backward  Linear  Stop  Linear   Forward
  63.   //                               -200      -100......0......100       200
  64.   servoLeft.writeMicroseconds(1500 + speedLeft);                                 // set left servo speed
  65.   servoRight.writeMicroseconds(1500 - speedRight);                               // set right servo speed
  66.   if(t == -1)                                                                    // if time = -1
  67.   {                                  
  68.     servoLeft.detach();                                                          // stop servo signals
  69.     servoRight.detach();  
  70.   }
  71.   delay(t);                                                                      // delay for t
  72. }
Add Comment
Please, Sign In to add comment