xszdx

Dependencies:   mbed TCS3472_I2C MCP23017 VL6180 DebounceIn WattBob_TextLCD

Committer:
sephora12680
Date:
Fri Mar 06 12:35:11 2020 +0000
Revision:
1:ad7134c4e365
Parent:
0:171d347f33f7
Child:
2:7c34106159c8
with sensors

Who changed what in which revision?

UserRevisionLine numberNew 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
sephora12680 1:ad7134c4e365 21 DigitalOut myled1 (p13); DigitalOut myled2 (p14);
sephora12680 1:ad7134c4e365 22 DigitalOut myled3 (p15); DigitalOut myled4 (p16);
sephora12680 1:ad7134c4e365 23
sephora12680 1:ad7134c4e365 24 DebounceIn pb1(p17); DebounceIn pb2(p18);
sephora12680 1:ad7134c4e365 25 DebounceIn pb3(p19); DebounceIn pb4(p20);
sephora12680 0:171d347f33f7 26
sephora12680 0:171d347f33f7 27 int old_pb1 = 0, old_pb2 = 0, old_pb3 = 0, old_pb4 = 0;
sephora12680 1:ad7134c4e365 28 int new_pb1, new_pb2, new_pb3, new_pb4;
sephora12680 0:171d347f33f7 29
sephora12680 1:ad7134c4e365 30 int i = 0, col, row, led; //variables for order manipulation
sephora12680 1:ad7134c4e365 31 int levelreset, tries, level = 1; //level parameters
sephora12680 1:ad7134c4e365 32 float delay; //delay for blinking LEDs
sephora12680 1:ad7134c4e365 33 int D = 30; //for distance sensor
sephora12680 0:171d347f33f7 34
sephora12680 1:ad7134c4e365 35 int order[24][4] = //ORDER ARRAY FOR LED GAME
sephora12680 1:ad7134c4e365 36 {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 37 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 38 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 39 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 40
sephora12680 1:ad7134c4e365 41 void LEDgame(); //function prototype of LED game
sephora12680 1:ad7134c4e365 42 void reset(); //to reset level
sephora12680 1:ad7134c4e365 43 void light(int); //turn on LEDs
sephora12680 1:ad7134c4e365 44 int Dist(); //check distance sensor
sephora12680 1:ad7134c4e365 45 int MAX(int,int,int); //find max rgb value
sephora12680 0:171d347f33f7 46
sephora12680 0:171d347f33f7 47 int main()
sephora12680 0:171d347f33f7 48 {
sephora12680 1:ad7134c4e365 49 pb1.mode(PullUp); pb2.mode(PullUp);
sephora12680 1:ad7134c4e365 50 pb3.mode(PullUp); pb4.mode(PullUp);
sephora12680 0:171d347f33f7 51 wait(.001);
sephora12680 1:ad7134c4e365 52
sephora12680 0:171d347f33f7 53 par_port = new MCP23017(p9, p10, 0x40);
sephora12680 1:ad7134c4e365 54 par_port->config(0x0F00, 0x0F00, 0x0F00); //configure MCP23017 chip on WattBob
sephora12680 0:171d347f33f7 55 lcd = new WattBob_TextLCD(par_port);
sephora12680 0:171d347f33f7 56 BACK_LIGHT_ON(par_port);
sephora12680 1:ad7134c4e365 57
sephora12680 1:ad7134c4e365 58 TOF_sensor.VL6180_Init();
sephora12680 1:ad7134c4e365 59 rgb_sensor.enablePowerAndRGBC();
sephora12680 1:ad7134c4e365 60 rgb_sensor.setIntegrationTime(100);
sephora12680 1:ad7134c4e365 61
sephora12680 1:ad7134c4e365 62 int rgb_read[4], maxrgb;
sephora12680 0:171d347f33f7 63 //char c = PC.getc();
sephora12680 1:ad7134c4e365 64 //(c == 'o')
sephora12680 0:171d347f33f7 65 while(1)
sephora12680 0:171d347f33f7 66 {
sephora12680 1:ad7134c4e365 67 rgb_sensor.getAllColors(rgb_read);
sephora12680 1:ad7134c4e365 68 maxrgb = MAX(rgb_read[1],rgb_read[2],rgb_read[3]);
sephora12680 1:ad7134c4e365 69 if (maxrgb == rgb_read[1])
sephora12680 1:ad7134c4e365 70 //if (maxrgb > 2000 && maxrgb < 3000)
sephora12680 1:ad7134c4e365 71 while(level <= 4 && Dist() < D)
sephora12680 0:171d347f33f7 72 {
sephora12680 1:ad7134c4e365 73 if (level == 0) break; //to stop game if level reset to zero.
sephora12680 1:ad7134c4e365 74 row = rand()%24;
sephora12680 1:ad7134c4e365 75 tries = 1;
sephora12680 1:ad7134c4e365 76 LEDgame();
sephora12680 0:171d347f33f7 77 }
sephora12680 1:ad7134c4e365 78 if (Dist() > D) level = 1; //resets game if card is removed.
sephora12680 0:171d347f33f7 79 }
sephora12680 0:171d347f33f7 80 }
sephora12680 0:171d347f33f7 81
sephora12680 0:171d347f33f7 82
sephora12680 0:171d347f33f7 83
sephora12680 1:ad7134c4e365 84 void LEDgame()
sephora12680 0:171d347f33f7 85 {
sephora12680 1:ad7134c4e365 86 while(Dist() < D)
sephora12680 0:171d347f33f7 87 {
sephora12680 0:171d347f33f7 88 levelreset = 0;
sephora12680 1:ad7134c4e365 89 for (col=0; col<4; col++)
sephora12680 1:ad7134c4e365 90 {led = order[row][col]; light(led);} //switches on the respective LEDs according to the order
sephora12680 0:171d347f33f7 91
sephora12680 1:ad7134c4e365 92 while(Dist() < D)
sephora12680 0:171d347f33f7 93 {
sephora12680 0:171d347f33f7 94 new_pb1 = pb1; new_pb2 = pb2;
sephora12680 0:171d347f33f7 95 new_pb3 = pb3; new_pb4 = pb4;
sephora12680 0:171d347f33f7 96
sephora12680 0:171d347f33f7 97 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 98 {
sephora12680 0:171d347f33f7 99 switch(order[row][i])
sephora12680 0:171d347f33f7 100 {
sephora12680 0:171d347f33f7 101 case 1:
sephora12680 0:171d347f33f7 102 {
sephora12680 0:171d347f33f7 103 if((new_pb1 == 0) && (old_pb1 == 1))
sephora12680 1:ad7134c4e365 104 { myled1 = 1; i++;}
sephora12680 0:171d347f33f7 105 else {reset(); levelreset = 1; tries++;}
sephora12680 0:171d347f33f7 106 } break;
sephora12680 0:171d347f33f7 107 case 2:
sephora12680 0:171d347f33f7 108 {
sephora12680 0:171d347f33f7 109 if((new_pb2 == 0) && (old_pb2 == 1))
sephora12680 1:ad7134c4e365 110 { myled2 = 1; i++;}
sephora12680 0:171d347f33f7 111 else {reset(); levelreset = 1; tries++;}
sephora12680 0:171d347f33f7 112 } break;
sephora12680 0:171d347f33f7 113 case 3:
sephora12680 0:171d347f33f7 114 {
sephora12680 0:171d347f33f7 115 if((new_pb3 == 0) && (old_pb3 == 1))
sephora12680 1:ad7134c4e365 116 { myled3 = 1; i++;}
sephora12680 0:171d347f33f7 117 else {reset(); levelreset = 1; tries++;}
sephora12680 0:171d347f33f7 118 } break;
sephora12680 0:171d347f33f7 119 case 4:
sephora12680 0:171d347f33f7 120 {
sephora12680 0:171d347f33f7 121 if((new_pb4 == 0) && (old_pb4 == 1))
sephora12680 1:ad7134c4e365 122 { myled4 = 1; i++;}
sephora12680 0:171d347f33f7 123 else {reset(); levelreset = 1; tries++;}
sephora12680 0:171d347f33f7 124 } break;
sephora12680 0:171d347f33f7 125 }
sephora12680 1:ad7134c4e365 126 if (levelreset == 1) //do if the level was reset during the game
sephora12680 0:171d347f33f7 127 {
sephora12680 1:ad7134c4e365 128 //reset level to zero to stop game if number of tries exceeded
sephora12680 1:ad7134c4e365 129 if (tries > 3) {level = 0; reset(); return;}
sephora12680 0:171d347f33f7 130 else break;
sephora12680 0:171d347f33f7 131 }
sephora12680 1:ad7134c4e365 132 else if (levelreset == 0) //else increment level counter
sephora12680 1:ad7134c4e365 133 if(i >= 4){level++; wait(2); reset(); return;}
sephora12680 0:171d347f33f7 134 }
sephora12680 0:171d347f33f7 135 old_pb1 = new_pb1; old_pb2 = new_pb2;
sephora12680 0:171d347f33f7 136 old_pb3 = new_pb3; old_pb4 = new_pb4;
sephora12680 0:171d347f33f7 137 }
sephora12680 0:171d347f33f7 138 }
sephora12680 0:171d347f33f7 139 }
sephora12680 0:171d347f33f7 140
sephora12680 0:171d347f33f7 141
sephora12680 0:171d347f33f7 142
sephora12680 0:171d347f33f7 143 void reset() //function to turn off LEDs and reset order
sephora12680 0:171d347f33f7 144 {
sephora12680 0:171d347f33f7 145 myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 0;
sephora12680 0:171d347f33f7 146 i = 0; wait(1); return;
sephora12680 0:171d347f33f7 147 }
sephora12680 0:171d347f33f7 148
sephora12680 1:ad7134c4e365 149 void light(int n) //to turn on LEDs
sephora12680 0:171d347f33f7 150 {
sephora12680 1:ad7134c4e365 151 //sets delay based on level number
sephora12680 1:ad7134c4e365 152 if(level == 1) delay = 1;
sephora12680 1:ad7134c4e365 153 else if (level == 2) delay = 0.75;
sephora12680 1:ad7134c4e365 154 else if (level == 3) delay = 0.50;
sephora12680 1:ad7134c4e365 155 else if (level == 4) delay = 0.25;
sephora12680 0:171d347f33f7 156
sephora12680 1:ad7134c4e365 157 //turns LED on for delay seconds before turning off
sephora12680 1:ad7134c4e365 158 if(n == 1) {myled1 = 1; wait(delay); myled1 = 0;}
sephora12680 1:ad7134c4e365 159 else if(n == 2) {myled2 = 1; wait(delay); myled2 = 0;}
sephora12680 1:ad7134c4e365 160 else if(n == 3) {myled3 = 1; wait(delay); myled3 = 0;}
sephora12680 1:ad7134c4e365 161 else if(n == 4) {myled4 = 1; wait(delay); myled4 = 0;}
sephora12680 0:171d347f33f7 162 return;
sephora12680 1:ad7134c4e365 163 }
sephora12680 1:ad7134c4e365 164
sephora12680 1:ad7134c4e365 165 int Dist() //returns distance
sephora12680 1:ad7134c4e365 166 {
sephora12680 1:ad7134c4e365 167 uint8_t dist;
sephora12680 1:ad7134c4e365 168 lcd->cls(); lcd->locate(0,0);
sephora12680 1:ad7134c4e365 169 dist = TOF_sensor.getDistance();
sephora12680 1:ad7134c4e365 170 lcd->printf("Dist=%d", dist);
sephora12680 1:ad7134c4e365 171 return dist;
sephora12680 1:ad7134c4e365 172 }
sephora12680 1:ad7134c4e365 173
sephora12680 1:ad7134c4e365 174 int MAX(int R, int G, int B) //returns max rgb value
sephora12680 1:ad7134c4e365 175 {
sephora12680 1:ad7134c4e365 176 int max;
sephora12680 1:ad7134c4e365 177 if (R > G) max = R;
sephora12680 1:ad7134c4e365 178 else max = G;
sephora12680 1:ad7134c4e365 179 if (B > max) max = B;
sephora12680 1:ad7134c4e365 180 return max;
sephora12680 0:171d347f33f7 181 }