|
| 1 | +#include <ZumoBuzzer.h> |
| 2 | +#include <ZumoMotors.h> |
| 3 | +#include <Pushbutton.h> |
| 4 | +#include <QTRSensors.h> |
| 5 | +#include <ZumoReflectanceSensorArray.h> |
| 6 | + |
| 7 | +#define LED 13 |
| 8 | + |
| 9 | +// this might need to be tuned for different lighting conditions, surfaces, etc. |
| 10 | +#define QTR_THRESHOLD 1500 // microseconds |
| 11 | +#define DISTANCE_THRESHOLD 400 // A0 DAC |
| 12 | + |
| 13 | +// these might need to be tuned for different motor types |
| 14 | +#define REVERSE_SPEED 200 // 0 is stopped, 400 is full speed |
| 15 | +#define TURN_SPEED 200 |
| 16 | +#define FORWARD_SPEED 200 |
| 17 | +#define REVERSE_DURATION 200 // ms |
| 18 | +#define TURN_DURATION 300 // ms |
| 19 | +#define SEARCH_DURATION 100 // ms |
| 20 | +#define CHARGE_DURATION 300 // ms |
| 21 | + |
| 22 | +ZumoBuzzer buzzer; |
| 23 | +ZumoMotors motors; |
| 24 | +Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12 |
| 25 | + |
| 26 | +#define NUM_SENSORS 6 |
| 27 | +unsigned int sensor_values[NUM_SENSORS]; |
| 28 | + |
| 29 | +ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN); |
| 30 | + |
| 31 | +void waitForButtonAndCountDown() |
| 32 | +{ |
| 33 | + digitalWrite(LED, HIGH); |
| 34 | + button.waitForButton(); |
| 35 | + digitalWrite(LED, LOW); |
| 36 | + |
| 37 | + // play audible countdown |
| 38 | + for (int i = 0; i < 3; i++) |
| 39 | + { |
| 40 | + delay(1000); |
| 41 | + buzzer.playNote(NOTE_G(3), 200, 15); |
| 42 | + } |
| 43 | + delay(1000); |
| 44 | + buzzer.playNote(NOTE_G(4), 500, 15); |
| 45 | + delay(1000); |
| 46 | +} |
| 47 | + |
| 48 | +void forward() |
| 49 | +{ |
| 50 | + motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED); |
| 51 | +} |
| 52 | + |
| 53 | +void reverse() |
| 54 | +{ |
| 55 | + motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED); |
| 56 | + delay(REVERSE_DURATION); |
| 57 | +} |
| 58 | + |
| 59 | +void charge() |
| 60 | +{ |
| 61 | + forward(); |
| 62 | + //delay(CHARGE_DURATION); |
| 63 | +} |
| 64 | + |
| 65 | +void setup() |
| 66 | +{ |
| 67 | + // uncomment if necessary to correct motor directions |
| 68 | + //motors.flipLeftMotor(true); |
| 69 | + //motors.flipRightMotor(true); |
| 70 | + |
| 71 | + pinMode(LED, HIGH); |
| 72 | + |
| 73 | + Serial.begin(9600); |
| 74 | + |
| 75 | + waitForButtonAndCountDown(); |
| 76 | +} |
| 77 | + |
| 78 | +void loop() |
| 79 | +{ |
| 80 | + if (button.isPressed()) { |
| 81 | + // if button is pressed, stop and wait for another press to go again |
| 82 | + motors.setSpeeds(0, 0); |
| 83 | + button.waitForRelease(); |
| 84 | + waitForButtonAndCountDown(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +// motors.setSpeeds(-TURN_SPEED, TURN_SPEED); |
| 89 | + |
| 90 | + int distanceSensorValue = analogRead(A0); |
| 91 | + sensors.read(sensor_values); |
| 92 | + |
| 93 | + Serial.print("Left Edge Sensor: "); |
| 94 | + Serial.print(sensor_values[0]); |
| 95 | + Serial.print(" Distance Sensor: "); |
| 96 | + Serial.print(distanceSensorValue); |
| 97 | + Serial.print(" Right Edge Sensor: "); |
| 98 | + Serial.print(sensor_values[5]); |
| 99 | + Serial.println(""); |
| 100 | + |
| 101 | + if (sensor_values[0] < QTR_THRESHOLD) { |
| 102 | + // if leftmost sensor detects line, reverse and turn to the right |
| 103 | + reverse(); |
| 104 | + |
| 105 | + motors.setSpeeds(TURN_SPEED, -TURN_SPEED); |
| 106 | + delay(TURN_DURATION); |
| 107 | + |
| 108 | + // otherwise, go straight |
| 109 | + forward(); |
| 110 | + } else if (sensor_values[5] < QTR_THRESHOLD) { |
| 111 | + // if rightmost sensor detects line, reverse and turn to the left |
| 112 | + reverse(); |
| 113 | + |
| 114 | + motors.setSpeeds(-TURN_SPEED, TURN_SPEED); |
| 115 | + delay(TURN_DURATION); |
| 116 | + |
| 117 | + // otherwise, go straight |
| 118 | + forward(); |
| 119 | + } else if (distanceSensorValue > 120) { |
| 120 | + // if opponent is in range, charge |
| 121 | + charge(); |
| 122 | + } else { |
| 123 | + motors.setSpeeds(-TURN_SPEED, TURN_SPEED); |
| 124 | + delay(SEARCH_DURATION); |
| 125 | + } |
| 126 | +} |
| 127 | + |
0 commit comments