|
| 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 | + |
| 5 | +Shieldbot shieldbot = Shieldbot(); //decares a Shieldbot object |
| 6 | + |
| 7 | +#define ARR_LEN(x) (sizeof(x) / sizeof((x)[0])) |
| 8 | + |
| 9 | +//#define DBGTEAM4 |
| 10 | + |
| 11 | +void setup(){ |
| 12 | + Serial.begin(9600);//Begin serial comm for debugging |
| 13 | + shieldbot.setMaxSpeed(60,70);//255 is max, if one motor is faster than another, adjust values |
| 14 | +} |
| 15 | + |
| 16 | +void loop() |
| 17 | +{ |
| 18 | + int s[5]; |
| 19 | + |
| 20 | + s[4] = shieldbot.readS1(); |
| 21 | + s[3] = shieldbot.readS2(); |
| 22 | + s[2] = shieldbot.readS3(); |
| 23 | + s[1] = shieldbot.readS4(); |
| 24 | + s[0] = shieldbot.readS5(); |
| 25 | + |
| 26 | + #define ___ 255 // don't care |
| 27 | + #define ON_ HIGH // light |
| 28 | + #define OFF LOW // dark |
| 29 | + |
| 30 | + #ifdef DBGTEAM4 |
| 31 | + #define txt(x) ,x |
| 32 | + #else |
| 33 | + #define txt(x) |
| 34 | + #endif |
| 35 | + |
| 36 | + // Table that defines the behavior of the motors, based on the |
| 37 | + // sensors. The entries in the table either have to match the sensors |
| 38 | + // exactly, or they can |
| 39 | + const struct lookup_t |
| 40 | + { |
| 41 | + int s[5]; |
| 42 | + char lspeed, rspeed; |
| 43 | + #ifdef DBGTEAM4 |
| 44 | + const char *desc; |
| 45 | + #endif |
| 46 | + } lookups[] = |
| 47 | + { |
| 48 | + { { OFF, OFF, ON_, OFF, OFF }, 127, 127 txt("1 center") }, // 1 center light: straight ahead |
| 49 | + { { OFF, ON_, OFF, ON_, OFF }, 127, 127 txt("2 center") }, // 2 center lights: straight ahead |
| 50 | + { { OFF, ON_, ON_, ON_, OFF }, 127, 127 txt("3 center") }, // 3 center lights: straight ahead |
| 51 | + { { ON_, ON_, ON_, ON_, ON_ }, 0, 0 txt("all lit") }, // All lit: stop, we don't know where to go |
| 52 | + { { ON_, ON_, ON_, ___, OFF }, 0, 127 txt("3/4 left") }, // 3 or 4 left lights: soft left |
| 53 | + { { OFF, ___, ON_, ON_, ON_ }, 127, 0 txt("3/4 right") }, // 3 or 4 right lights: soft right |
| 54 | + { { OFF, ON_, ___, OFF, OFF }, 127, 0 txt("1/2 left") }, // 1 or 2 left lights: soft left |
| 55 | + { { OFF, OFF, ___, ON_, OFF }, 0, 127 txt("1/2 right") }, // 1 or 2 right lights: soft right |
| 56 | + { { ON_, ON_, OFF, OFF, OFF }, 0, 127 txt("2 left") }, // 2 left lights: soft left |
| 57 | + { { OFF, OFF, OFF, ON_, ON_ }, 127, 0 txt("2 right") }, // 2 right lights: soft right |
| 58 | + { { ON_, OFF, OFF, OFF, OFF }, -128, 127 txt("1 left") }, // 1 left light: hard left |
| 59 | + { { OFF, OFF, OFF, OFF, ON_ }, 127, -128 txt("1 right") }, // 1 right light: hard right |
| 60 | + }; |
| 61 | + |
| 62 | + int found; |
| 63 | + for (unsigned lut = 0; lut < ARR_LEN(lookups); lut++) |
| 64 | + { |
| 65 | + const struct lookup_t *plookup = &lookups[lut]; |
| 66 | + found = 1; |
| 67 | + |
| 68 | + for (unsigned index = 0; (found) && (index < 5); index++) |
| 69 | + { |
| 70 | + int wanted = plookup->s[index]; |
| 71 | + |
| 72 | + if ( (wanted != ___) && (s[index] != wanted) ) |
| 73 | + { |
| 74 | + found = 0; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (found) |
| 80 | + { |
| 81 | + #ifdef DBGTEAM4 |
| 82 | + Serial.println(plookup->desc); |
| 83 | + #endif |
| 84 | + shieldbot.drive(plookup->lspeed, plookup->rspeed); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!found) |
| 90 | + { |
| 91 | + #ifdef DBGTEAM4 |
| 92 | + Serial.println("???"); |
| 93 | + #endif |
| 94 | + shieldbot.drive(0, 0); |
| 95 | + delay(100); |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments