|
| 1 | +// Robotics with the BOE Shield - RoamingWithWhiskers |
| 2 | +// Go forward. Back up and turn if whiskers indicate BOE Shield bot bumped |
| 3 | +// into something. |
| 4 | + |
| 5 | +#include <Servo.h> // Include servo library |
| 6 | + |
| 7 | +#define NOTE_C4 262 |
| 8 | + |
| 9 | +Servo servoLeft; // Declare left and right servos |
| 10 | +Servo servoRight; |
| 11 | + |
| 12 | + |
| 13 | +static const int step_dur = 50; // In milliseconds |
| 14 | +static const int honk_dur = 1000 + 1000 + 250; // In milliseconds |
| 15 | + |
| 16 | + |
| 17 | +// static const uint8_t rght_whskr_pin = 7; |
| 18 | +// static const uint8_t left_whskr_pin = 5; |
| 19 | + |
| 20 | + |
| 21 | +static const uint8_t rght_servo_pin = 12; |
| 22 | +static const uint8_t left_servo_pin = 13; |
| 23 | +static const uint8_t speaker_pin = 9; |
| 24 | + |
| 25 | + |
| 26 | +static const uint8_t pin_shift_f_l = 8; |
| 27 | +static const uint8_t left_btn_pin = pin_shift_f_l - 1; |
| 28 | +static const uint8_t bkwd_btn_pin = pin_shift_f_l - 2; |
| 29 | +static const uint8_t frwd_btn_pin = pin_shift_f_l - 3; |
| 30 | +static const uint8_t rght_btn_pin = pin_shift_f_l - 4; |
| 31 | +static const uint8_t honk_btn_pin = pin_shift_f_l - 5; |
| 32 | + |
| 33 | + |
| 34 | +void honk(unsigned int n = 2); |
| 35 | + |
| 36 | + |
| 37 | +void setup() // Built-in initialization block |
| 38 | +{ |
| 39 | + Serial.begin(9600); |
| 40 | + |
| 41 | + // Set up input from receiver: |
| 42 | + pinMode(left_btn_pin, INPUT_PULLUP); |
| 43 | + pinMode(bkwd_btn_pin, INPUT_PULLUP); |
| 44 | + pinMode(frwd_btn_pin, INPUT_PULLUP); |
| 45 | + pinMode(rght_btn_pin, INPUT_PULLUP); |
| 46 | + pinMode(honk_btn_pin, INPUT_PULLUP); |
| 47 | + |
| 48 | + servoLeft.attach(left_servo_pin); // Attach left signal to pin 13 |
| 49 | + servoRight.attach(rght_servo_pin); // Attach right signal to pin 12 |
| 50 | + |
| 51 | + // Set up speaker |
| 52 | + pinMode(speaker_pin, OUTPUT); |
| 53 | + noTone(speaker_pin); |
| 54 | + honk(1); |
| 55 | +} |
| 56 | + |
| 57 | +void loop() // Main loop auto-repeats |
| 58 | +{ |
| 59 | + // byte wLeft = digitalRead(5); // Copy left result to wLeft |
| 60 | + // byte wRight = digitalRead(7); // Copy right result to wRight |
| 61 | + |
| 62 | + if (digitalRead(honk_btn_pin) == LOW) { |
| 63 | + halt(); |
| 64 | + honk(2); |
| 65 | + // delay(step_dur); |
| 66 | + } else if (digitalRead(frwd_btn_pin) == LOW) { |
| 67 | + while (digitalRead(frwd_btn_pin) == LOW) forward(step_dur); |
| 68 | + halt(); |
| 69 | + } else if (digitalRead(bkwd_btn_pin) == LOW) { |
| 70 | + while (digitalRead(bkwd_btn_pin) == LOW) backward(step_dur); |
| 71 | + halt(); |
| 72 | + } else if (digitalRead(left_btn_pin) == LOW) { |
| 73 | + while (digitalRead(left_btn_pin) == LOW) turnLeft(step_dur); |
| 74 | + halt(); |
| 75 | + } else if (digitalRead(rght_btn_pin) == LOW) { |
| 76 | + while (digitalRead(rght_btn_pin) == LOW) turnRight(step_dur); |
| 77 | + halt(); |
| 78 | + } |
| 79 | + |
| 80 | + // if((wLeft == 0) && (wRight == 0)) // If both whiskers contact |
| 81 | + // { |
| 82 | + // backward(1000); // Back up 1 second |
| 83 | + // turnLeft(800); // Turn left about 120 degrees |
| 84 | + // } |
| 85 | + // else if(wLeft == 0) // If only left whisker contact |
| 86 | + // { |
| 87 | + // backward(1000); // Back up 1 second |
| 88 | + // turnRight(400); // Turn right about 60 degrees |
| 89 | + // } |
| 90 | + // else if(wRight == 0) // If only right whisker contact |
| 91 | + // { |
| 92 | + // backward(1000); // Back up 1 second |
| 93 | + // turnLeft(400); // Turn left about 60 degrees |
| 94 | + // } |
| 95 | + // else // Otherwise, no whisker contact |
| 96 | + // { |
| 97 | + // forward(20); // Forward 1/50 of a second |
| 98 | + // } |
| 99 | +} |
| 100 | + |
| 101 | +void halt() // Halt function |
| 102 | +{ |
| 103 | + servoLeft.writeMicroseconds(1500); // Left wheel counterclockwise |
| 104 | + servoRight.writeMicroseconds(1500); // Right wheel clockwise |
| 105 | +} |
| 106 | + |
| 107 | +void forward(int time) // Forward function |
| 108 | +{ |
| 109 | + servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise |
| 110 | + servoRight.writeMicroseconds(1300); // Right wheel clockwise |
| 111 | + delay(time); // Maneuver for time ms |
| 112 | +} |
| 113 | + |
| 114 | +void turnLeft(int time) // Left turn function |
| 115 | +{ |
| 116 | + servoLeft.writeMicroseconds(1300); // Left wheel clockwise |
| 117 | + servoRight.writeMicroseconds(1300); // Right wheel clockwise |
| 118 | + delay(time); // Maneuver for time ms |
| 119 | +} |
| 120 | + |
| 121 | +void turnRight(int time) // Right turn function |
| 122 | +{ |
| 123 | + servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise |
| 124 | + servoRight.writeMicroseconds(1700); // Right wheel counterclockwise |
| 125 | + delay(time); // Maneuver for time ms |
| 126 | +} |
| 127 | + |
| 128 | +void backward(int time) // Backward function |
| 129 | +{ |
| 130 | + servoLeft.writeMicroseconds(1300); // Left wheel clockwise |
| 131 | + servoRight.writeMicroseconds(1700); // Right wheel counterclockwise |
| 132 | + delay(time); // Maneuver for time ms |
| 133 | +} |
| 134 | + |
| 135 | +void honk(unsigned int n) { |
| 136 | + for (int i = 0; i < n; i++) { |
| 137 | + tone(speaker_pin, NOTE_C4, 1000); // Play tone for 1 second |
| 138 | + delay(1000); // Delay to finish tone |
| 139 | + noTone(speaker_pin); |
| 140 | + if (i+1 < n) delay(250); // Only delay between honks |
| 141 | + } |
| 142 | +} |
0 commit comments