Advertisement
Guest User

Untitled

a guest
Oct 21st, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3. Servo servoLeft;
  4. Servo servoRight;
  5.  
  6. void setup()                                 // Built-in initialization block
  7. {
  8.   tone(4, 3000, 1000);                       // Play tone for 1 second
  9.   delay(1000);                               // Delay to finish tone
  10.   pinMode(10, INPUT);  
  11.   pinMode(9, OUTPUT);   // Left IR LED & Receiver
  12.  
  13.   pinMode(2, OUTPUT);
  14.   pinMode(3, INPUT);
  15.   Serial.begin(9600);  
  16.  
  17.   servoLeft.attach(13);
  18.   servoRight.attach(12);
  19. }  
  20.  
  21. void loop()                                  // Main loop auto-repeats
  22. {
  23.   int irRight = irDetect(2, 3, 38000);
  24.   int irLeft = irDetect(9,10, 38000);
  25.   Serial.println(irLeft, irRight);
  26.   delay(100);
  27.  
  28.   if (irLeft == 0 && irRight == 0) { // if IR LED/RECEIVER detects both sides
  29.   servoLeft.writeMicroseconds(1300); // Left wheel clockwise
  30.   servoRight.writeMicroseconds(1300); // Right wheel clockwise
  31.  
  32.   }
  33.  
  34.   else if (irLeft == 1 && irRight == 0){ //Right IR detected, Left IR not detected
  35.   servoLeft.writeMicroseconds(1300); // Left wheel cclockwise
  36.  
  37.  }
  38.   else if (irLeft == 0 && irRight == 1){ //Right IR not detected, Left IR detected
  39.  servoRight.writeMicroseconds(1300); // Right wheel clockwise
  40.  
  41.  }
  42.  else if (irLeft == 1 && irRight == 1) { // if both receivers detect nothing
  43.    servoRight.writeMicroseconds(1700); //reverse left wheel
  44.    servoLeft.writeMicroseconds(1700); //reverse right wheel
  45. }
  46. }
  47.  
  48. int irDetect (int irLedPin, int irReceiverPin, long frequency) {
  49.   tone(irLedPin , frequency, 8);
  50.   delay(1);
  51.   int ir = digitalRead(irReceiverPin);
  52.   delay(1);
  53.   return ir;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement