Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo servoLeft;
- Servo servoRight;
- void setup() // Built-in initialization block
- {
- tone(4, 3000, 1000); // Play tone for 1 second
- delay(1000); // Delay to finish tone
- pinMode(10, INPUT);
- pinMode(9, OUTPUT); // Left IR LED & Receiver
- pinMode(2, OUTPUT);
- pinMode(3, INPUT);
- Serial.begin(9600);
- servoLeft.attach(13);
- servoRight.attach(12);
- }
- void loop() // Main loop auto-repeats
- {
- int irRight = irDetect(2, 3, 38000);
- int irLeft = irDetect(9,10, 38000);
- Serial.println(irLeft, irRight);
- delay(100);
- if (irLeft == 0 && irRight == 0) { // if IR LED/RECEIVER detects both sides
- servoLeft.writeMicroseconds(1300); // Left wheel clockwise
- servoRight.writeMicroseconds(1300); // Right wheel clockwise
- }
- else if (irLeft == 1 && irRight == 0){ //Right IR detected, Left IR not detected
- servoLeft.writeMicroseconds(1300); // Left wheel cclockwise
- }
- else if (irLeft == 0 && irRight == 1){ //Right IR not detected, Left IR detected
- servoRight.writeMicroseconds(1300); // Right wheel clockwise
- }
- else if (irLeft == 1 && irRight == 1) { // if both receivers detect nothing
- servoRight.writeMicroseconds(1700); //reverse left wheel
- servoLeft.writeMicroseconds(1700); //reverse right wheel
- }
- }
- int irDetect (int irLedPin, int irReceiverPin, long frequency) {
- tone(irLedPin , frequency, 8);
- delay(1);
- int ir = digitalRead(irReceiverPin);
- delay(1);
- return ir;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement