First Simon Says version

Dependencies:   mbed DebounceIn WS2812

Committer:
elab
Date:
Thu Feb 13 09:07:14 2020 +0000
Revision:
0:47b1ab4dd893
first version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elab 0:47b1ab4dd893 1 #include "mbed.h"
elab 0:47b1ab4dd893 2 #include <stdio.h>
elab 0:47b1ab4dd893 3 #include <stdlib.h>
elab 0:47b1ab4dd893 4 #include <time.h>
elab 0:47b1ab4dd893 5 #include "LED_WS2812.h"
elab 0:47b1ab4dd893 6
elab 0:47b1ab4dd893 7 Serial pc(USBTX, USBRX);
elab 0:47b1ab4dd893 8 DigitalOut myled(LED1);
elab 0:47b1ab4dd893 9 DigitalIn greenButton(D3,PullUp);
elab 0:47b1ab4dd893 10 DigitalIn redButton(D4,PullUp);
elab 0:47b1ab4dd893 11 DigitalIn blueButton(D5,PullUp);
elab 0:47b1ab4dd893 12 DigitalIn yellowButton(D6, PullUp);
elab 0:47b1ab4dd893 13 LED_WS2812 led(D9,4);
elab 0:47b1ab4dd893 14
elab 0:47b1ab4dd893 15 int buttonArray[32];
elab 0:47b1ab4dd893 16 int array[32]; // LEDS colors array
elab 0:47b1ab4dd893 17
elab 0:47b1ab4dd893 18 int readButton(int turn){
elab 0:47b1ab4dd893 19 int currentValueG, currentValueR, currentValueB, currentValueY, status=0;
elab 0:47b1ab4dd893 20 int max=500;
elab 0:47b1ab4dd893 21 for (int i=0;i<32;i++){
elab 0:47b1ab4dd893 22 buttonArray[i]=55;
elab 0:47b1ab4dd893 23 }
elab 0:47b1ab4dd893 24 printf("readButton\n");
elab 0:47b1ab4dd893 25 for (int i=0; i<= turn; i++){
elab 0:47b1ab4dd893 26 for (int t=0; t<max; t++){
elab 0:47b1ab4dd893 27 currentValueG = greenButton.read();
elab 0:47b1ab4dd893 28 currentValueR = redButton.read();
elab 0:47b1ab4dd893 29 currentValueB = blueButton.read();
elab 0:47b1ab4dd893 30 currentValueY = yellowButton.read();
elab 0:47b1ab4dd893 31 if (currentValueG == 0){
elab 0:47b1ab4dd893 32 printf("Green button\n");
elab 0:47b1ab4dd893 33 wait_ms(250);
elab 0:47b1ab4dd893 34 buttonArray[i]=0;
elab 0:47b1ab4dd893 35 t=max;
elab 0:47b1ab4dd893 36 status=0;
elab 0:47b1ab4dd893 37 }
elab 0:47b1ab4dd893 38 else if (currentValueR == 0){
elab 0:47b1ab4dd893 39 printf("RedButton\n");
elab 0:47b1ab4dd893 40 wait_ms(250);
elab 0:47b1ab4dd893 41 buttonArray[i]=1;
elab 0:47b1ab4dd893 42 t=max;
elab 0:47b1ab4dd893 43 status=0;
elab 0:47b1ab4dd893 44 }
elab 0:47b1ab4dd893 45 else if(currentValueB == 0){
elab 0:47b1ab4dd893 46 printf("BlueButton\n");
elab 0:47b1ab4dd893 47 wait_ms(250);
elab 0:47b1ab4dd893 48 buttonArray[i]=2;
elab 0:47b1ab4dd893 49 t=max;
elab 0:47b1ab4dd893 50 status=0;
elab 0:47b1ab4dd893 51 }
elab 0:47b1ab4dd893 52 else if(currentValueY == 0){
elab 0:47b1ab4dd893 53 printf("Yellow button\n");
elab 0:47b1ab4dd893 54 wait_ms(250);
elab 0:47b1ab4dd893 55 buttonArray[i]=3;
elab 0:47b1ab4dd893 56 t=max;
elab 0:47b1ab4dd893 57 status=0;
elab 0:47b1ab4dd893 58 }
elab 0:47b1ab4dd893 59 else if(t>=(max-1)){
elab 0:47b1ab4dd893 60 printf("time out !!! \n");
elab 0:47b1ab4dd893 61 status=1;
elab 0:47b1ab4dd893 62 }
elab 0:47b1ab4dd893 63 wait_ms(10);
elab 0:47b1ab4dd893 64 }
elab 0:47b1ab4dd893 65 }
elab 0:47b1ab4dd893 66
elab 0:47b1ab4dd893 67 for(int j=0; j<=turn; j++) {
elab 0:47b1ab4dd893 68 printf("buttonArray[%d]=%d\n", j, buttonArray[j]);
elab 0:47b1ab4dd893 69 }
elab 0:47b1ab4dd893 70 printf("end of readButton \n");
elab 0:47b1ab4dd893 71 return status;
elab 0:47b1ab4dd893 72 }
elab 0:47b1ab4dd893 73
elab 0:47b1ab4dd893 74 int main() {
elab 0:47b1ab4dd893 75 int maxTurn=2, status, diff=0;
elab 0:47b1ab4dd893 76 srand(time(0));
elab 0:47b1ab4dd893 77 led.SetIntensity(25);
elab 0:47b1ab4dd893 78 led.SetColor(RED);
elab 0:47b1ab4dd893 79 for(int t=0; t<=maxTurn; t++){
elab 0:47b1ab4dd893 80 // generate the array and ON the LEDs
elab 0:47b1ab4dd893 81 printf("turn = %d\n", t);
elab 0:47b1ab4dd893 82 array[t]=rand()%4;
elab 0:47b1ab4dd893 83 for (int i=0; i<=t; i++){
elab 0:47b1ab4dd893 84 switch(array[i])
elab 0:47b1ab4dd893 85 {
elab 0:47b1ab4dd893 86 case 0:
elab 0:47b1ab4dd893 87 printf(" >>>>>>> green\n");
elab 0:47b1ab4dd893 88 led.SetColor(0,GREEN);
elab 0:47b1ab4dd893 89 wait(1);
elab 0:47b1ab4dd893 90 led.SetColor(0,BLACK);
elab 0:47b1ab4dd893 91 break;
elab 0:47b1ab4dd893 92 case 1:
elab 0:47b1ab4dd893 93 printf(" >>>>>>> red\n");
elab 0:47b1ab4dd893 94 // led.SetColor(1,RED);
elab 0:47b1ab4dd893 95 // wait(1);
elab 0:47b1ab4dd893 96 // led.SetColor(1,BLACK);
elab 0:47b1ab4dd893 97 break;
elab 0:47b1ab4dd893 98 case 2:
elab 0:47b1ab4dd893 99 printf(" >>>>>>> blue\n");
elab 0:47b1ab4dd893 100 led.SetColor(2,BLUE);
elab 0:47b1ab4dd893 101 wait(1);
elab 0:47b1ab4dd893 102 led.SetColor(2,BLACK);
elab 0:47b1ab4dd893 103 break;
elab 0:47b1ab4dd893 104 case 3:
elab 0:47b1ab4dd893 105 printf(" >>>>>>> yellow\n");
elab 0:47b1ab4dd893 106 led.SetColor(3,YELLOW);
elab 0:47b1ab4dd893 107 wait(1);
elab 0:47b1ab4dd893 108 led.SetColor(3,BLACK);
elab 0:47b1ab4dd893 109 break;
elab 0:47b1ab4dd893 110 default:
elab 0:47b1ab4dd893 111 printf(" >>>>>>> no color \n");
elab 0:47b1ab4dd893 112 break;
elab 0:47b1ab4dd893 113 }
elab 0:47b1ab4dd893 114 }
elab 0:47b1ab4dd893 115 status = readButton(t);
elab 0:47b1ab4dd893 116 // compare two arrays
elab 0:47b1ab4dd893 117 if (status==1){
elab 0:47b1ab4dd893 118 printf("Time out, game over !\n");
elab 0:47b1ab4dd893 119 t=maxTurn;// force t to maxTurn to exit
elab 0:47b1ab4dd893 120 }
elab 0:47b1ab4dd893 121 else {
elab 0:47b1ab4dd893 122 for(int j=0; j<=t; j++){
elab 0:47b1ab4dd893 123 if(array[j]!=buttonArray[j]){
elab 0:47b1ab4dd893 124 j=t;
elab 0:47b1ab4dd893 125 diff=1;
elab 0:47b1ab4dd893 126 }
elab 0:47b1ab4dd893 127
elab 0:47b1ab4dd893 128 }
elab 0:47b1ab4dd893 129 if (diff==1){
elab 0:47b1ab4dd893 130 t=maxTurn;
elab 0:47b1ab4dd893 131 printf("Color no match, Game Over !\n");
elab 0:47b1ab4dd893 132 }
elab 0:47b1ab4dd893 133 else{
elab 0:47b1ab4dd893 134 printf("You win !\n");
elab 0:47b1ab4dd893 135 }
elab 0:47b1ab4dd893 136 }
elab 0:47b1ab4dd893 137 printf("Turn finished\n");
elab 0:47b1ab4dd893 138 }
elab 0:47b1ab4dd893 139
elab 0:47b1ab4dd893 140 }