xszdx
Dependencies: mbed TCS3472_I2C MCP23017 VL6180 DebounceIn WattBob_TextLCD
main_copy.cpp@2:7c34106159c8, 2020-03-15 (annotated)
- Committer:
- maazshaikh
- Date:
- Sun Mar 15 14:35:32 2020 +0000
- Revision:
- 2:7c34106159c8
- Parent:
- 1:ad7134c4e365
jj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sephora12680 | 0:171d347f33f7 | 1 | #include "mbed.h" |
sephora12680 | 0:171d347f33f7 | 2 | #include "DebounceIn.h" |
sephora12680 | 0:171d347f33f7 | 3 | #include "stdint.h" |
sephora12680 | 0:171d347f33f7 | 4 | #include "MCP23017.h" |
sephora12680 | 0:171d347f33f7 | 5 | #include "WattBob_TextLCD.h" |
sephora12680 | 0:171d347f33f7 | 6 | #include "VL6180.h" |
sephora12680 | 1:ad7134c4e365 | 7 | #include "TCS3472_I2C.h" |
sephora12680 | 0:171d347f33f7 | 8 | |
sephora12680 | 0:171d347f33f7 | 9 | #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) |
sephora12680 | 0:171d347f33f7 | 10 | #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) |
sephora12680 | 0:171d347f33f7 | 11 | |
sephora12680 | 0:171d347f33f7 | 12 | MCP23017 *par_port; |
sephora12680 | 0:171d347f33f7 | 13 | WattBob_TextLCD *lcd; |
sephora12680 | 0:171d347f33f7 | 14 | |
sephora12680 | 1:ad7134c4e365 | 15 | #define IDENTIFICATIONMODEL_ID 0x0000 |
sephora12680 | 0:171d347f33f7 | 16 | |
sephora12680 | 1:ad7134c4e365 | 17 | Serial PC(USBTX, USBRX); //default 9600 baud |
sephora12680 | 1:ad7134c4e365 | 18 | VL6180 TOF_sensor(p28, p27); |
sephora12680 | 1:ad7134c4e365 | 19 | TCS3472_I2C rgb_sensor(p9, p10); |
sephora12680 | 0:171d347f33f7 | 20 | |
maazshaikh | 2:7c34106159c8 | 21 | DigitalOut servo11 (p8); DigitalOut servo12 (p7); |
maazshaikh | 2:7c34106159c8 | 22 | DigitalOut servo13 (p6); DigitalOut servo14 (p5); |
maazshaikh | 2:7c34106159c8 | 23 | |
maazshaikh | 2:7c34106159c8 | 24 | DigitalOut servo21 (p29); DigitalOut servo22 (p23); |
maazshaikh | 2:7c34106159c8 | 25 | DigitalOut servo23 (p22); DigitalOut servo24 (p21); |
maazshaikh | 2:7c34106159c8 | 26 | |
maazshaikh | 2:7c34106159c8 | 27 | DigitalOut servo31 (p26); DigitalOut servo32 (p25); |
maazshaikh | 2:7c34106159c8 | 28 | DigitalOut servo33 (p12); DigitalOut servo34 (p11); |
maazshaikh | 2:7c34106159c8 | 29 | |
sephora12680 | 1:ad7134c4e365 | 30 | DigitalOut myled1 (p13); DigitalOut myled2 (p14); |
sephora12680 | 1:ad7134c4e365 | 31 | DigitalOut myled3 (p15); DigitalOut myled4 (p16); |
sephora12680 | 1:ad7134c4e365 | 32 | |
sephora12680 | 1:ad7134c4e365 | 33 | DebounceIn pb1(p17); DebounceIn pb2(p18); |
sephora12680 | 1:ad7134c4e365 | 34 | DebounceIn pb3(p19); DebounceIn pb4(p20); |
sephora12680 | 0:171d347f33f7 | 35 | |
sephora12680 | 0:171d347f33f7 | 36 | int old_pb1 = 0, old_pb2 = 0, old_pb3 = 0, old_pb4 = 0; |
sephora12680 | 1:ad7134c4e365 | 37 | int new_pb1, new_pb2, new_pb3, new_pb4; |
sephora12680 | 0:171d347f33f7 | 38 | |
sephora12680 | 1:ad7134c4e365 | 39 | int i = 0, col, row, led; //variables for order manipulation |
sephora12680 | 1:ad7134c4e365 | 40 | int levelreset, tries, level = 1; //level parameters |
sephora12680 | 1:ad7134c4e365 | 41 | float delay; //delay for blinking LEDs |
sephora12680 | 1:ad7134c4e365 | 42 | int D = 30; //for distance sensor |
sephora12680 | 0:171d347f33f7 | 43 | |
sephora12680 | 1:ad7134c4e365 | 44 | int order[24][4] = //ORDER ARRAY FOR LED GAME |
sephora12680 | 1:ad7134c4e365 | 45 | {1,2,3,4, 2,1,3,4, 3,1,2,4, 1,3,2,4, 2,3,1,4, 3,2,1,4, |
sephora12680 | 1:ad7134c4e365 | 46 | 4,2,1,3, 2,4,1,3, 1,4,2,3, 4,1,2,3, 2,1,4,3, 1,2,4,3, |
sephora12680 | 1:ad7134c4e365 | 47 | 1,3,4,2, 3,1,4,2, 4,1,3,2, 1,4,3,2, 3,4,1,2, 4,3,1,2, |
sephora12680 | 1:ad7134c4e365 | 48 | 4,3,2,1, 3,4,2,1, 2,4,3,1, 4,2,3,1, 3,2,4,1, 2,3,4,1}; |
sephora12680 | 0:171d347f33f7 | 49 | |
sephora12680 | 1:ad7134c4e365 | 50 | void LEDgame(); //function prototype of LED game |
sephora12680 | 1:ad7134c4e365 | 51 | void reset(); //to reset level |
sephora12680 | 1:ad7134c4e365 | 52 | void light(int); //turn on LEDs |
maazshaikh | 2:7c34106159c8 | 53 | void Maint(); // maintainence mode |
sephora12680 | 1:ad7134c4e365 | 54 | int Dist(); //check distance sensor |
sephora12680 | 1:ad7134c4e365 | 55 | int MAX(int,int,int); //find max rgb value |
sephora12680 | 0:171d347f33f7 | 56 | |
sephora12680 | 0:171d347f33f7 | 57 | int main() |
maazshaikh | 2:7c34106159c8 | 58 | |
sephora12680 | 0:171d347f33f7 | 59 | { |
sephora12680 | 1:ad7134c4e365 | 60 | pb1.mode(PullUp); pb2.mode(PullUp); |
sephora12680 | 1:ad7134c4e365 | 61 | pb3.mode(PullUp); pb4.mode(PullUp); |
sephora12680 | 0:171d347f33f7 | 62 | wait(.001); |
sephora12680 | 1:ad7134c4e365 | 63 | |
sephora12680 | 0:171d347f33f7 | 64 | par_port = new MCP23017(p9, p10, 0x40); |
sephora12680 | 1:ad7134c4e365 | 65 | par_port->config(0x0F00, 0x0F00, 0x0F00); //configure MCP23017 chip on WattBob |
sephora12680 | 0:171d347f33f7 | 66 | lcd = new WattBob_TextLCD(par_port); |
sephora12680 | 0:171d347f33f7 | 67 | BACK_LIGHT_ON(par_port); |
sephora12680 | 1:ad7134c4e365 | 68 | TOF_sensor.VL6180_Init(); |
sephora12680 | 1:ad7134c4e365 | 69 | rgb_sensor.enablePowerAndRGBC(); |
sephora12680 | 1:ad7134c4e365 | 70 | rgb_sensor.setIntegrationTime(100); |
sephora12680 | 1:ad7134c4e365 | 71 | int rgb_read[4], maxrgb; |
maazshaikh | 2:7c34106159c8 | 72 | while (1){ |
maazshaikh | 2:7c34106159c8 | 73 | char c = PC.getc(); |
maazshaikh | 2:7c34106159c8 | 74 | if (c == 'o'){ |
sephora12680 | 0:171d347f33f7 | 75 | while(1) |
sephora12680 | 0:171d347f33f7 | 76 | { |
sephora12680 | 1:ad7134c4e365 | 77 | rgb_sensor.getAllColors(rgb_read); |
sephora12680 | 1:ad7134c4e365 | 78 | maxrgb = MAX(rgb_read[1],rgb_read[2],rgb_read[3]); |
sephora12680 | 1:ad7134c4e365 | 79 | if (maxrgb == rgb_read[1]) |
sephora12680 | 1:ad7134c4e365 | 80 | //if (maxrgb > 2000 && maxrgb < 3000) |
sephora12680 | 1:ad7134c4e365 | 81 | while(level <= 4 && Dist() < D) |
sephora12680 | 0:171d347f33f7 | 82 | { |
sephora12680 | 1:ad7134c4e365 | 83 | if (level == 0) break; //to stop game if level reset to zero. |
sephora12680 | 1:ad7134c4e365 | 84 | row = rand()%24; |
sephora12680 | 1:ad7134c4e365 | 85 | tries = 1; |
sephora12680 | 1:ad7134c4e365 | 86 | LEDgame(); |
sephora12680 | 0:171d347f33f7 | 87 | } |
maazshaikh | 2:7c34106159c8 | 88 | if (Dist() > D) { |
maazshaikh | 2:7c34106159c8 | 89 | level = 1; |
maazshaikh | 2:7c34106159c8 | 90 | myled1=0; myled2=0; myled3=0; myled4=0; |
maazshaikh | 2:7c34106159c8 | 91 | } //resets game if card is removed. |
sephora12680 | 0:171d347f33f7 | 92 | } |
sephora12680 | 0:171d347f33f7 | 93 | } |
sephora12680 | 0:171d347f33f7 | 94 | |
maazshaikh | 2:7c34106159c8 | 95 | else if (c== 'm'){ |
maazshaikh | 2:7c34106159c8 | 96 | while (1){ |
maazshaikh | 2:7c34106159c8 | 97 | char d = PC.getc(); |
maazshaikh | 2:7c34106159c8 | 98 | if (d=='D') {PC.printf("%d", Dist());} |
maazshaikh | 2:7c34106159c8 | 99 | if (d=='C'){ |
maazshaikh | 2:7c34106159c8 | 100 | rgb_sensor.getAllColors(rgb_read); |
maazshaikh | 2:7c34106159c8 | 101 | maxrgb = MAX(rgb_read[1],rgb_read[2],rgb_read[3]); |
maazshaikh | 2:7c34106159c8 | 102 | if (maxrgb == rgb_read[1]) PC.puts("Red"); |
maazshaikh | 2:7c34106159c8 | 103 | else if (maxrgb == rgb_read[2]) PC.puts("Green"); |
maazshaikh | 2:7c34106159c8 | 104 | else if (maxrgb == rgb_read[3]) PC.puts("Blue"); |
maazshaikh | 2:7c34106159c8 | 105 | } |
maazshaikh | 2:7c34106159c8 | 106 | else if (d=='r') myled1=1; |
maazshaikh | 2:7c34106159c8 | 107 | else if (d=='y') myled2=1; |
maazshaikh | 2:7c34106159c8 | 108 | else if (d=='g') myled3=1; |
maazshaikh | 2:7c34106159c8 | 109 | else if (d=='w') myled4=1; |
maazshaikh | 2:7c34106159c8 | 110 | else if (d=='s') {LEDgame();} |
maazshaikh | 2:7c34106159c8 | 111 | else if (d=='d') {myled1=0; myled2=0; myled3=0; myled4=0; } |
maazshaikh | 2:7c34106159c8 | 112 | else if (d=='o'){ |
maazshaikh | 2:7c34106159c8 | 113 | while(1){ |
maazshaikh | 2:7c34106159c8 | 114 | char p=PC.getc(); |
maazshaikh | 2:7c34106159c8 | 115 | if (p=='f') servo11=1; |
maazshaikh | 2:7c34106159c8 | 116 | else if (p=='s') servo12=1; |
maazshaikh | 2:7c34106159c8 | 117 | else if (p=='t') servo13=1; |
maazshaikh | 2:7c34106159c8 | 118 | else if (p=='F') servo14=1; |
maazshaikh | 2:7c34106159c8 | 119 | else if (p=='R'){ |
maazshaikh | 2:7c34106159c8 | 120 | servo11=0; servo12=0; servo13=0; servo14=0; |
maazshaikh | 2:7c34106159c8 | 121 | break; |
maazshaikh | 2:7c34106159c8 | 122 | } |
maazshaikh | 2:7c34106159c8 | 123 | } |
maazshaikh | 2:7c34106159c8 | 124 | } |
maazshaikh | 2:7c34106159c8 | 125 | else if (d=='S'){ |
maazshaikh | 2:7c34106159c8 | 126 | while(1){ |
maazshaikh | 2:7c34106159c8 | 127 | char p=PC.getc(); |
maazshaikh | 2:7c34106159c8 | 128 | if (p=='f') servo21=1; |
maazshaikh | 2:7c34106159c8 | 129 | else if (p=='s') servo22=1; |
maazshaikh | 2:7c34106159c8 | 130 | else if (p=='t') servo23=1; |
maazshaikh | 2:7c34106159c8 | 131 | else if (p=='F') servo24=1; |
maazshaikh | 2:7c34106159c8 | 132 | else if (p=='R'){ |
maazshaikh | 2:7c34106159c8 | 133 | servo21=0; servo22=0; servo23=0; servo24=0; |
maazshaikh | 2:7c34106159c8 | 134 | break; |
maazshaikh | 2:7c34106159c8 | 135 | } |
maazshaikh | 2:7c34106159c8 | 136 | } |
maazshaikh | 2:7c34106159c8 | 137 | } |
maazshaikh | 2:7c34106159c8 | 138 | else if (d=='t'){ |
maazshaikh | 2:7c34106159c8 | 139 | while(1){ |
maazshaikh | 2:7c34106159c8 | 140 | char p=PC.getc(); |
maazshaikh | 2:7c34106159c8 | 141 | if (p=='f') servo31=1; |
maazshaikh | 2:7c34106159c8 | 142 | else if (p=='s') servo32=1; |
maazshaikh | 2:7c34106159c8 | 143 | else if (p=='t') servo33=1; |
maazshaikh | 2:7c34106159c8 | 144 | else if (p=='F') servo34=1; |
maazshaikh | 2:7c34106159c8 | 145 | else if (p=='R'){ |
maazshaikh | 2:7c34106159c8 | 146 | servo31=0; servo32=0; servo33=0; servo34=0; |
maazshaikh | 2:7c34106159c8 | 147 | break; |
maazshaikh | 2:7c34106159c8 | 148 | } |
maazshaikh | 2:7c34106159c8 | 149 | } |
maazshaikh | 2:7c34106159c8 | 150 | } |
maazshaikh | 2:7c34106159c8 | 151 | |
maazshaikh | 2:7c34106159c8 | 152 | |
maazshaikh | 2:7c34106159c8 | 153 | else if (d=='R') {break;} |
maazshaikh | 2:7c34106159c8 | 154 | |
maazshaikh | 2:7c34106159c8 | 155 | |
maazshaikh | 2:7c34106159c8 | 156 | |
maazshaikh | 2:7c34106159c8 | 157 | } |
maazshaikh | 2:7c34106159c8 | 158 | } |
maazshaikh | 2:7c34106159c8 | 159 | } |
maazshaikh | 2:7c34106159c8 | 160 | } |
sephora12680 | 0:171d347f33f7 | 161 | |
sephora12680 | 1:ad7134c4e365 | 162 | void LEDgame() |
sephora12680 | 0:171d347f33f7 | 163 | { |
sephora12680 | 1:ad7134c4e365 | 164 | while(Dist() < D) |
sephora12680 | 0:171d347f33f7 | 165 | { |
sephora12680 | 0:171d347f33f7 | 166 | levelreset = 0; |
sephora12680 | 1:ad7134c4e365 | 167 | for (col=0; col<4; col++) |
sephora12680 | 1:ad7134c4e365 | 168 | {led = order[row][col]; light(led);} //switches on the respective LEDs according to the order |
sephora12680 | 0:171d347f33f7 | 169 | |
sephora12680 | 1:ad7134c4e365 | 170 | while(Dist() < D) |
sephora12680 | 0:171d347f33f7 | 171 | { |
sephora12680 | 0:171d347f33f7 | 172 | new_pb1 = pb1; new_pb2 = pb2; |
sephora12680 | 0:171d347f33f7 | 173 | new_pb3 = pb3; new_pb4 = pb4; |
sephora12680 | 0:171d347f33f7 | 174 | |
sephora12680 | 0:171d347f33f7 | 175 | if( ((new_pb1 == 0) && (old_pb1 == 1)) || ((new_pb2 == 0) && (old_pb2 == 1)) || ((new_pb3 == 0) && (old_pb3 == 1)) || ((new_pb4 == 0) && (old_pb4 == 1)) ) |
sephora12680 | 0:171d347f33f7 | 176 | { |
sephora12680 | 0:171d347f33f7 | 177 | switch(order[row][i]) |
sephora12680 | 0:171d347f33f7 | 178 | { |
sephora12680 | 0:171d347f33f7 | 179 | case 1: |
sephora12680 | 0:171d347f33f7 | 180 | { |
sephora12680 | 0:171d347f33f7 | 181 | if((new_pb1 == 0) && (old_pb1 == 1)) |
sephora12680 | 1:ad7134c4e365 | 182 | { myled1 = 1; i++;} |
sephora12680 | 0:171d347f33f7 | 183 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 184 | } break; |
sephora12680 | 0:171d347f33f7 | 185 | case 2: |
sephora12680 | 0:171d347f33f7 | 186 | { |
sephora12680 | 0:171d347f33f7 | 187 | if((new_pb2 == 0) && (old_pb2 == 1)) |
sephora12680 | 1:ad7134c4e365 | 188 | { myled2 = 1; i++;} |
sephora12680 | 0:171d347f33f7 | 189 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 190 | } break; |
sephora12680 | 0:171d347f33f7 | 191 | case 3: |
sephora12680 | 0:171d347f33f7 | 192 | { |
sephora12680 | 0:171d347f33f7 | 193 | if((new_pb3 == 0) && (old_pb3 == 1)) |
sephora12680 | 1:ad7134c4e365 | 194 | { myled3 = 1; i++;} |
sephora12680 | 0:171d347f33f7 | 195 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 196 | } break; |
sephora12680 | 0:171d347f33f7 | 197 | case 4: |
sephora12680 | 0:171d347f33f7 | 198 | { |
sephora12680 | 0:171d347f33f7 | 199 | if((new_pb4 == 0) && (old_pb4 == 1)) |
sephora12680 | 1:ad7134c4e365 | 200 | { myled4 = 1; i++;} |
sephora12680 | 0:171d347f33f7 | 201 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 202 | } break; |
sephora12680 | 0:171d347f33f7 | 203 | } |
sephora12680 | 1:ad7134c4e365 | 204 | if (levelreset == 1) //do if the level was reset during the game |
sephora12680 | 0:171d347f33f7 | 205 | { |
sephora12680 | 1:ad7134c4e365 | 206 | //reset level to zero to stop game if number of tries exceeded |
sephora12680 | 1:ad7134c4e365 | 207 | if (tries > 3) {level = 0; reset(); return;} |
sephora12680 | 0:171d347f33f7 | 208 | else break; |
sephora12680 | 0:171d347f33f7 | 209 | } |
sephora12680 | 1:ad7134c4e365 | 210 | else if (levelreset == 0) //else increment level counter |
sephora12680 | 1:ad7134c4e365 | 211 | if(i >= 4){level++; wait(2); reset(); return;} |
sephora12680 | 0:171d347f33f7 | 212 | } |
sephora12680 | 0:171d347f33f7 | 213 | old_pb1 = new_pb1; old_pb2 = new_pb2; |
sephora12680 | 0:171d347f33f7 | 214 | old_pb3 = new_pb3; old_pb4 = new_pb4; |
sephora12680 | 0:171d347f33f7 | 215 | } |
sephora12680 | 0:171d347f33f7 | 216 | } |
sephora12680 | 0:171d347f33f7 | 217 | } |
sephora12680 | 0:171d347f33f7 | 218 | |
sephora12680 | 0:171d347f33f7 | 219 | |
sephora12680 | 0:171d347f33f7 | 220 | |
sephora12680 | 0:171d347f33f7 | 221 | void reset() //function to turn off LEDs and reset order |
sephora12680 | 0:171d347f33f7 | 222 | { |
sephora12680 | 0:171d347f33f7 | 223 | myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0; |
sephora12680 | 0:171d347f33f7 | 224 | i = 0; wait(1); return; |
sephora12680 | 0:171d347f33f7 | 225 | } |
sephora12680 | 0:171d347f33f7 | 226 | |
sephora12680 | 1:ad7134c4e365 | 227 | void light(int n) //to turn on LEDs |
sephora12680 | 0:171d347f33f7 | 228 | { |
sephora12680 | 1:ad7134c4e365 | 229 | //sets delay based on level number |
sephora12680 | 1:ad7134c4e365 | 230 | if(level == 1) delay = 1; |
sephora12680 | 1:ad7134c4e365 | 231 | else if (level == 2) delay = 0.75; |
sephora12680 | 1:ad7134c4e365 | 232 | else if (level == 3) delay = 0.50; |
sephora12680 | 1:ad7134c4e365 | 233 | else if (level == 4) delay = 0.25; |
sephora12680 | 0:171d347f33f7 | 234 | |
sephora12680 | 1:ad7134c4e365 | 235 | //turns LED on for delay seconds before turning off |
sephora12680 | 1:ad7134c4e365 | 236 | if(n == 1) {myled1 = 1; wait(delay); myled1 = 0;} |
sephora12680 | 1:ad7134c4e365 | 237 | else if(n == 2) {myled2 = 1; wait(delay); myled2 = 0;} |
sephora12680 | 1:ad7134c4e365 | 238 | else if(n == 3) {myled3 = 1; wait(delay); myled3 = 0;} |
sephora12680 | 1:ad7134c4e365 | 239 | else if(n == 4) {myled4 = 1; wait(delay); myled4 = 0;} |
sephora12680 | 0:171d347f33f7 | 240 | return; |
sephora12680 | 1:ad7134c4e365 | 241 | } |
sephora12680 | 1:ad7134c4e365 | 242 | |
sephora12680 | 1:ad7134c4e365 | 243 | int Dist() //returns distance |
sephora12680 | 1:ad7134c4e365 | 244 | { |
sephora12680 | 1:ad7134c4e365 | 245 | uint8_t dist; |
sephora12680 | 1:ad7134c4e365 | 246 | lcd->cls(); lcd->locate(0,0); |
sephora12680 | 1:ad7134c4e365 | 247 | dist = TOF_sensor.getDistance(); |
sephora12680 | 1:ad7134c4e365 | 248 | lcd->printf("Dist=%d", dist); |
sephora12680 | 1:ad7134c4e365 | 249 | return dist; |
sephora12680 | 1:ad7134c4e365 | 250 | } |
sephora12680 | 1:ad7134c4e365 | 251 | |
sephora12680 | 1:ad7134c4e365 | 252 | int MAX(int R, int G, int B) //returns max rgb value |
sephora12680 | 1:ad7134c4e365 | 253 | { |
sephora12680 | 1:ad7134c4e365 | 254 | int max; |
sephora12680 | 1:ad7134c4e365 | 255 | if (R > G) max = R; |
sephora12680 | 1:ad7134c4e365 | 256 | else max = G; |
sephora12680 | 1:ad7134c4e365 | 257 | if (B > max) max = B; |
sephora12680 | 1:ad7134c4e365 | 258 | return max; |
maazshaikh | 2:7c34106159c8 | 259 | } |
maazshaikh | 2:7c34106159c8 | 260 | |
maazshaikh | 2:7c34106159c8 | 261 |