![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
xszdx
Dependencies: mbed TCS3472_I2C MCP23017 VL6180 DebounceIn WattBob_TextLCD
main_copy.cpp@0:171d347f33f7, 2020-03-03 (annotated)
- Committer:
- sephora12680
- Date:
- Tue Mar 03 17:42:15 2020 +0000
- Revision:
- 0:171d347f33f7
- Child:
- 1:ad7134c4e365
yas
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 | 0:171d347f33f7 | 7 | |
sephora12680 | 0:171d347f33f7 | 8 | //Hyperterminal configuration |
sephora12680 | 0:171d347f33f7 | 9 | //9600 bauds, 8-bit data, no parity |
sephora12680 | 0:171d347f33f7 | 10 | //VL6180X defines |
sephora12680 | 0:171d347f33f7 | 11 | |
sephora12680 | 0:171d347f33f7 | 12 | #define IDENTIFICATIONMODEL_ID 0x0000 |
sephora12680 | 0:171d347f33f7 | 13 | |
sephora12680 | 0:171d347f33f7 | 14 | Serial PC(USBTX, USBRX); |
sephora12680 | 0:171d347f33f7 | 15 | VL6180 TOF_sensor(p28, p27); |
sephora12680 | 0:171d347f33f7 | 16 | DigitalOut myled(LED1); |
sephora12680 | 0:171d347f33f7 | 17 | |
sephora12680 | 0:171d347f33f7 | 18 | #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) |
sephora12680 | 0:171d347f33f7 | 19 | #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) |
sephora12680 | 0:171d347f33f7 | 20 | |
sephora12680 | 0:171d347f33f7 | 21 | MCP23017 *par_port; |
sephora12680 | 0:171d347f33f7 | 22 | WattBob_TextLCD *lcd; |
sephora12680 | 0:171d347f33f7 | 23 | |
sephora12680 | 0:171d347f33f7 | 24 | |
sephora12680 | 0:171d347f33f7 | 25 | DigitalOut myled1 (p12); DigitalOut myled2 (p13); |
sephora12680 | 0:171d347f33f7 | 26 | DigitalOut myled3 (p14); DigitalOut myled4 (p15); |
sephora12680 | 0:171d347f33f7 | 27 | |
sephora12680 | 0:171d347f33f7 | 28 | DebounceIn pb1(p8); DebounceIn pb2(p9); |
sephora12680 | 0:171d347f33f7 | 29 | DebounceIn pb3(p10); DebounceIn pb4(p11); |
sephora12680 | 0:171d347f33f7 | 30 | |
sephora12680 | 0:171d347f33f7 | 31 | int old_pb1 = 0, old_pb2 = 0, old_pb3 = 0, old_pb4 = 0; |
sephora12680 | 0:171d347f33f7 | 32 | int new_pb1, new_pb2, new_pb3, new_pb4; |
sephora12680 | 0:171d347f33f7 | 33 | |
sephora12680 | 0:171d347f33f7 | 34 | int i = 0, j, row, led; |
sephora12680 | 0:171d347f33f7 | 35 | int order[10][4]; |
sephora12680 | 0:171d347f33f7 | 36 | int levelreset, tries, level = 1; |
sephora12680 | 0:171d347f33f7 | 37 | float delay; uint8_t dist; |
sephora12680 | 0:171d347f33f7 | 38 | |
sephora12680 | 0:171d347f33f7 | 39 | /*int order[4][4] = {1,2,3,4, |
sephora12680 | 0:171d347f33f7 | 40 | 4,3,2,1, |
sephora12680 | 0:171d347f33f7 | 41 | 3,2,1,4, |
sephora12680 | 0:171d347f33f7 | 42 | 2,1,3,4};*/ |
sephora12680 | 0:171d347f33f7 | 43 | |
sephora12680 | 0:171d347f33f7 | 44 | void level1(); |
sephora12680 | 0:171d347f33f7 | 45 | void reset(); |
sephora12680 | 0:171d347f33f7 | 46 | void light(int,int); |
sephora12680 | 0:171d347f33f7 | 47 | |
sephora12680 | 0:171d347f33f7 | 48 | int Dist() |
sephora12680 | 0:171d347f33f7 | 49 | { |
sephora12680 | 0:171d347f33f7 | 50 | lcd->cls(); lcd->locate(0,0); |
sephora12680 | 0:171d347f33f7 | 51 | dist = TOF_sensor.getDistance(); |
sephora12680 | 0:171d347f33f7 | 52 | lcd->printf("Dist=%d", dist); |
sephora12680 | 0:171d347f33f7 | 53 | wait(0.5); |
sephora12680 | 0:171d347f33f7 | 54 | return dist; |
sephora12680 | 0:171d347f33f7 | 55 | } |
sephora12680 | 0:171d347f33f7 | 56 | |
sephora12680 | 0:171d347f33f7 | 57 | // SPST Pushbutton count demo using internal PullUp function |
sephora12680 | 0:171d347f33f7 | 58 | // no external PullUp resistor needed |
sephora12680 | 0:171d347f33f7 | 59 | // Pushbutton from P8 to GND. |
sephora12680 | 0:171d347f33f7 | 60 | // Demonstrates need for debounce - will not count more than once per button hit |
sephora12680 | 0:171d347f33f7 | 61 | // This ocuurs on all switches due to mechanical contact bounce |
sephora12680 | 0:171d347f33f7 | 62 | |
sephora12680 | 0:171d347f33f7 | 63 | |
sephora12680 | 0:171d347f33f7 | 64 | int main() |
sephora12680 | 0:171d347f33f7 | 65 | { |
sephora12680 | 0:171d347f33f7 | 66 | // Use internal pullup for pushbutton |
sephora12680 | 0:171d347f33f7 | 67 | pb1.mode(PullUp); |
sephora12680 | 0:171d347f33f7 | 68 | pb2.mode(PullUp); |
sephora12680 | 0:171d347f33f7 | 69 | pb3.mode(PullUp); |
sephora12680 | 0:171d347f33f7 | 70 | pb4.mode(PullUp); |
sephora12680 | 0:171d347f33f7 | 71 | // Delay for initial pullup to take effect |
sephora12680 | 0:171d347f33f7 | 72 | wait(.001); |
sephora12680 | 0:171d347f33f7 | 73 | TOF_sensor.VL6180_Init(); |
sephora12680 | 0:171d347f33f7 | 74 | par_port = new MCP23017(p9, p10, 0x40); |
sephora12680 | 0:171d347f33f7 | 75 | par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob |
sephora12680 | 0:171d347f33f7 | 76 | lcd = new WattBob_TextLCD(par_port); |
sephora12680 | 0:171d347f33f7 | 77 | BACK_LIGHT_ON(par_port); |
sephora12680 | 0:171d347f33f7 | 78 | //char c = PC.getc(); |
sephora12680 | 0:171d347f33f7 | 79 | //if (c == 'o') |
sephora12680 | 0:171d347f33f7 | 80 | while(1) |
sephora12680 | 0:171d347f33f7 | 81 | { |
sephora12680 | 0:171d347f33f7 | 82 | while(Dist() < 250) |
sephora12680 | 0:171d347f33f7 | 83 | while(level <= 4) |
sephora12680 | 0:171d347f33f7 | 84 | { |
sephora12680 | 0:171d347f33f7 | 85 | if (level == 0) break; |
sephora12680 | 0:171d347f33f7 | 86 | for (row=0; row<10; row++) |
sephora12680 | 0:171d347f33f7 | 87 | for (j=0; j<4; j++) |
sephora12680 | 0:171d347f33f7 | 88 | { order [row][j] = (rand()% 4) + 1;} |
sephora12680 | 0:171d347f33f7 | 89 | row = rand()%4; |
sephora12680 | 0:171d347f33f7 | 90 | tries = 0; |
sephora12680 | 0:171d347f33f7 | 91 | level1(); |
sephora12680 | 0:171d347f33f7 | 92 | } |
sephora12680 | 0:171d347f33f7 | 93 | } |
sephora12680 | 0:171d347f33f7 | 94 | } |
sephora12680 | 0:171d347f33f7 | 95 | |
sephora12680 | 0:171d347f33f7 | 96 | |
sephora12680 | 0:171d347f33f7 | 97 | |
sephora12680 | 0:171d347f33f7 | 98 | void level1() |
sephora12680 | 0:171d347f33f7 | 99 | { |
sephora12680 | 0:171d347f33f7 | 100 | while(Dist() < 250) |
sephora12680 | 0:171d347f33f7 | 101 | { |
sephora12680 | 0:171d347f33f7 | 102 | levelreset = 0; |
sephora12680 | 0:171d347f33f7 | 103 | for (j=0; j<4; j++) |
sephora12680 | 0:171d347f33f7 | 104 | { led = order[row][j]; light(led,1); } //switches on the respective LEDs according to the order |
sephora12680 | 0:171d347f33f7 | 105 | |
sephora12680 | 0:171d347f33f7 | 106 | while(Dist() < 250) |
sephora12680 | 0:171d347f33f7 | 107 | { |
sephora12680 | 0:171d347f33f7 | 108 | new_pb1 = pb1; new_pb2 = pb2; |
sephora12680 | 0:171d347f33f7 | 109 | new_pb3 = pb3; new_pb4 = pb4; |
sephora12680 | 0:171d347f33f7 | 110 | |
sephora12680 | 0:171d347f33f7 | 111 | 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 | 112 | { |
sephora12680 | 0:171d347f33f7 | 113 | switch(order[row][i]) |
sephora12680 | 0:171d347f33f7 | 114 | { |
sephora12680 | 0:171d347f33f7 | 115 | case 1: |
sephora12680 | 0:171d347f33f7 | 116 | { |
sephora12680 | 0:171d347f33f7 | 117 | if((new_pb1 == 0) && (old_pb1 == 1)) |
sephora12680 | 0:171d347f33f7 | 118 | { light(1,0); i++;} |
sephora12680 | 0:171d347f33f7 | 119 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 120 | } break; |
sephora12680 | 0:171d347f33f7 | 121 | case 2: |
sephora12680 | 0:171d347f33f7 | 122 | { |
sephora12680 | 0:171d347f33f7 | 123 | if((new_pb2 == 0) && (old_pb2 == 1)) |
sephora12680 | 0:171d347f33f7 | 124 | { light(2,0); i++;} |
sephora12680 | 0:171d347f33f7 | 125 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 126 | } break; |
sephora12680 | 0:171d347f33f7 | 127 | case 3: |
sephora12680 | 0:171d347f33f7 | 128 | { |
sephora12680 | 0:171d347f33f7 | 129 | if((new_pb3 == 0) && (old_pb3 == 1)) |
sephora12680 | 0:171d347f33f7 | 130 | { light(3,0); i++;} |
sephora12680 | 0:171d347f33f7 | 131 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 132 | } break; |
sephora12680 | 0:171d347f33f7 | 133 | case 4: |
sephora12680 | 0:171d347f33f7 | 134 | { |
sephora12680 | 0:171d347f33f7 | 135 | if((new_pb4 == 0) && (old_pb4 == 1)) |
sephora12680 | 0:171d347f33f7 | 136 | { light(4,0); i++;} |
sephora12680 | 0:171d347f33f7 | 137 | else {reset(); levelreset = 1; tries++;} |
sephora12680 | 0:171d347f33f7 | 138 | } break; |
sephora12680 | 0:171d347f33f7 | 139 | } |
sephora12680 | 0:171d347f33f7 | 140 | if (levelreset == 1) |
sephora12680 | 0:171d347f33f7 | 141 | { |
sephora12680 | 0:171d347f33f7 | 142 | if (tries > 3) {reset(); level = 0; return;} |
sephora12680 | 0:171d347f33f7 | 143 | else break; |
sephora12680 | 0:171d347f33f7 | 144 | } |
sephora12680 | 0:171d347f33f7 | 145 | else if (levelreset == 0) |
sephora12680 | 0:171d347f33f7 | 146 | if(i >= 4){level++; reset(); return;} |
sephora12680 | 0:171d347f33f7 | 147 | } |
sephora12680 | 0:171d347f33f7 | 148 | old_pb1 = new_pb1; old_pb2 = new_pb2; |
sephora12680 | 0:171d347f33f7 | 149 | old_pb3 = new_pb3; old_pb4 = new_pb4; |
sephora12680 | 0:171d347f33f7 | 150 | } |
sephora12680 | 0:171d347f33f7 | 151 | } |
sephora12680 | 0:171d347f33f7 | 152 | } |
sephora12680 | 0:171d347f33f7 | 153 | |
sephora12680 | 0:171d347f33f7 | 154 | |
sephora12680 | 0:171d347f33f7 | 155 | |
sephora12680 | 0:171d347f33f7 | 156 | void reset() //function to turn off LEDs and reset order |
sephora12680 | 0:171d347f33f7 | 157 | { |
sephora12680 | 0:171d347f33f7 | 158 | myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0; |
sephora12680 | 0:171d347f33f7 | 159 | i = 0; wait(1); return; |
sephora12680 | 0:171d347f33f7 | 160 | } |
sephora12680 | 0:171d347f33f7 | 161 | |
sephora12680 | 0:171d347f33f7 | 162 | void light(int n, int m) //to turn on LEDs |
sephora12680 | 0:171d347f33f7 | 163 | { |
sephora12680 | 0:171d347f33f7 | 164 | if (m == 0) //for answer |
sephora12680 | 0:171d347f33f7 | 165 | { |
sephora12680 | 0:171d347f33f7 | 166 | //turns LED on for 1 second |
sephora12680 | 0:171d347f33f7 | 167 | if(n == 1) {myled1 = 1; wait(1); myled1 = 0;} |
sephora12680 | 0:171d347f33f7 | 168 | else if(n == 2) {myled2 = 1; wait(1); myled2 = 0;} |
sephora12680 | 0:171d347f33f7 | 169 | else if(n == 3) {myled3 = 1; wait(1); myled3 = 0;} |
sephora12680 | 0:171d347f33f7 | 170 | else if(n == 4) {myled4 = 1; wait(1); myled4 = 0;} |
sephora12680 | 0:171d347f33f7 | 171 | } |
sephora12680 | 0:171d347f33f7 | 172 | |
sephora12680 | 0:171d347f33f7 | 173 | else if (m == 1) //for demo |
sephora12680 | 0:171d347f33f7 | 174 | { |
sephora12680 | 0:171d347f33f7 | 175 | //sets delay based on level number |
sephora12680 | 0:171d347f33f7 | 176 | if(level == 1) delay = 1; |
sephora12680 | 0:171d347f33f7 | 177 | else if (level == 2) delay = 0.75; |
sephora12680 | 0:171d347f33f7 | 178 | else if (level == 3) delay = 0.50; |
sephora12680 | 0:171d347f33f7 | 179 | else if (level == 4) delay = 0.25; |
sephora12680 | 0:171d347f33f7 | 180 | |
sephora12680 | 0:171d347f33f7 | 181 | //turns LED on for delay seconds before turning off |
sephora12680 | 0:171d347f33f7 | 182 | if(n == 1) {myled1 = 1; wait(delay); myled1 = 0; wait(0.2);} |
sephora12680 | 0:171d347f33f7 | 183 | else if(n == 2) {myled2 = 1; wait(delay); myled2 = 0; wait(0.2);} |
sephora12680 | 0:171d347f33f7 | 184 | else if(n == 3) {myled3 = 1; wait(delay); myled3 = 0; wait(0.2);} |
sephora12680 | 0:171d347f33f7 | 185 | else if(n == 4) {myled4 = 1; wait(delay); myled4 = 0; wait(0.2);} |
sephora12680 | 0:171d347f33f7 | 186 | } |
sephora12680 | 0:171d347f33f7 | 187 | return; |
sephora12680 | 0:171d347f33f7 | 188 | } |