Juego de desactivar la bomba

Dependencies:   mbed

Committer:
tzuran
Date:
Wed May 29 22:06:13 2019 +0000
Revision:
0:863c53b79de8
Version final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzuran 0:863c53b79de8 1 #include "mbed.h"
tzuran 0:863c53b79de8 2 #include "math.h"
tzuran 0:863c53b79de8 3
tzuran 0:863c53b79de8 4 //LED RGB
tzuran 0:863c53b79de8 5 DigitalOut RGB_r(LED_RED);
tzuran 0:863c53b79de8 6 DigitalOut RGB_g(LED_GREEN);
tzuran 0:863c53b79de8 7
tzuran 0:863c53b79de8 8 //SALIDA BCD
tzuran 0:863c53b79de8 9 void numeros_bcd(int n);
tzuran 0:863c53b79de8 10 BusOut unidades(D2, D3, D8, D9);
tzuran 0:863c53b79de8 11 BusOut decenas(D4, D5, D6, D7);
tzuran 0:863c53b79de8 12
tzuran 0:863c53b79de8 13 //TICKERS
tzuran 0:863c53b79de8 14 Ticker contador;
tzuran 0:863c53b79de8 15 Ticker boton;
tzuran 0:863c53b79de8 16 int mandar = 0;
tzuran 0:863c53b79de8 17 void cuenta_seg();
tzuran 0:863c53b79de8 18 unsigned int cuenta_descendente;
tzuran 0:863c53b79de8 19 int estado_parpadeo = 0;
tzuran 0:863c53b79de8 20
tzuran 0:863c53b79de8 21 //PARPADEO
tzuran 0:863c53b79de8 22 int partida_perdida = 0;
tzuran 0:863c53b79de8 23 int partida_terminada = 0;
tzuran 0:863c53b79de8 24
tzuran 0:863c53b79de8 25 //BOTON DE RESTART
tzuran 0:863c53b79de8 26 DigitalIn restartButton(PTB8);
tzuran 0:863c53b79de8 27 void button_in();
tzuran 0:863c53b79de8 28 unsigned char state1 = 0x00;
tzuran 0:863c53b79de8 29 int button_output = 0;
tzuran 0:863c53b79de8 30
tzuran 0:863c53b79de8 31 //CABLES A DESCONECTAR
tzuran 0:863c53b79de8 32 DigitalIn cable_1(PTE2);
tzuran 0:863c53b79de8 33 DigitalIn cable_2(PTE3);
tzuran 0:863c53b79de8 34 DigitalIn cable_3(PTE4);
tzuran 0:863c53b79de8 35 DigitalIn cable_4(PTE5);
tzuran 0:863c53b79de8 36 int cableDesconectado();
tzuran 0:863c53b79de8 37 int n = 0; //INDICE DEL VECTOR cablesDesconectados
tzuran 0:863c53b79de8 38 int cablesDesconectados[4] = {0};
tzuran 0:863c53b79de8 39 int cablesPasados(int n); //DEVUELVE UN 1 SI SE HAYA n EN EL VECTOR cablesDesconectados
tzuran 0:863c53b79de8 40 //MEDICION DE RUIDO PARA RAND
tzuran 0:863c53b79de8 41 AnalogIn noise(PTC1);
tzuran 0:863c53b79de8 42
tzuran 0:863c53b79de8 43 //SECUENCIA
tzuran 0:863c53b79de8 44 int secuencia[4];
tzuran 0:863c53b79de8 45 int cuenta = 0;
tzuran 0:863c53b79de8 46 void secuencia_aleatoria();
tzuran 0:863c53b79de8 47
tzuran 0:863c53b79de8 48 //MAQUINA DE ESTADOS GENERAL
tzuran 0:863c53b79de8 49 void ME_GENERAL();
tzuran 0:863c53b79de8 50 enum ME_GENERAL_ESTADOS {PAUSA,INICIO, COMPARO_CABLE_1, COMPARO_CABLE_2, COMPARO_CABLE_3, COMPARO_CABLE_4, GANASTE, PERDISTE };
tzuran 0:863c53b79de8 51 ME_GENERAL_ESTADOS ME_GENERAL_ESTADO;
tzuran 0:863c53b79de8 52
tzuran 0:863c53b79de8 53
tzuran 0:863c53b79de8 54 int main()
tzuran 0:863c53b79de8 55 {
tzuran 0:863c53b79de8 56 //ESTADO INICIAL LEDS
tzuran 0:863c53b79de8 57 RGB_r = 1;
tzuran 0:863c53b79de8 58 RGB_g = 1;
tzuran 0:863c53b79de8 59
tzuran 0:863c53b79de8 60 //PULLUP EN LAS ENTRADAS
tzuran 0:863c53b79de8 61 cable_1.mode(PullUp);
tzuran 0:863c53b79de8 62 cable_2.mode(PullUp);
tzuran 0:863c53b79de8 63 cable_3.mode(PullUp);
tzuran 0:863c53b79de8 64 cable_4.mode(PullUp);
tzuran 0:863c53b79de8 65 restartButton.mode(PullUp);
tzuran 0:863c53b79de8 66
tzuran 0:863c53b79de8 67 //VALOR INICIAL DE LA CUENTA DESCENDENTE
tzuran 0:863c53b79de8 68 cuenta_descendente = 60;
tzuran 0:863c53b79de8 69
tzuran 0:863c53b79de8 70 //TICKERS
tzuran 0:863c53b79de8 71 contador.attach(&cuenta_seg, 1);
tzuran 0:863c53b79de8 72 boton.attach(&button_in, 0.01);
tzuran 0:863c53b79de8 73
tzuran 0:863c53b79de8 74
tzuran 0:863c53b79de8 75 //GENERACION DE SECUENCIA ALEATORIA
tzuran 0:863c53b79de8 76 srand(int(noise * 10000));
tzuran 0:863c53b79de8 77 secuencia_aleatoria();
tzuran 0:863c53b79de8 78
tzuran 0:863c53b79de8 79
tzuran 0:863c53b79de8 80
tzuran 0:863c53b79de8 81 ME_GENERAL_ESTADO = PAUSA;
tzuran 0:863c53b79de8 82
tzuran 0:863c53b79de8 83 while(1) {
tzuran 0:863c53b79de8 84 //DETECCION DE CABLES DESCONECTADOS
tzuran 0:863c53b79de8 85 if(ME_GENERAL_ESTADO != PAUSA){
tzuran 0:863c53b79de8 86 cableDesconectado();
tzuran 0:863c53b79de8 87
tzuran 0:863c53b79de8 88 //ENVIO DE DATOS A LA PC
tzuran 0:863c53b79de8 89 if(mandar) {
tzuran 0:863c53b79de8 90 printf("Cuenta: %d Partida terminada: %d Partida perdida: %d Reset: %d\n\r", cuenta_descendente, partida_terminada, partida_perdida, button_output);
tzuran 0:863c53b79de8 91 printf("Cables: [%d, %d, %d, %d] Secuencia: [%d %d %d %d]\n\r", cablesDesconectados[0], cablesDesconectados[1], cablesDesconectados[2], cablesDesconectados[3], secuencia[0], secuencia[1], secuencia[2], secuencia[3]);
tzuran 0:863c53b79de8 92 mandar = 0;
tzuran 0:863c53b79de8 93 }
tzuran 0:863c53b79de8 94
tzuran 0:863c53b79de8 95 }
tzuran 0:863c53b79de8 96 ME_GENERAL();
tzuran 0:863c53b79de8 97
tzuran 0:863c53b79de8 98 //PRENDIDO DE LOS DISPLAYS
tzuran 0:863c53b79de8 99 numeros_bcd(cuenta_descendente);
tzuran 0:863c53b79de8 100 }
tzuran 0:863c53b79de8 101 }
tzuran 0:863c53b79de8 102
tzuran 0:863c53b79de8 103
tzuran 0:863c53b79de8 104 //GENERACION DE SECUENCIA ALEATORIA
tzuran 0:863c53b79de8 105 void secuencia_aleatoria()
tzuran 0:863c53b79de8 106 {
tzuran 0:863c53b79de8 107 secuencia[0] = rand() % 4 + 1;
tzuran 0:863c53b79de8 108 for(int i = 1; i < 4; i++) {
tzuran 0:863c53b79de8 109 int randm = rand() % 4 + 1;
tzuran 0:863c53b79de8 110 int aux = 1;
tzuran 0:863c53b79de8 111 for(int j = 0; j < i; j++) {
tzuran 0:863c53b79de8 112 if(secuencia[j] == randm)
tzuran 0:863c53b79de8 113 aux = 0;
tzuran 0:863c53b79de8 114 }
tzuran 0:863c53b79de8 115 if(aux) {
tzuran 0:863c53b79de8 116 secuencia[i] = randm;
tzuran 0:863c53b79de8 117 } else {
tzuran 0:863c53b79de8 118 i--;
tzuran 0:863c53b79de8 119 }
tzuran 0:863c53b79de8 120 }
tzuran 0:863c53b79de8 121 }
tzuran 0:863c53b79de8 122
tzuran 0:863c53b79de8 123 //MUESTREO DEL BOTON DE ENTRADA PARA LA ELIMINACION DEL REBOTE
tzuran 0:863c53b79de8 124 void button_in()
tzuran 0:863c53b79de8 125 {
tzuran 0:863c53b79de8 126
tzuran 0:863c53b79de8 127 state1 = state1 >> 1;
tzuran 0:863c53b79de8 128
tzuran 0:863c53b79de8 129 if(!restartButton) {
tzuran 0:863c53b79de8 130 state1 |= 0x80;
tzuran 0:863c53b79de8 131 }
tzuran 0:863c53b79de8 132 if(state1 == 0xFF)
tzuran 0:863c53b79de8 133 button_output = 1;
tzuran 0:863c53b79de8 134 else if(state1 == 0)
tzuran 0:863c53b79de8 135 button_output = 0;
tzuran 0:863c53b79de8 136 }
tzuran 0:863c53b79de8 137
tzuran 0:863c53b79de8 138 //TICKER 1 seg
tzuran 0:863c53b79de8 139 void cuenta_seg()
tzuran 0:863c53b79de8 140 {
tzuran 0:863c53b79de8 141 mandar = 1; //FLAG PARA MANDAR DATOS
tzuran 0:863c53b79de8 142
tzuran 0:863c53b79de8 143 //DESCUENTO
tzuran 0:863c53b79de8 144 if(cuenta_descendente > 0 && partida_terminada == 0 && ME_GENERAL_ESTADO != PAUSA)
tzuran 0:863c53b79de8 145 cuenta_descendente--;
tzuran 0:863c53b79de8 146
tzuran 0:863c53b79de8 147 //CAMBIO ENTRE PRENDIDO Y APAGADO EL LED ROJO
tzuran 0:863c53b79de8 148 if(estado_parpadeo && partida_perdida) {
tzuran 0:863c53b79de8 149 RGB_r = 0;
tzuran 0:863c53b79de8 150 } else {
tzuran 0:863c53b79de8 151 RGB_r = 1;
tzuran 0:863c53b79de8 152 }
tzuran 0:863c53b79de8 153
tzuran 0:863c53b79de8 154 estado_parpadeo = !estado_parpadeo;
tzuran 0:863c53b79de8 155 }
tzuran 0:863c53b79de8 156
tzuran 0:863c53b79de8 157 void ME_GENERAL()
tzuran 0:863c53b79de8 158 {
tzuran 0:863c53b79de8 159 switch(ME_GENERAL_ESTADO) {
tzuran 0:863c53b79de8 160 case PAUSA:
tzuran 0:863c53b79de8 161 if(button_output == 1) {
tzuran 0:863c53b79de8 162 secuencia_aleatoria();
tzuran 0:863c53b79de8 163 ME_GENERAL_ESTADO = INICIO;
tzuran 0:863c53b79de8 164 }
tzuran 0:863c53b79de8 165 break;
tzuran 0:863c53b79de8 166 case INICIO:
tzuran 0:863c53b79de8 167 RGB_r = 1;
tzuran 0:863c53b79de8 168 RGB_g = 1;
tzuran 0:863c53b79de8 169 //RESETEO VARIABLES DE PARTIDA
tzuran 0:863c53b79de8 170 n = 0;
tzuran 0:863c53b79de8 171 partida_perdida = 0;
tzuran 0:863c53b79de8 172 partida_terminada = 0;
tzuran 0:863c53b79de8 173 cuenta_descendente = 60;
tzuran 0:863c53b79de8 174 for(int i = 0; i < 4; i++)
tzuran 0:863c53b79de8 175 cablesDesconectados[i] = 0;
tzuran 0:863c53b79de8 176
tzuran 0:863c53b79de8 177 ME_GENERAL_ESTADO = COMPARO_CABLE_1;
tzuran 0:863c53b79de8 178 break;
tzuran 0:863c53b79de8 179 case COMPARO_CABLE_1:
tzuran 0:863c53b79de8 180 //DETECTO SI SE DESCONECTO UN PRIMER CABLE Y COMPARO SI ES EL PRIMERO DE LA SECUENCIA ALEATORIA
tzuran 0:863c53b79de8 181 if(cablesDesconectados[0] != 0) {
tzuran 0:863c53b79de8 182 if(cablesDesconectados[0] == secuencia[0]) {
tzuran 0:863c53b79de8 183 ME_GENERAL_ESTADO = COMPARO_CABLE_2;
tzuran 0:863c53b79de8 184 } else {
tzuran 0:863c53b79de8 185 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 186 }
tzuran 0:863c53b79de8 187 }
tzuran 0:863c53b79de8 188 //SI SE ACABA EL TIEMPO SE VA AL ESTADO DE PERDISTE
tzuran 0:863c53b79de8 189 if(cuenta_descendente <= 0)
tzuran 0:863c53b79de8 190 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 191 break;
tzuran 0:863c53b79de8 192 case COMPARO_CABLE_2:
tzuran 0:863c53b79de8 193 if(cablesDesconectados[1] != 0) {
tzuran 0:863c53b79de8 194 if(cablesDesconectados[1] == secuencia[1]) {
tzuran 0:863c53b79de8 195 ME_GENERAL_ESTADO = COMPARO_CABLE_3;
tzuran 0:863c53b79de8 196 } else {
tzuran 0:863c53b79de8 197 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 198 }
tzuran 0:863c53b79de8 199 }
tzuran 0:863c53b79de8 200 if(cuenta_descendente <= 0)
tzuran 0:863c53b79de8 201 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 202 break;
tzuran 0:863c53b79de8 203 case COMPARO_CABLE_3:
tzuran 0:863c53b79de8 204 if(cablesDesconectados[2] != 0) {
tzuran 0:863c53b79de8 205 if(cablesDesconectados[2] == secuencia[2]) {
tzuran 0:863c53b79de8 206 ME_GENERAL_ESTADO = COMPARO_CABLE_4;
tzuran 0:863c53b79de8 207 } else {
tzuran 0:863c53b79de8 208 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 209 }
tzuran 0:863c53b79de8 210 }
tzuran 0:863c53b79de8 211 if(cuenta_descendente <= 0)
tzuran 0:863c53b79de8 212 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 213 break;
tzuran 0:863c53b79de8 214 case COMPARO_CABLE_4:
tzuran 0:863c53b79de8 215 if(cablesDesconectados[3] != 0) {
tzuran 0:863c53b79de8 216 if(cablesDesconectados[3] == secuencia[3]) {
tzuran 0:863c53b79de8 217 ME_GENERAL_ESTADO = GANASTE;
tzuran 0:863c53b79de8 218 } else {
tzuran 0:863c53b79de8 219 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 220 }
tzuran 0:863c53b79de8 221 }
tzuran 0:863c53b79de8 222 if(cuenta_descendente <= 0)
tzuran 0:863c53b79de8 223 ME_GENERAL_ESTADO = PERDISTE;
tzuran 0:863c53b79de8 224 break;
tzuran 0:863c53b79de8 225 case GANASTE:
tzuran 0:863c53b79de8 226 //SE PRENDE EL LED VERDE
tzuran 0:863c53b79de8 227 RGB_g = 0;
tzuran 0:863c53b79de8 228
tzuran 0:863c53b79de8 229 //FLAG PARA EL PARPADEO
tzuran 0:863c53b79de8 230 partida_terminada = 1;
tzuran 0:863c53b79de8 231
tzuran 0:863c53b79de8 232
tzuran 0:863c53b79de8 233 if(button_output == 1) {
tzuran 0:863c53b79de8 234 secuencia_aleatoria();
tzuran 0:863c53b79de8 235 ME_GENERAL_ESTADO = INICIO;
tzuran 0:863c53b79de8 236 }
tzuran 0:863c53b79de8 237 break;
tzuran 0:863c53b79de8 238 case PERDISTE:
tzuran 0:863c53b79de8 239 //FLAGS PARA EL PARPADEO
tzuran 0:863c53b79de8 240 partida_perdida = 1;
tzuran 0:863c53b79de8 241 partida_terminada = 1;
tzuran 0:863c53b79de8 242 if(button_output == 1) {
tzuran 0:863c53b79de8 243 secuencia_aleatoria();
tzuran 0:863c53b79de8 244 ME_GENERAL_ESTADO = INICIO;
tzuran 0:863c53b79de8 245 }
tzuran 0:863c53b79de8 246 break;
tzuran 0:863c53b79de8 247 }
tzuran 0:863c53b79de8 248 }
tzuran 0:863c53b79de8 249
tzuran 0:863c53b79de8 250 //FUNCION QUE LLENA EL VECTOR cablesDesconectados CON EL ORDEN Y NUMERO(IDENTIFICACION) DE CABLES DESCONECTADOS
tzuran 0:863c53b79de8 251 int cableDesconectado()
tzuran 0:863c53b79de8 252 {
tzuran 0:863c53b79de8 253 if(cable_1 == 1 && cablesPasados(1) == 0) {
tzuran 0:863c53b79de8 254 cablesDesconectados[n] = 1;
tzuran 0:863c53b79de8 255 n++;
tzuran 0:863c53b79de8 256 return 1;
tzuran 0:863c53b79de8 257 } else if(cable_2 == 1 && cablesPasados(2) == 0) {
tzuran 0:863c53b79de8 258 cablesDesconectados[n] = 2;
tzuran 0:863c53b79de8 259 n++;
tzuran 0:863c53b79de8 260 return 2;
tzuran 0:863c53b79de8 261 } else if(cable_3 == 1 && cablesPasados(3) == 0) {
tzuran 0:863c53b79de8 262 cablesDesconectados[n] = 3;
tzuran 0:863c53b79de8 263 n++;
tzuran 0:863c53b79de8 264 return 3;
tzuran 0:863c53b79de8 265 } else if(cable_4 == 1 && cablesPasados(4) == 0) {
tzuran 0:863c53b79de8 266 cablesDesconectados[n] = 4;
tzuran 0:863c53b79de8 267 n++;
tzuran 0:863c53b79de8 268 return 4;
tzuran 0:863c53b79de8 269 } else {
tzuran 0:863c53b79de8 270 return 0;
tzuran 0:863c53b79de8 271 }
tzuran 0:863c53b79de8 272 }
tzuran 0:863c53b79de8 273
tzuran 0:863c53b79de8 274 //DEVUELVE UN 1 SI n SE HAYA EN cablesDesconectados
tzuran 0:863c53b79de8 275 int cablesPasados(int n)
tzuran 0:863c53b79de8 276 {
tzuran 0:863c53b79de8 277 for(int i = 0; i < 4; i++) {
tzuran 0:863c53b79de8 278 if(cablesDesconectados[i] == n) {
tzuran 0:863c53b79de8 279 return 1;
tzuran 0:863c53b79de8 280 }
tzuran 0:863c53b79de8 281 }
tzuran 0:863c53b79de8 282
tzuran 0:863c53b79de8 283 return 0;
tzuran 0:863c53b79de8 284 }
tzuran 0:863c53b79de8 285
tzuran 0:863c53b79de8 286 //FUNCION QUE PONE AL NUMERO n EN LOS DISPLAYS DE 7 SEGMENTOS
tzuran 0:863c53b79de8 287 void numeros_bcd(int n)
tzuran 0:863c53b79de8 288 {
tzuran 0:863c53b79de8 289 char un = n % 10;
tzuran 0:863c53b79de8 290 char dc = n / 10;
tzuran 0:863c53b79de8 291
tzuran 0:863c53b79de8 292 if(estado_parpadeo && partida_terminada) {
tzuran 0:863c53b79de8 293 unidades = 0xF;
tzuran 0:863c53b79de8 294 decenas = 0xF;
tzuran 0:863c53b79de8 295 } else {
tzuran 0:863c53b79de8 296 unidades = un;
tzuran 0:863c53b79de8 297 decenas = dc;
tzuran 0:863c53b79de8 298 }
tzuran 0:863c53b79de8 299
tzuran 0:863c53b79de8 300 }