|
| 1 | +// LCD Menu items |
| 2 | +char obstacle[16] = "1.Obst"; |
| 3 | +char circle[16] = "2.Circle"; |
| 4 | +char squ[16] = "3.Square"; |
| 5 | +char reset[16] = "4.Reset"; |
| 6 | + |
| 7 | +unsigned char ReadSwitch(unsigned char sw); |
| 8 | + |
| 9 | +// Declare variables |
| 10 | +int servoPort[2] = {SERVO_0, SERVO_1}; |
| 11 | +int waitState = 1000; |
| 12 | +int lServ = 2000; |
| 13 | +int rServ = 1000; |
| 14 | +int sensorPin = GPP_0; |
| 15 | +int cm; |
| 16 | +static int active = -1; // Check button pressed constant |
| 17 | + |
| 18 | +// Values for moving bot back or forward |
| 19 | +static int Lbck = 2500; |
| 20 | +static int Rbck = 1000; |
| 21 | +static int Lfwd = 1000; |
| 22 | +static int Rfwd = 2000; |
| 23 | + |
| 24 | + |
| 25 | +void setup() { |
| 26 | + |
| 27 | + // Initialise serial, servos, servos and IR sensors |
| 28 | + Serial.begin(9600); |
| 29 | + EmoroServo.attach(servoPort[0]); |
| 30 | + EmoroServo.attach(servoPort[1]); |
| 31 | + Ultrasonic.attach(sensorPin); |
| 32 | + |
| 33 | + pinMode(IO_0, INPUT_PULLUP); |
| 34 | + pinMode(IO_1, INPUT_PULLUP); |
| 35 | + |
| 36 | + // Call mainMenu function to print the main menu |
| 37 | + mainMenu(); |
| 38 | +} |
| 39 | + |
| 40 | +void loop() { |
| 41 | + // Read sensor pin distance in CM |
| 42 | + cm = Ultrasonic.read(sensorPin); |
| 43 | + if (ReadEmoroHardware() & SW_AVAILABLE) { |
| 44 | + // Check if switches are pressed, if pressed, set active to x |
| 45 | + if (ReadSwitch(SW_1) == 1) |
| 46 | + { |
| 47 | + active = 1; |
| 48 | + } |
| 49 | + else if (ReadSwitch(SW_2) == 1) |
| 50 | + { |
| 51 | + active = 2; |
| 52 | + } |
| 53 | + else if (ReadSwitch(SW_3) == 1) |
| 54 | + { |
| 55 | + active = 3; |
| 56 | + } |
| 57 | + else if (ReadSwitch(SW_4) == 1) |
| 58 | + { |
| 59 | + active = 4; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (active == 1) { |
| 64 | + Serial.println("Obst"); |
| 65 | + Lcd.clear(); |
| 66 | + Lcd.locate(0, 0); |
| 67 | + Lcd.print("Moving..."); |
| 68 | + moveBot(); |
| 69 | + |
| 70 | + // Debug |
| 71 | + Serial.print("Servo pulse width: "); |
| 72 | + Serial.print(EmoroServo.read(servoPort[0])); |
| 73 | + Serial.print(" us\n"); |
| 74 | + Serial.print("Servo pulse width: "); |
| 75 | + Serial.print(EmoroServo.read(servoPort[1])); |
| 76 | + Serial.print(" us\n"); |
| 77 | + Serial.println("Moving..."); |
| 78 | + |
| 79 | + // Calls the objDetected function that stops bot |
| 80 | + // Then moves away from the object |
| 81 | + objDetected(); |
| 82 | + } |
| 83 | + else if (active == 2) { |
| 84 | + Serial.println("Switch SW_2 is activated"); |
| 85 | + Lcd.clear(); |
| 86 | + Lcd.locate(0, 0); |
| 87 | + Lcd.print("Circle Track"); |
| 88 | + Lcd.locate(1, 0); |
| 89 | + Lcd.print("Following"); |
| 90 | + |
| 91 | + doCircle(); |
| 92 | + } |
| 93 | + else if (active == 3) { |
| 94 | + Serial.println("Switch SW_3 is activated"); |
| 95 | + Lcd.clear(); |
| 96 | + Lcd.locate(0, 0); |
| 97 | + Lcd.print("Square Track"); |
| 98 | + Lcd.locate(1, 0); |
| 99 | + Lcd.print("Following"); |
| 100 | + |
| 101 | + doSquare(); |
| 102 | + } |
| 103 | + else if (active == 4) { |
| 104 | + Serial.println("Robot Reset"); |
| 105 | + stopBot(); |
| 106 | + Lcd.locate(0, 0); |
| 107 | + Lcd.print("Resetting..."); |
| 108 | + Lcd.clear(); |
| 109 | + active = -1; |
| 110 | + |
| 111 | + mainMenu(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/********************* |
| 116 | + * EMORO FUNCTIONS * |
| 117 | + *********************/ |
| 118 | + |
| 119 | +// Function to enable return to main menu |
| 120 | +void mainMenu() |
| 121 | +{ |
| 122 | + if (ReadEmoroHardware() & LCD_AVAILABLE) { // if LCD and switch avaliable |
| 123 | + Serial.println("LCD Ready"); |
| 124 | + Lcd.locate(0, 0); |
| 125 | + Lcd.print(obstacle); |
| 126 | + Lcd.locate(0, 8); |
| 127 | + Lcd.print(circle); |
| 128 | + Lcd.locate(1, 0); |
| 129 | + Lcd.print(squ); |
| 130 | + Lcd.locate(1, 9); |
| 131 | + Lcd.print(reset); |
| 132 | + } else { |
| 133 | + Serial.println("LCD Not Ready"); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Function to stop the bot |
| 138 | +void stopBot() |
| 139 | +{ |
| 140 | + // Set servo pulse to 1500us. For position servo motors this will set the middle position, |
| 141 | + // for servo motors with continuous rotation this will stop rotation |
| 142 | + EmoroServo.write(servoPort[0], 1500); |
| 143 | + EmoroServo.write(servoPort[1], 1500); |
| 144 | +} |
| 145 | + |
| 146 | +// Function to move bot |
| 147 | +void moveBot() |
| 148 | +{ |
| 149 | + // Set servo pulse to 1000us. For position servo motors this will set the left position, |
| 150 | + // for servo motors with continuous rotation this will set continuous rotation in CCW direction |
| 151 | + EmoroServo.write(servoPort[0], lServ); |
| 152 | + EmoroServo.write(servoPort[1], rServ); |
| 153 | +} |
| 154 | + |
| 155 | +// Function to detect obstacles |
| 156 | +void objDetected() |
| 157 | +{ |
| 158 | + cm = Ultrasonic.read(sensorPin); |
| 159 | + // Stop robot if less than 25cm |
| 160 | + if (cm <= 25) |
| 161 | + { |
| 162 | + Serial.println("Object Detected!"); |
| 163 | + Lcd.clear(); |
| 164 | + Lcd.locate(2, 0); |
| 165 | + Lcd.print("Object Detected!"); |
| 166 | + |
| 167 | + // Calls stopBot function to stop bot if less than 25cm |
| 168 | + stopBot(); |
| 169 | + delay(85); |
| 170 | + // Moves the bot back |
| 171 | + EmoroServo.write(servoPort[0], 500); |
| 172 | + EmoroServo.write(servoPort[1], 2500); |
| 173 | + delay(200); |
| 174 | + // Turns bot |
| 175 | + EmoroServo.write(servoPort[0], 500); |
| 176 | + EmoroServo.write(servoPort[1], 500); |
| 177 | + delay(250); |
| 178 | + // Moves forward and away form the object |
| 179 | + EmoroServo.write(servoPort[0], lServ); |
| 180 | + EmoroServo.write(servoPort[1], rServ); |
| 181 | + //delay(100); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// Function for following circle |
| 186 | +void doCircle() |
| 187 | +{ |
| 188 | + // If Both sensors detect white, go forward. |
| 189 | + if (digitalRead(IO_0) == 1 && digitalRead(IO_1) == 1) { |
| 190 | + EmoroServo.write(servoPort[0], Lfwd); |
| 191 | + EmoroServo.write(servoPort[1], Rfwd); |
| 192 | + Lcd.locate(0, 0); |
| 193 | + Serial.print("White"); |
| 194 | + Lcd.print("White"); |
| 195 | + Lcd.locate(1, 0); |
| 196 | + Lcd.print(digitalRead(IO_0)); |
| 197 | + Lcd.locate(1, 2); |
| 198 | + Lcd.print(digitalRead(IO_1)); |
| 199 | + } |
| 200 | + // Only left sensor is on the black, turn right. |
| 201 | + else if (digitalRead(IO_0) == 1) { |
| 202 | + EmoroServo.write(SERVO_0, Lbck); |
| 203 | + EmoroServo.write(SERVO_1, Rfwd); |
| 204 | + Lcd.locate(0, 0); |
| 205 | + Lcd.print("Left Sensor"); |
| 206 | + Lcd.locate(1, 0); |
| 207 | + Lcd.print(digitalRead(IO_0)); |
| 208 | + Lcd.locate(1, 2); |
| 209 | + Lcd.print(digitalRead(IO_1)); |
| 210 | + delay(10); |
| 211 | + } |
| 212 | + // Only right sensor is on the black, turn left. |
| 213 | + else if (digitalRead(IO_1) == 1) { |
| 214 | + EmoroServo.write(SERVO_0, Lfwd); |
| 215 | + EmoroServo.write(SERVO_1, Rbck); |
| 216 | + Lcd.locate(0, 0); |
| 217 | + Lcd.print("Right Sensor"); |
| 218 | + Lcd.locate(1, 0); |
| 219 | + Lcd.print(digitalRead(IO_0)); |
| 220 | + Lcd.locate(1, 2); |
| 221 | + Lcd.print(digitalRead(IO_1)); |
| 222 | + delay(10); |
| 223 | + } |
| 224 | +} |
| 225 | +// Function for following square |
| 226 | +void doSquare() |
| 227 | +{ |
| 228 | + // If Both sensors detect white, go forward. |
| 229 | + if (digitalRead(IO_0) == 1 && digitalRead(IO_1) == 1) { |
| 230 | + EmoroServo.write(servoPort[0], Lfwd); |
| 231 | + EmoroServo.write(servoPort[1], Rfwd); |
| 232 | + Lcd.locate(0, 0); |
| 233 | + Serial.print("White"); |
| 234 | + Lcd.print("White"); |
| 235 | + Lcd.locate(1, 0); |
| 236 | + Lcd.print(digitalRead(IO_0)); |
| 237 | + Lcd.locate(1, 2); |
| 238 | + Lcd.print(digitalRead(IO_1)); |
| 239 | + } |
| 240 | + // Only left sensor is on the black, turn 90 deg right. |
| 241 | + else if (digitalRead(IO_0) == 1) { |
| 242 | + EmoroServo.write(SERVO_0, Lbck); |
| 243 | + EmoroServo.write(SERVO_1, Rfwd); |
| 244 | + delay(500); |
| 245 | + Lcd.locate(0, 0); |
| 246 | + Lcd.print("Left Sensor"); |
| 247 | + Lcd.locate(1, 0); |
| 248 | + Lcd.print(digitalRead(IO_0)); |
| 249 | + Lcd.locate(1, 2); |
| 250 | + Lcd.print(digitalRead(IO_1)); |
| 251 | + } |
| 252 | + // Only right sensor is on the black, turn 90 deg left. |
| 253 | + else if (digitalRead(IO_1) == 1) { |
| 254 | + EmoroServo.write(SERVO_0, Lfwd); |
| 255 | + EmoroServo.write(SERVO_1, Rbck); |
| 256 | + delay(500); |
| 257 | + Lcd.locate(0, 0); |
| 258 | + Lcd.print("Right Sensor"); |
| 259 | + Lcd.locate(1, 0); |
| 260 | + Lcd.print(digitalRead(IO_0)); |
| 261 | + Lcd.locate(1, 2); |
| 262 | + Lcd.print(digitalRead(IO_1)); |
| 263 | + } |
| 264 | +} |
0 commit comments