Sergio Nigeria / Mbed 2 deprecated Ejercicio_Simon_ARIAS

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //ARIAS - MARAS 6°B Tec
00002 #include "mbed.h"
00003 #define CANT_PULSADORES 5
00004 #define BASE_TIEMPO     0.001
00005 
00006 enum
00007 {
00008     SUELTO, QUIZA_PRESIONADO, PRESIONADO, QUIZA_SUELTO
00009 };
00010 BusIn pulsadores(PTA13, PTD5, PTD0, PTD2, PTD3);
00011 int salidas[CANT_PULSADORES];
00012 
00013 BusOut led(PTC12, PTC13, PTC16, PTC17);
00014 
00015 Serial pc(USBTX, USBRX);
00016 
00017 DigitalOut ledR(PTD6);
00018 DigitalOut ledV(PTD7);
00019 
00020 Ticker tim;
00021 int time_puls[CANT_PULSADORES] = {0};
00022 int trand=1234;
00023 int tled=1000;
00024 
00025 void juego(void);
00026 
00027 int Debounce(int index);
00028 
00029 void TimerDecremento(void)
00030 {
00031     if(tled > 0)
00032         tled--;
00033 
00034     for(int i = 0; i < CANT_PULSADORES; i++)
00035         if (time_puls[i] > 0)
00036             time_puls[i]--;
00037     trand++;//tiempo para que al usar rand cambie la semilla
00038     if(trand>=12345)
00039         trand=1234;
00040 }
00041 
00042 int sec[24]= {0}; //secuancia para el juego
00043 int nivel=0;//cantidad de niveles completados
00044 int x=1;
00045 int main()
00046 {
00047     ledR=1;
00048     ledV=1;
00049     tim.attach(&TimerDecremento, BASE_TIEMPO);
00050     
00051     pulsadores[0].mode(PullNone);
00052     pulsadores[1].mode(PullNone);
00053     pulsadores[2].mode(PullNone);
00054     pulsadores[3].mode(PullNone);
00055     pulsadores[4].mode(PullNone);
00056 
00057     while(1)
00058     {
00059         juego();
00060         for(int i = 0; i < CANT_PULSADORES; i++)
00061         {
00062             salidas[i] = Debounce(i);
00063         }
00064     }
00065 }
00066 
00067 
00068 void juego(void)
00069 {
00070     static int i=0;//nivel actual
00071     static int estadoled = 5;
00072     static int a = 0;//valor del nivel actual
00073     switch(estadoled)
00074     {
00075     case 0://INICIO
00076         ledR=1;
00077         nivel++;
00078         estadoled=1;
00079         tled=500;
00080         pc.printf("----------------\n");
00081         if(nivel==25)//DETECTA SI HICIMOS LOS 24 PUNTOS
00082             estadoled=6;
00083         break;
00084 
00085     case 1://led apagado
00086         led = 0;//apaga el led despues de mostrarlo durante 500ms
00087         if(tled==0)
00088         {
00089             tled=500;
00090             estadoled=2;
00091         }
00092         break;
00093 
00094     case 2://muestra led
00095         a = sec[i];
00096         led[a]=1;//muestra la secuencia a pulsar, el led se enciende por 500ms
00097         if(x==0)//para que solo se envie 1 vez al hercules
00098         {
00099             if(a==0)
00100             pc.printf("ROJO\n");
00101             if(a==1)
00102             pc.printf("VERDE\n");
00103             if(a==2)
00104             pc.printf("AZUL\n");
00105             if(a==3)
00106             pc.printf("AMARILLO\n");
00107             x=1;
00108         }
00109         if(tled==0)
00110         {
00111             tled=500;
00112             i++;
00113 
00114             if(i==nivel)//si ya mostro todos los colores pasa al siguiente estado
00115             {
00116                 estadoled=3;
00117                 i=0;
00118                 x=0;
00119             }
00120 
00121             else//si le falta algun o varios colores pasa al estado anterior para mostrar el siguiente
00122             {
00123                 estadoled=1;
00124                 x=0;
00125             }
00126 
00127         }
00128         break;
00129 
00130 
00131     case 3://Ingresa secuencia
00132         led = 0;
00133         a = sec[i];
00134 
00135         if(Debounce(a)==1)//si se presiona el boton correcto el programa sigue
00136         {
00137             i++;
00138             estadoled=4;
00139             tled=500;
00140         }
00141 
00142         if((Debounce(0)==1 || Debounce(1)==1 || Debounce(2)==1 || Debounce(3)==1)&&Debounce(a)==0)//si falla vuelve al estado apagado
00143         {
00144             estadoled=5;
00145         }
00146 
00147         break;
00148 
00149 
00150     case 4://Indica led Pulsado
00151         led[a] = 1;//el led pulsado se enciende durante 500ms
00152         if(tled==0)
00153         {
00154             if(i==nivel)//si se ingreso toda la secuencia correctamente vuelve al estado inicial
00155             {
00156                 estadoled=0;
00157                 i=0;
00158             }
00159 
00160             else//si falta ingresar algun color de la secuencia vuelve al paso anterior
00161                 estadoled=3;
00162         }
00163         break;
00164 
00165     case 5: //CASO OFF/PIERDE
00166         ledV=1;
00167         ledR=0;
00168         led=0;
00169         i=0;
00170         nivel=0;
00171         a=0;
00172         
00173         if(x==0)
00174         {
00175             pc.printf("\n");
00176             pc.printf("Usted Perdio, presione en pulsador de inicio para volver a jugar\n");
00177             x=1;
00178         }
00179         
00180         if(Debounce(4)==1 && tled==0)//SI PRESIONAMOS EL 5° PULSADOR
00181         {
00182             srand((unsigned)trand);//CAMBIA LA SEMILLA DEL RAND EN BASE A UN CONTADOR QUE VA CONTANDO CONSTANTEMENTE, POR LO QUE SIEMPRE SERA DIFERENTE
00183             for(int r=0; r<24; r++)
00184             {
00185                 sec[r]=(rand() % 4);//GENRA LOS 24 NÚMEROS ALEATORIOS
00186             }
00187             estadoled=0;
00188             x=0;
00189         }
00190         break;
00191 
00192     case 6: //CASO GANA
00193         led=0;
00194         ledV=0;
00195                 if(x==0)
00196         {
00197             pc.printf("Usted a Ganado, lo felicitamos\n");
00198             pc.printf("Presione para volver al estado inicial\n");
00199             x=1;
00200         }
00201         if(Debounce(4)==1)//si presionamos el pulsador 5 vuelve al estado apagado
00202         {
00203             estadoled=5;
00204             tled=100;
00205         }
00206         break;
00207     }
00208 }
00209 
00210 
00211 // ANTIRREBOTE //
00212 
00213 int Debounce(int index)
00214 {
00215 
00216     int Estado = 0;
00217     static int DEBOUNCE_estado[CANT_PULSADORES] = {SUELTO};
00218     switch (DEBOUNCE_estado[index])
00219     {
00220     case SUELTO:
00221         Estado = 0;
00222         if (pulsadores[index] == 1)
00223         {
00224             DEBOUNCE_estado[index] = QUIZA_PRESIONADO;
00225             time_puls[index] = 30;
00226         }
00227         break;
00228     case QUIZA_PRESIONADO:
00229         if (time_puls[index] == 0)
00230         {
00231             if (pulsadores[index] == 1)
00232             {
00233                 DEBOUNCE_estado[index] = PRESIONADO;
00234             }
00235             if (pulsadores[index] == 0)
00236             {
00237                 DEBOUNCE_estado[index] = SUELTO;
00238             }
00239         }
00240         break;
00241     case PRESIONADO:
00242         Estado = 1;
00243         if (pulsadores[index] == 0)
00244         {
00245             DEBOUNCE_estado[index] = QUIZA_SUELTO;
00246             time_puls[index] = 30;
00247         }
00248         break;
00249     case QUIZA_SUELTO:
00250         if (time_puls[index] == 0)
00251         {
00252             if (pulsadores[index] == 0)
00253             {
00254                 DEBOUNCE_estado[index] = SUELTO;
00255             }
00256             if (pulsadores[index] == 1)
00257             {
00258                 DEBOUNCE_estado[index] = PRESIONADO;
00259             }
00260         }
00261         break;
00262     }
00263     return Estado;
00264 }