|
| 1 | +//Make sure the dip switches are turned ON, and none of your shields are using pins A0,A1,A2,A3 or D4 |
| 2 | + |
| 3 | +#include <Shieldbot.h> //includes the Shieldbot Library |
| 4 | +#include <Servo.h> |
| 5 | + |
| 6 | +Shieldbot shieldbot = Shieldbot(); //decares a Shieldbot object |
| 7 | + |
| 8 | +Servo myservo; |
| 9 | +int servopin = 12; |
| 10 | + |
| 11 | +int switch1pin = 11; |
| 12 | +int switch2pin = 12; |
| 13 | +int switch3pin = 13; |
| 14 | + |
| 15 | +int switch1state = 0; |
| 16 | +int switch2state = 0; |
| 17 | +int switch3state = 0; |
| 18 | + |
| 19 | +// Bot sensors |
| 20 | +int S1,S2,S3,S4,S5; //values to store state of sensors |
| 21 | + |
| 22 | +// Bot speed controls |
| 23 | +int fwdhi = 127; |
| 24 | +int bwdhi = -128; |
| 25 | +int fwdlo = 64; |
| 26 | +int bwdlo = -64; |
| 27 | +int fwdoff = 0; |
| 28 | + |
| 29 | +// servo position |
| 30 | +int pos = 0; |
| 31 | +int posMin = 0; |
| 32 | +int posMax = 180; |
| 33 | +int posSpeed = 5; |
| 34 | + |
| 35 | +// wait = 0, fill-up = 1-3, deliver = 4 |
| 36 | +int fillState = 0; |
| 37 | + |
| 38 | +void setup(){ |
| 39 | + Serial.begin(9600);//Begin serial comm for debugging |
| 40 | + shieldbot.setMaxSpeed(50,50);//255 is max, if one motor is faster than another, adjust values |
| 41 | +/* |
| 42 | + myservo.attach(servopin); |
| 43 | + myservo.write(posMin); |
| 44 | +*/ |
| 45 | + pinMode(switch1pin, INPUT); |
| 46 | + pinMode(switch2pin, INPUT); |
| 47 | + pinMode(switch3pin, INPUT); |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +void loop(){ |
| 52 | + /* |
| 53 | + debugloop(); |
| 54 | + driveBot(); |
| 55 | + */ |
| 56 | + |
| 57 | + switch (fillState) { |
| 58 | + case 0: |
| 59 | + // Waiting for button push, is it pushed? |
| 60 | + getSwitches(); |
| 61 | + if (switch1state == HIGH || switch2state == HIGH || switch3state == HIGH) { |
| 62 | + fillState = 1; |
| 63 | + shieldbot.drive(fwdhi,fwdhi); |
| 64 | + delay(200); // Get off of the line |
| 65 | + } |
| 66 | + break; |
| 67 | + default: |
| 68 | + // Fill or deliver |
| 69 | + driveBot(); |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +void debugloop() { |
| 76 | + //Read all the sensors |
| 77 | + S1 = shieldbot.readS1(); |
| 78 | + S2 = shieldbot.readS2(); |
| 79 | + S3 = shieldbot.readS3(); |
| 80 | + S4 = shieldbot.readS4(); |
| 81 | + S5 = shieldbot.readS5(); |
| 82 | + |
| 83 | + switch1state = digitalRead(switch1pin); |
| 84 | + switch2state = digitalRead(switch2pin); |
| 85 | + switch3state = digitalRead(switch3pin); |
| 86 | + |
| 87 | + //Print the status of each sensor to the Serial console |
| 88 | + Serial.print(" S1: "); |
| 89 | + Serial.print(S1); |
| 90 | + Serial.print(" S2: "); |
| 91 | + Serial.print(S2); |
| 92 | + Serial.print(" S3: "); |
| 93 | + Serial.print(S3); |
| 94 | + Serial.print(" S4: "); |
| 95 | + Serial.print(S4); |
| 96 | + Serial.print(" S5: "); |
| 97 | + Serial.print(S5); |
| 98 | + Serial.print(" sw1 "); |
| 99 | + Serial.print(switch1state); |
| 100 | + Serial.print(" sw2 "); |
| 101 | + Serial.print(switch2state); |
| 102 | + Serial.print(" sw3 "); |
| 103 | + Serial.print(switch3state); |
| 104 | + Serial.println(); |
| 105 | +/* |
| 106 | + for(pos = posMin; pos < posMax; pos += posSpeed) { |
| 107 | + analogWrite(servopin, pos); |
| 108 | + delay(15); |
| 109 | + } |
| 110 | +*/ |
| 111 | +} |
| 112 | + |
| 113 | +void getSwitches() { |
| 114 | + switch1state = digitalRead(switch1pin); |
| 115 | + Serial.print("sw1 "); |
| 116 | + Serial.print(switch1state); |
| 117 | + switch2state = digitalRead(switch2pin); |
| 118 | + Serial.print(" sw2 "); |
| 119 | + Serial.print(switch2state); |
| 120 | + switch3state = digitalRead(switch3pin); |
| 121 | + Serial.print(" sw3 "); |
| 122 | + Serial.print(switch3state); |
| 123 | + Serial.println(); |
| 124 | +} |
| 125 | + |
| 126 | +void driveBot() { |
| 127 | + |
| 128 | + //Read all the sensors |
| 129 | + S1 = shieldbot.readS1(); |
| 130 | + S2 = shieldbot.readS2(); |
| 131 | + S3 = shieldbot.readS3(); |
| 132 | + S4 = shieldbot.readS4(); |
| 133 | + S5 = shieldbot.readS5(); |
| 134 | + |
| 135 | + //Print the status of each sensor to the Serial console |
| 136 | + Serial.print(" S1: "); |
| 137 | + Serial.print(S1); |
| 138 | + Serial.print(" S2: "); |
| 139 | + Serial.print(S2); |
| 140 | + Serial.print(" S3: "); |
| 141 | + Serial.print(S3); |
| 142 | + Serial.print(" S4: "); |
| 143 | + Serial.print(S4); |
| 144 | + Serial.print(" S5: "); |
| 145 | + Serial.print(S5); |
| 146 | + Serial.print(" sw1 "); |
| 147 | + Serial.print(switch1state); |
| 148 | + Serial.print(" sw2 "); |
| 149 | + Serial.print(switch2state); |
| 150 | + Serial.print(" sw3 "); |
| 151 | + Serial.print(switch3state); |
| 152 | + Serial.print(" "); |
| 153 | + |
| 154 | + //Note about IR sensors |
| 155 | + //if a sensor sees HIGH, it means that it just sees a reflective surface background(ex. whie) |
| 156 | + //if a sensor sees LOW, it means that it sees a non-reflective surface or empty space (ex. black tape line, or empty space off ledge) |
| 157 | + |
| 158 | + if (S3 == LOW && S2 == LOW && S4 == LOW && S1 == LOW && S5 == LOW) { |
| 159 | + // get drink |
| 160 | + shieldbot.stop(); |
| 161 | + fillOrDeliver(); |
| 162 | + } else if (S3 == LOW && S4 == LOW && S2 == LOW) { |
| 163 | + // forward slower |
| 164 | + Serial.println("forward low"); |
| 165 | + shieldbot.drive(fwdlo,fwdlo); |
| 166 | + } else if (S3 == LOW && S2 == LOW) { |
| 167 | + // lean right |
| 168 | + Serial.println("lean right"); |
| 169 | + shieldbot.drive(fwdhi,fwdlo); |
| 170 | + } else if (S3 == LOW && S4 == LOW) { |
| 171 | + // lean left |
| 172 | + Serial.println("lean left"); |
| 173 | + shieldbot.drive(fwdlo,fwdhi); |
| 174 | + } else if (S3 == LOW) { |
| 175 | + // go foward |
| 176 | + Serial.println("forward"); |
| 177 | + shieldbot.drive(fwdhi,fwdhi); |
| 178 | + } else if (S2 == LOW) { |
| 179 | + // right low |
| 180 | + Serial.println("right low"); |
| 181 | + shieldbot.drive(fwdhi,fwdoff); |
| 182 | + delay(100); |
| 183 | + } else if (S4 == LOW) { |
| 184 | + // left low |
| 185 | + Serial.println("left low"); |
| 186 | + shieldbot.drive(fwdoff,fwdhi); |
| 187 | + delay(100); |
| 188 | + } else if (S1 == LOW) { |
| 189 | + // right hard |
| 190 | + Serial.println("left high"); |
| 191 | + shieldbot.drive(fwdhi,bwdhi); |
| 192 | + delay(100); |
| 193 | + } else if (S5 == LOW) { |
| 194 | + // left hard |
| 195 | + Serial.println("left high"); |
| 196 | + shieldbot.drive(bwdhi,fwdhi); |
| 197 | + delay(100); |
| 198 | + } else { |
| 199 | + // find the line |
| 200 | + Serial.println("find the line"); |
| 201 | + shieldbot.drive(fwdhi,fwdhi); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +int doTheFill = 0; |
| 206 | +void shouldFill() { |
| 207 | + doTheFill = 0; |
| 208 | + if (fillState == 1) { |
| 209 | + doTheFill = switch1state; |
| 210 | + } else if (fillState == 2) { |
| 211 | + doTheFill = switch2state; |
| 212 | + } else if (fillState == 3) { |
| 213 | + doTheFill = switch3state; |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +void fillOrDeliver() { |
| 218 | + if (fillState < 4) { |
| 219 | + // fill |
| 220 | + fillState++; |
| 221 | + |
| 222 | + shouldFill(); |
| 223 | + if (doTheFill == 1) { |
| 224 | + Serial.print("fill "); |
| 225 | + Serial.println(fillState); |
| 226 | + |
| 227 | + // push the fill button |
| 228 | + for(pos = posMin; pos < posMax; pos += posSpeed) { |
| 229 | + myservo.write(pos); |
| 230 | + delay(15); |
| 231 | + } |
| 232 | + delay(15); |
| 233 | + for(pos = posMax; pos>=posMin; pos-=posSpeed) { |
| 234 | + myservo.write(pos); |
| 235 | + delay(15); |
| 236 | + } |
| 237 | + delay(2500); // Wait for fill-up |
| 238 | + } else { |
| 239 | + Serial.print("no need to fill "); |
| 240 | + Serial.println(fillState); |
| 241 | + } |
| 242 | + |
| 243 | + shieldbot.drive(fwdhi,fwdhi); |
| 244 | + delay(100); // Get off of the line |
| 245 | + } else { |
| 246 | + // delivered |
| 247 | + fillState = 0; |
| 248 | + Serial.println("delivered"); |
| 249 | + } |
| 250 | +} |
| 251 | + |
0 commit comments