Ejercicio realizado por: HU, Julian y LOPEZ, Gabriel.

Dependencies:   mbed

Committer:
GabiLopez
Date:
Wed Jun 26 16:00:03 2019 +0000
Revision:
1:06553fdedc7e
Parent:
0:ff8d1aea6fb4
Child:
2:2aa02cd60b0b
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GabiLopez 1:06553fdedc7e 1 //Incluyo las librerias a utilizar.
GabiLopez 0:ff8d1aea6fb4 2 #include "mbed.h"
GabiLopez 0:ff8d1aea6fb4 3
GabiLopez 1:06553fdedc7e 4 //Se cambia el nombre de "0" y de "1" por NO, SI, ON y OFF para que el progama sea mas facil de leer.
GabiLopez 1:06553fdedc7e 5 #define NO 0
GabiLopez 1:06553fdedc7e 6 #define SI 1
GabiLopez 1:06553fdedc7e 7 #define ON 1
GabiLopez 1:06553fdedc7e 8 #define OFF 0
GabiLopez 1:06553fdedc7e 9
GabiLopez 1:06553fdedc7e 10 //Lo mismo que en el caso anterior, pero con el led.
GabiLopez 1:06553fdedc7e 11 #define LED_ON 0
GabiLopez 1:06553fdedc7e 12 #define LED_OFF 1
GabiLopez 0:ff8d1aea6fb4 13
GabiLopez 1:06553fdedc7e 14 //Lo mismo que en el caso anterior, pero con el pulsador.
GabiLopez 1:06553fdedc7e 15 #define PULS_ON 0
GabiLopez 1:06553fdedc7e 16 #define PULS_OFF 1
GabiLopez 1:06553fdedc7e 17
GabiLopez 1:06553fdedc7e 18 //Se definen las entradas digitales para los tres pulsadores.
GabiLopez 1:06553fdedc7e 19 DigitalIn pulsador_1 (PTC12);
GabiLopez 1:06553fdedc7e 20 DigitalIn pulsador_2 (PTC13);
GabiLopez 1:06553fdedc7e 21 DigitalIn pulsador_3 (PTC16);
GabiLopez 1:06553fdedc7e 22
GabiLopez 1:06553fdedc7e 23 //Se definen como salidas digitales los 3 leds para podes jugar.
GabiLopez 0:ff8d1aea6fb4 24 DigitalOut ledrojo(LED_RED);
GabiLopez 0:ff8d1aea6fb4 25 DigitalOut ledazul(LED_BLUE);
GabiLopez 0:ff8d1aea6fb4 26 DigitalOut ledverde(LED_GREEN);
GabiLopez 0:ff8d1aea6fb4 27
GabiLopez 1:06553fdedc7e 28 //Se definen todos los Tickers.
GabiLopez 1:06553fdedc7e 29 Ticker pulsos;
GabiLopez 0:ff8d1aea6fb4 30 Ticker aviso;
GabiLopez 1:06553fdedc7e 31 Ticker colores;
GabiLopez 1:06553fdedc7e 32 Ticker ti;
GabiLopez 1:06553fdedc7e 33 Ticker random;
GabiLopez 0:ff8d1aea6fb4 34
GabiLopez 1:06553fdedc7e 35 //Se definen todos los prototipos de las funciones.
GabiLopez 1:06553fdedc7e 36 void pulsador();
GabiLopez 1:06553fdedc7e 37 void timer();
GabiLopez 1:06553fdedc7e 38 void detector();
GabiLopez 1:06553fdedc7e 39 void muestra();
GabiLopez 1:06553fdedc7e 40 void mostrar();
GabiLopez 0:ff8d1aea6fb4 41 void intermitencia();
GabiLopez 1:06553fdedc7e 42 void obtengorandom();
GabiLopez 0:ff8d1aea6fb4 43
GabiLopez 1:06553fdedc7e 44 //Se definen todas las variables a utilizar
GabiLopez 1:06553fdedc7e 45 uint8_t secuencia[25], turno=0,toff=0; //Estas son uint8_t para ocupar menos espacio
GabiLopez 1:06553fdedc7e 46 uint8_t juego=OFF,a=0,empieza=NO,ocupado=NO,interaccion=NO; //Estas son uint8_t para ocupar menos espacio
GabiLopez 1:06553fdedc7e 47 uint8_t pulso_1=OFF,pulso_2=OFF,pulso_3=OFF; //Estas son uint8_t para ocupar menos espacio
GabiLopez 1:06553fdedc7e 48 int tiempo=0, valorrandom;
GabiLopez 0:ff8d1aea6fb4 49
GabiLopez 0:ff8d1aea6fb4 50 int main()
GabiLopez 0:ff8d1aea6fb4 51 {
GabiLopez 1:06553fdedc7e 52 //Habilito las interrupciones.
GabiLopez 0:ff8d1aea6fb4 53 __enable_irq();
GabiLopez 1:06553fdedc7e 54 //Defino la semilla para los numero random.
GabiLopez 1:06553fdedc7e 55 srand(valorrandom);
GabiLopez 1:06553fdedc7e 56 //Llamo a la funcion obtengorandom cada 10ms.
GabiLopez 1:06553fdedc7e 57 random.attach(&obtengorandom,0.01);
GabiLopez 1:06553fdedc7e 58 //Activo las R Pull-UP internas de los 3 pulsadores.
GabiLopez 1:06553fdedc7e 59 pulsador_1.mode(PullUp);
GabiLopez 1:06553fdedc7e 60 pulsador_2.mode(PullUp);
GabiLopez 1:06553fdedc7e 61 pulsador_3.mode(PullUp);
GabiLopez 1:06553fdedc7e 62 //Llamo a la funcion pulsador cada 10ms.
GabiLopez 1:06553fdedc7e 63 pulsos.attach(&pulsador,0.01);
GabiLopez 1:06553fdedc7e 64 //Al empezar el programa, se apagan todos los leds.
GabiLopez 1:06553fdedc7e 65 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 66 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 67 ledazul = LED_OFF;
GabiLopez 0:ff8d1aea6fb4 68
GabiLopez 1:06553fdedc7e 69 while (true) {
GabiLopez 1:06553fdedc7e 70 if (juego == OFF) {
GabiLopez 1:06553fdedc7e 71 if (a == 0) {
GabiLopez 1:06553fdedc7e 72 aviso.attach(&intermitencia, 1);
GabiLopez 1:06553fdedc7e 73 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 74 printf("------------------------ATENCION------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 75 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 76 printf(" Presione cualquier pulsador para comenzar \n"); //Decoracion
GabiLopez 1:06553fdedc7e 77 a = 1;
GabiLopez 1:06553fdedc7e 78 }
GabiLopez 1:06553fdedc7e 79 if (pulso_2 == ON || pulso_1 == ON || pulso_3 == ON) {
GabiLopez 1:06553fdedc7e 80 pulso_1 = OFF;
GabiLopez 1:06553fdedc7e 81 pulso_3 = OFF;
GabiLopez 1:06553fdedc7e 82 pulso_2 = OFF;
GabiLopez 1:06553fdedc7e 83 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 84 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 85 ledverde = LED_OFF;
GabiLopez 1:06553fdedc7e 86 juego = ON;
GabiLopez 1:06553fdedc7e 87 }
GabiLopez 1:06553fdedc7e 88 }
GabiLopez 1:06553fdedc7e 89 if (turno == 25 && ocupado == NO) {
GabiLopez 1:06553fdedc7e 90 printf("Congratulations, you won the game!");
GabiLopez 1:06553fdedc7e 91 a = 0,turno = 0;
GabiLopez 1:06553fdedc7e 92 juego = OFF,empieza = NO,ocupado = NO;
GabiLopez 1:06553fdedc7e 93 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 94 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 95 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 96 }
GabiLopez 1:06553fdedc7e 97 if (juego == ON && empieza == SI && ocupado == NO && turno < 25) {
GabiLopez 1:06553fdedc7e 98 secuencia[turno] = rand () % 3;
GabiLopez 1:06553fdedc7e 99 turno++;
GabiLopez 1:06553fdedc7e 100 ocupado = SI;
GabiLopez 1:06553fdedc7e 101 colores.attach(&mostrar,0.1);
GabiLopez 1:06553fdedc7e 102 }
GabiLopez 1:06553fdedc7e 103 if (interaccion == SI) {
GabiLopez 1:06553fdedc7e 104 pulsos.attach(&pulsador,0.01);
GabiLopez 1:06553fdedc7e 105 ti.attach(&timer,0.01);
GabiLopez 1:06553fdedc7e 106 tiempo = 1001;
GabiLopez 1:06553fdedc7e 107 interaccion = NO;
GabiLopez 0:ff8d1aea6fb4 108 }
GabiLopez 0:ff8d1aea6fb4 109 }
GabiLopez 1:06553fdedc7e 110 }
GabiLopez 1:06553fdedc7e 111 void timer()
GabiLopez 1:06553fdedc7e 112 {
GabiLopez 1:06553fdedc7e 113 static uint8_t c=0;
GabiLopez 1:06553fdedc7e 114 tiempo--;
GabiLopez 1:06553fdedc7e 115 if(pulso_2 == ON && secuencia[c] == 1) {
GabiLopez 1:06553fdedc7e 116 //printf("correcto\n");
GabiLopez 1:06553fdedc7e 117 pulso_2 = OFF;
GabiLopez 1:06553fdedc7e 118 ledverde = LED_OFF;
GabiLopez 1:06553fdedc7e 119 tiempo = 1001;
GabiLopez 1:06553fdedc7e 120 c++;
GabiLopez 1:06553fdedc7e 121 }
GabiLopez 1:06553fdedc7e 122 if(pulso_1 == ON && secuencia[c] == 0) {
GabiLopez 1:06553fdedc7e 123 //printf("correcto\n");
GabiLopez 1:06553fdedc7e 124 pulso_1 = OFF;
GabiLopez 1:06553fdedc7e 125 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 126 tiempo = 1001;
GabiLopez 1:06553fdedc7e 127 c++;
GabiLopez 1:06553fdedc7e 128 }
GabiLopez 1:06553fdedc7e 129 if(pulso_3 == ON && secuencia[c] == 2) {
GabiLopez 1:06553fdedc7e 130 //printf("correcto\n");
GabiLopez 1:06553fdedc7e 131 pulso_3 = OFF;
GabiLopez 1:06553fdedc7e 132 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 133 tiempo = 1001;
GabiLopez 1:06553fdedc7e 134 c++;
GabiLopez 1:06553fdedc7e 135 }
GabiLopez 1:06553fdedc7e 136 if (c == turno) {
GabiLopez 1:06553fdedc7e 137 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 138 printf("--------------------Nivel %d superado-------------------\n",c); //Decoracion
GabiLopez 1:06553fdedc7e 139 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 0:ff8d1aea6fb4 140
GabiLopez 1:06553fdedc7e 141 ocupado=NO;
GabiLopez 1:06553fdedc7e 142 pulsos.detach();
GabiLopez 1:06553fdedc7e 143 ti.detach();
GabiLopez 1:06553fdedc7e 144 c=0;
GabiLopez 1:06553fdedc7e 145 toff=6;
GabiLopez 0:ff8d1aea6fb4 146 }
GabiLopez 1:06553fdedc7e 147 if (pulso_3 == ON && secuencia[c] != 2 ||
GabiLopez 1:06553fdedc7e 148 pulso_1 == ON && secuencia[c] != 0 ||
GabiLopez 1:06553fdedc7e 149 pulso_2 == ON && secuencia[c] != 1 ||
GabiLopez 1:06553fdedc7e 150 tiempo==0) {
GabiLopez 1:06553fdedc7e 151 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 152 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 153 printf("-----------------------YOU LOSE-------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 154 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 155 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 0:ff8d1aea6fb4 156
GabiLopez 1:06553fdedc7e 157 ti.detach();
GabiLopez 1:06553fdedc7e 158 a = 0,turno = 0;
GabiLopez 1:06553fdedc7e 159 juego = OFF,empieza = NO,ocupado = NO;
GabiLopez 1:06553fdedc7e 160 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 161 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 162 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 163 pulso_1 = OFF;
GabiLopez 1:06553fdedc7e 164 pulso_2 = OFF;
GabiLopez 1:06553fdedc7e 165 pulso_3 = OFF;
GabiLopez 0:ff8d1aea6fb4 166 }
GabiLopez 0:ff8d1aea6fb4 167 }
GabiLopez 0:ff8d1aea6fb4 168
GabiLopez 1:06553fdedc7e 169 void mostrar()
GabiLopez 0:ff8d1aea6fb4 170 {
GabiLopez 1:06553fdedc7e 171 muestra();
GabiLopez 0:ff8d1aea6fb4 172 }
GabiLopez 0:ff8d1aea6fb4 173
GabiLopez 1:06553fdedc7e 174 void muestra()
GabiLopez 0:ff8d1aea6fb4 175 {
GabiLopez 1:06553fdedc7e 176 static uint8_t i=0,ton=0;
GabiLopez 1:06553fdedc7e 177 uint8_t color;//tf=turno+1;
GabiLopez 1:06553fdedc7e 178 if (ton == 0 && toff == 0) {
GabiLopez 1:06553fdedc7e 179 if (i == turno) {
GabiLopez 1:06553fdedc7e 180 colores.detach();
GabiLopez 1:06553fdedc7e 181 interaccion = SI;
GabiLopez 1:06553fdedc7e 182 i = 99;
GabiLopez 1:06553fdedc7e 183 }
GabiLopez 1:06553fdedc7e 184 color=secuencia[i];
GabiLopez 1:06553fdedc7e 185 if (i == 99)
GabiLopez 1:06553fdedc7e 186 color = 3;
GabiLopez 1:06553fdedc7e 187 switch (color) {
GabiLopez 1:06553fdedc7e 188 case 3:
GabiLopez 1:06553fdedc7e 189 i = 99;
GabiLopez 1:06553fdedc7e 190 break;
GabiLopez 1:06553fdedc7e 191 case 0:
GabiLopez 1:06553fdedc7e 192 ledrojo = LED_ON;
GabiLopez 1:06553fdedc7e 193 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 194 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 195 ton=11;
GabiLopez 1:06553fdedc7e 196 break;
GabiLopez 1:06553fdedc7e 197
GabiLopez 1:06553fdedc7e 198 case 1:
GabiLopez 1:06553fdedc7e 199 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 200 ledverde= LED_ON;
GabiLopez 1:06553fdedc7e 201 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 202 ton=11;
GabiLopez 1:06553fdedc7e 203 break;
GabiLopez 1:06553fdedc7e 204
GabiLopez 1:06553fdedc7e 205 case 2:
GabiLopez 1:06553fdedc7e 206 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 207 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 208 ledazul = LED_ON;
GabiLopez 1:06553fdedc7e 209 ton=11;
GabiLopez 1:06553fdedc7e 210 break;
GabiLopez 1:06553fdedc7e 211 }//switch
GabiLopez 1:06553fdedc7e 212 i++;
GabiLopez 1:06553fdedc7e 213 if(i == 100)
GabiLopez 1:06553fdedc7e 214 i = 0;
GabiLopez 1:06553fdedc7e 215 }//if ton/toff=0
GabiLopez 1:06553fdedc7e 216 if (ton > 0) {
GabiLopez 1:06553fdedc7e 217 ton--;
GabiLopez 1:06553fdedc7e 218 toff = 6;
GabiLopez 0:ff8d1aea6fb4 219 }
GabiLopez 1:06553fdedc7e 220 if (ton == 0 && toff > 0) {
GabiLopez 1:06553fdedc7e 221 toff--;
GabiLopez 1:06553fdedc7e 222 ledrojo = LED_OFF;
GabiLopez 1:06553fdedc7e 223 ledverde= LED_OFF;
GabiLopez 1:06553fdedc7e 224 ledazul = LED_OFF;
GabiLopez 1:06553fdedc7e 225 }
GabiLopez 1:06553fdedc7e 226 }//final
GabiLopez 1:06553fdedc7e 227 void pulsador()
GabiLopez 1:06553fdedc7e 228 {
GabiLopez 1:06553fdedc7e 229 detector();
GabiLopez 1:06553fdedc7e 230 }
GabiLopez 1:06553fdedc7e 231 void detector()
GabiLopez 1:06553fdedc7e 232 {
GabiLopez 1:06553fdedc7e 233 static uint8_t verificacion_1=OFF,verificacion_2=OFF,verificacion_3=OFF, antirebote=0;
GabiLopez 1:06553fdedc7e 234 //pulsador 1
GabiLopez 1:06553fdedc7e 235 if (pulsador_1 == PULS_ON && pulso_1 == OFF && verificacion_1 == OFF) {
GabiLopez 1:06553fdedc7e 236 antirebote = 4;
GabiLopez 1:06553fdedc7e 237 verificacion_1 = ON;
GabiLopez 1:06553fdedc7e 238 ledrojo = LED_ON;
GabiLopez 1:06553fdedc7e 239 }
GabiLopez 1:06553fdedc7e 240 //pulsador 2
GabiLopez 1:06553fdedc7e 241 if (pulsador_2 == PULS_ON && pulso_2 == OFF && verificacion_2 == OFF) {
GabiLopez 1:06553fdedc7e 242 antirebote = 4;
GabiLopez 1:06553fdedc7e 243 verificacion_2 = ON;
GabiLopez 1:06553fdedc7e 244 ledverde= LED_ON;
GabiLopez 0:ff8d1aea6fb4 245 }
GabiLopez 1:06553fdedc7e 246 //pulsador 3
GabiLopez 1:06553fdedc7e 247 if (pulsador_3 == PULS_ON && pulso_3 == OFF && verificacion_3 == OFF) {
GabiLopez 1:06553fdedc7e 248 antirebote = 4;
GabiLopez 1:06553fdedc7e 249 verificacion_3 = ON;
GabiLopez 1:06553fdedc7e 250 ledazul = LED_ON;
GabiLopez 0:ff8d1aea6fb4 251 }
GabiLopez 1:06553fdedc7e 252 if (antirebote==0) {
GabiLopez 1:06553fdedc7e 253 if (pulsador_1 == PULS_OFF && verificacion_1 == ON) {
GabiLopez 1:06553fdedc7e 254 verificacion_1 = OFF;
GabiLopez 1:06553fdedc7e 255 pulso_1 = ON;
GabiLopez 1:06553fdedc7e 256 //printf("pulsador 1 on\n");
GabiLopez 1:06553fdedc7e 257 }
GabiLopez 1:06553fdedc7e 258 if (pulsador_2 == PULS_OFF && verificacion_2 == ON) {
GabiLopez 1:06553fdedc7e 259 verificacion_2 = OFF;
GabiLopez 1:06553fdedc7e 260 pulso_2 = ON;
GabiLopez 1:06553fdedc7e 261 //printf("pulsador 2 on\n");
GabiLopez 1:06553fdedc7e 262 }
GabiLopez 1:06553fdedc7e 263 if (pulsador_3 == PULS_OFF && verificacion_3 == ON) {
GabiLopez 1:06553fdedc7e 264 verificacion_3 = OFF;
GabiLopez 1:06553fdedc7e 265 pulso_3 = ON;
GabiLopez 1:06553fdedc7e 266 //printf("pulsador 3 on\n");
GabiLopez 1:06553fdedc7e 267 }
GabiLopez 1:06553fdedc7e 268 }
GabiLopez 1:06553fdedc7e 269 if (antirebote>0)
GabiLopez 1:06553fdedc7e 270 antirebote--;
GabiLopez 0:ff8d1aea6fb4 271 }
GabiLopez 0:ff8d1aea6fb4 272
GabiLopez 0:ff8d1aea6fb4 273 void intermitencia()
GabiLopez 0:ff8d1aea6fb4 274 {
GabiLopez 1:06553fdedc7e 275 if (juego == OFF) {
GabiLopez 1:06553fdedc7e 276 ledrojo = !ledrojo;
GabiLopez 1:06553fdedc7e 277 ledazul = !ledazul;
GabiLopez 1:06553fdedc7e 278 ledverde= !ledverde;
GabiLopez 0:ff8d1aea6fb4 279 }
GabiLopez 1:06553fdedc7e 280 if (juego == ON) {
GabiLopez 1:06553fdedc7e 281 printf("--------------------------------------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 282 printf("-----------------------ATENCION-------------------------\n"); //Decoracion
GabiLopez 1:06553fdedc7e 283 printf(" QUE COMIENCE EL JUEGO \n"); //Decoracion
GabiLopez 1:06553fdedc7e 284 empieza = SI;
GabiLopez 0:ff8d1aea6fb4 285 aviso.detach();
GabiLopez 1:06553fdedc7e 286 pulsos.detach();
GabiLopez 0:ff8d1aea6fb4 287 }
GabiLopez 0:ff8d1aea6fb4 288 }
GabiLopez 0:ff8d1aea6fb4 289
GabiLopez 1:06553fdedc7e 290 void obtengorandom ()
GabiLopez 1:06553fdedc7e 291 {
GabiLopez 1:06553fdedc7e 292 valorrandom++;
GabiLopez 1:06553fdedc7e 293 srand(valorrandom);
GabiLopez 1:06553fdedc7e 294 }