eLab Team / Mbed 2 deprecated SimonSays

Dependencies:   mbed DebounceIn WS2812

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <stdio.h> 
00003 #include <stdlib.h> 
00004 #include <time.h> 
00005 #include "LED_WS2812.h"
00006  
00007 Serial pc(USBTX, USBRX); 
00008 DigitalOut myled(LED1);
00009 DigitalIn greenButton(D3,PullUp);
00010 DigitalIn redButton(D4,PullUp);
00011 DigitalIn blueButton(D5,PullUp);
00012 DigitalIn yellowButton(D6, PullUp);
00013 LED_WS2812 led(D9,4);
00014 
00015 int buttonArray[32];
00016 int array[32]; // LEDS colors array
00017 
00018 int readButton(int turn){
00019     int currentValueG, currentValueR, currentValueB, currentValueY, status=0;
00020     int max=400;
00021     for (int i=0;i<32;i++){
00022         buttonArray[i]=55;
00023     }
00024     printf("readButton\n");
00025     for (int i=0; i<= turn; i++){
00026         for (int t=0; t<max; t++){
00027             currentValueG = greenButton.read();
00028             currentValueR = redButton.read();
00029             currentValueB = blueButton.read();
00030             currentValueY = yellowButton.read();
00031             if (currentValueG == 0){
00032                 printf("Green button\n");
00033                 wait_ms(250);
00034                 buttonArray[i]=0;
00035                 t=max;
00036                 status=0;
00037             }
00038             else if (currentValueR == 0){
00039                 printf("RedButton\n");
00040                 wait_ms(250);
00041                 buttonArray[i]=1;
00042                 t=max;
00043                 status=0;
00044             }
00045             else if(currentValueB == 0){
00046                 printf("BlueButton\n");
00047                 wait_ms(250);
00048                 buttonArray[i]=2;
00049                 t=max;
00050                 status=0;
00051             }
00052             else if(currentValueY == 0){
00053                 printf("Yellow button\n");
00054                 wait_ms(250);
00055                 buttonArray[i]=3;
00056                 t=max;
00057                 status=0;
00058             }
00059             else if(t>=(max-1)){
00060                 printf("time out !!! \n");
00061                 status=1;
00062             }
00063             wait_ms(10);
00064         }       
00065     }
00066 
00067     for(int j=0; j<=turn; j++) {
00068         printf("buttonArray[%d]=%d\n", j, buttonArray[j]);
00069     }
00070     printf("end of readButton \n");
00071     return status;
00072 }
00073 
00074 int main() {
00075     int maxTurn=5, status, diff=0;
00076     srand(time(0));
00077     led.SetColor(BLACK);   
00078     led.SetIntensity(25);
00079     for(int t=0; t<=maxTurn; t++){
00080         // generate the array  and ON the LEDs
00081         printf("turn = %d\n", t);
00082         array[t]=rand()%4;
00083         for (int i=0; i<=t; i++){
00084             switch(array[i])
00085             {
00086                 case 0:
00087                     printf(" >>>>>>> green\n");
00088                     led.SetColor(GREEN,0);
00089                     wait_ms(500);
00090                     led.SetColor(BLACK);
00091                     wait_ms(50);
00092                     break;
00093                 case 1:
00094                     printf(" >>>>>>> red\n");
00095                     led.SetColor(RED,1);
00096                     wait_ms(500);
00097                     led.SetColor(BLACK);
00098                     wait_ms(50);
00099                     break;
00100                 case 2:
00101                     printf(" >>>>>>> blue\n");
00102                     led.SetColor(BLUE,2);
00103                     wait_ms(500);
00104                     led.SetColor(BLACK);
00105                     wait_ms(50);
00106                     break;
00107                 case 3:
00108                     printf(" >>>>>>> yellow\n");
00109                     led.SetColor(YELLOW,3);
00110                     wait_ms(500);
00111                     led.SetColor(BLACK);
00112                     wait_ms(50);
00113                     break;
00114                 default:
00115                     printf(" >>>>>>> no color \n");
00116                     break;
00117             }
00118         }
00119         status = readButton(t);
00120         // compare two arrays
00121         if (status==1){
00122             printf("Time out, game over !\n");
00123             led.SetColor(ORANGE);
00124             t=maxTurn;// force t to maxTurn to exit       
00125         }
00126         else {
00127             for(int j=0; j<=t; j++){
00128                 if(array[j]!=buttonArray[j]){
00129                     j=t;
00130                     diff=1;
00131                 }
00132          
00133             }
00134             if (diff==1){
00135                 t=maxTurn;
00136                 printf("Color no match, Game Over !\n");
00137                 led.InsertColor(RED);
00138                 led.StartBlink(0.2);
00139             }
00140             else{
00141                 if(t<maxTurn){
00142                     printf("t=%d\n",t);
00143                     printf("next level !!!\n");
00144                 }
00145                 else{
00146                     printf("t=%d\n",t);
00147                     printf("You win !\n");
00148                     led.InsertColor(GREEN);
00149                     led.StartBlink(0.2);
00150                     wait(3);
00151                     led.StopBlink();
00152                 }
00153             }
00154         }
00155         printf("Turn finished\n");
00156     }
00157           
00158 }