Juan Ignacio Cobas
/
TP_SIMON_FINAL
TP 1 Ejer 2 simon Cobas, Montero
main.cpp@0:694030aeaef8, 2019-05-21 (annotated)
- Committer:
- cobasjuan
- Date:
- Tue May 21 13:44:17 2019 +0000
- Revision:
- 0:694030aeaef8
- Child:
- 1:2ec64607a2a4
Funciona sin rand
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cobasjuan | 0:694030aeaef8 | 1 | #include "mbed.h" |
cobasjuan | 0:694030aeaef8 | 2 | #include "tsi_sensor.h" |
cobasjuan | 0:694030aeaef8 | 3 | |
cobasjuan | 0:694030aeaef8 | 4 | /* This defines will be replaced by PinNames soon */ |
cobasjuan | 0:694030aeaef8 | 5 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
cobasjuan | 0:694030aeaef8 | 6 | #define ELEC0 9 |
cobasjuan | 0:694030aeaef8 | 7 | #define ELEC1 10 |
cobasjuan | 0:694030aeaef8 | 8 | #elif defined (TARGET_KL05Z) |
cobasjuan | 0:694030aeaef8 | 9 | #define ELEC0 9 |
cobasjuan | 0:694030aeaef8 | 10 | #define ELEC1 8 |
cobasjuan | 0:694030aeaef8 | 11 | #else |
cobasjuan | 0:694030aeaef8 | 12 | #error TARGET NOT DEFINED |
cobasjuan | 0:694030aeaef8 | 13 | #endif |
cobasjuan | 0:694030aeaef8 | 14 | |
cobasjuan | 0:694030aeaef8 | 15 | #define ESPERAR_A 300 |
cobasjuan | 0:694030aeaef8 | 16 | #define MOSTRAR 301 |
cobasjuan | 0:694030aeaef8 | 17 | #define ESPERO_JUG 302 |
cobasjuan | 0:694030aeaef8 | 18 | #define MUESTREO 303 |
cobasjuan | 0:694030aeaef8 | 19 | #define ESPERAR_B 304 |
cobasjuan | 0:694030aeaef8 | 20 | #define ESPERAR_C 305 |
cobasjuan | 0:694030aeaef8 | 21 | #define JUGANDO 306 |
cobasjuan | 0:694030aeaef8 | 22 | |
cobasjuan | 0:694030aeaef8 | 23 | Ticker timer; |
cobasjuan | 0:694030aeaef8 | 24 | void t(); |
cobasjuan | 0:694030aeaef8 | 25 | |
cobasjuan | 0:694030aeaef8 | 26 | DigitalOut ledr(LED1); |
cobasjuan | 0:694030aeaef8 | 27 | DigitalOut ledg(LED2); |
cobasjuan | 0:694030aeaef8 | 28 | DigitalOut ledb(LED3); |
cobasjuan | 0:694030aeaef8 | 29 | |
cobasjuan | 0:694030aeaef8 | 30 | |
cobasjuan | 0:694030aeaef8 | 31 | void GENERAL(); |
cobasjuan | 0:694030aeaef8 | 32 | void MOSTRAR_LED(); |
cobasjuan | 0:694030aeaef8 | 33 | void JUEGO(); |
cobasjuan | 0:694030aeaef8 | 34 | void PULSADORES(); |
cobasjuan | 0:694030aeaef8 | 35 | |
cobasjuan | 0:694030aeaef8 | 36 | int secuencia[20]= {1,2,3,2,2,3,1,3,1,2,3,1,2,3,1,2,3}; |
cobasjuan | 0:694030aeaef8 | 37 | int cant=0, finsec=0, n=0, P1=0, P2=0, P3=0; |
cobasjuan | 0:694030aeaef8 | 38 | int habmostrar=0, habjug=0, fin_mostrar=0, finjug=0, perdiste=0; |
cobasjuan | 0:694030aeaef8 | 39 | volatile unsigned int tiempo=100; |
cobasjuan | 0:694030aeaef8 | 40 | static int traba=0, principio=0; |
cobasjuan | 0:694030aeaef8 | 41 | |
cobasjuan | 0:694030aeaef8 | 42 | TSIAnalogSlider tsi(ELEC0, ELEC1, 40); |
cobasjuan | 0:694030aeaef8 | 43 | |
cobasjuan | 0:694030aeaef8 | 44 | int main() |
cobasjuan | 0:694030aeaef8 | 45 | { |
cobasjuan | 0:694030aeaef8 | 46 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 47 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 48 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 49 | timer.attach(&t,0.01); |
cobasjuan | 0:694030aeaef8 | 50 | while(1) { |
cobasjuan | 0:694030aeaef8 | 51 | GENERAL(); |
cobasjuan | 0:694030aeaef8 | 52 | MOSTRAR_LED(); |
cobasjuan | 0:694030aeaef8 | 53 | JUEGO(); |
cobasjuan | 0:694030aeaef8 | 54 | PULSADORES(); |
cobasjuan | 0:694030aeaef8 | 55 | } |
cobasjuan | 0:694030aeaef8 | 56 | } |
cobasjuan | 0:694030aeaef8 | 57 | |
cobasjuan | 0:694030aeaef8 | 58 | |
cobasjuan | 0:694030aeaef8 | 59 | void GENERAL() |
cobasjuan | 0:694030aeaef8 | 60 | { |
cobasjuan | 0:694030aeaef8 | 61 | static int paso_a=ESPERAR_A; |
cobasjuan | 0:694030aeaef8 | 62 | switch(paso_a) { |
cobasjuan | 0:694030aeaef8 | 63 | case ESPERAR_A: |
cobasjuan | 0:694030aeaef8 | 64 | habmostrar=0; |
cobasjuan | 0:694030aeaef8 | 65 | habjug=0; |
cobasjuan | 0:694030aeaef8 | 66 | fin_mostrar=0; |
cobasjuan | 0:694030aeaef8 | 67 | finjug=0; |
cobasjuan | 0:694030aeaef8 | 68 | perdiste=0; |
cobasjuan | 0:694030aeaef8 | 69 | cant=0; |
cobasjuan | 0:694030aeaef8 | 70 | principio=0; |
cobasjuan | 0:694030aeaef8 | 71 | if((P1 == 1 || P2 == 1 || P3 == 1) && traba == 0) { |
cobasjuan | 0:694030aeaef8 | 72 | traba=1; |
cobasjuan | 0:694030aeaef8 | 73 | paso_a=MOSTRAR; |
cobasjuan | 0:694030aeaef8 | 74 | principio++; |
cobasjuan | 0:694030aeaef8 | 75 | printf("Inicio del juego\n\r"); |
cobasjuan | 0:694030aeaef8 | 76 | } |
cobasjuan | 0:694030aeaef8 | 77 | break; |
cobasjuan | 0:694030aeaef8 | 78 | case MOSTRAR: |
cobasjuan | 0:694030aeaef8 | 79 | habmostrar=1; |
cobasjuan | 0:694030aeaef8 | 80 | finjug=0; |
cobasjuan | 0:694030aeaef8 | 81 | if(fin_mostrar==1) { |
cobasjuan | 0:694030aeaef8 | 82 | paso_a=ESPERO_JUG; |
cobasjuan | 0:694030aeaef8 | 83 | } |
cobasjuan | 0:694030aeaef8 | 84 | break; |
cobasjuan | 0:694030aeaef8 | 85 | case ESPERO_JUG: |
cobasjuan | 0:694030aeaef8 | 86 | habjug=1; |
cobasjuan | 0:694030aeaef8 | 87 | fin_mostrar=0; |
cobasjuan | 0:694030aeaef8 | 88 | if(finjug==1) { |
cobasjuan | 0:694030aeaef8 | 89 | cant++; |
cobasjuan | 0:694030aeaef8 | 90 | paso_a=MOSTRAR; |
cobasjuan | 0:694030aeaef8 | 91 | } |
cobasjuan | 0:694030aeaef8 | 92 | if(perdiste==1) { |
cobasjuan | 0:694030aeaef8 | 93 | paso_a=ESPERAR_A; |
cobasjuan | 0:694030aeaef8 | 94 | } |
cobasjuan | 0:694030aeaef8 | 95 | break; |
cobasjuan | 0:694030aeaef8 | 96 | } |
cobasjuan | 0:694030aeaef8 | 97 | } |
cobasjuan | 0:694030aeaef8 | 98 | |
cobasjuan | 0:694030aeaef8 | 99 | |
cobasjuan | 0:694030aeaef8 | 100 | void MOSTRAR_LED() |
cobasjuan | 0:694030aeaef8 | 101 | { |
cobasjuan | 0:694030aeaef8 | 102 | static int paso_b=ESPERAR_B; |
cobasjuan | 0:694030aeaef8 | 103 | switch(paso_b) { |
cobasjuan | 0:694030aeaef8 | 104 | case ESPERAR_B: |
cobasjuan | 0:694030aeaef8 | 105 | if(habmostrar==1) { |
cobasjuan | 0:694030aeaef8 | 106 | paso_b=MUESTREO; |
cobasjuan | 0:694030aeaef8 | 107 | } |
cobasjuan | 0:694030aeaef8 | 108 | break; |
cobasjuan | 0:694030aeaef8 | 109 | case MUESTREO: |
cobasjuan | 0:694030aeaef8 | 110 | if(finsec==0) { |
cobasjuan | 0:694030aeaef8 | 111 | if (tiempo == 0) { |
cobasjuan | 0:694030aeaef8 | 112 | if(cant < n) { |
cobasjuan | 0:694030aeaef8 | 113 | finsec=1; |
cobasjuan | 0:694030aeaef8 | 114 | } else if(secuencia[n]== 1) { |
cobasjuan | 0:694030aeaef8 | 115 | ledr=0; |
cobasjuan | 0:694030aeaef8 | 116 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 117 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 118 | printf("LED_ROJO\n\r"); |
cobasjuan | 0:694030aeaef8 | 119 | } else if(secuencia[n]== 2) { |
cobasjuan | 0:694030aeaef8 | 120 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 121 | ledg=0; |
cobasjuan | 0:694030aeaef8 | 122 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 123 | printf("LED_VERDE\n\r"); |
cobasjuan | 0:694030aeaef8 | 124 | } else if(secuencia[n]== 3) { |
cobasjuan | 0:694030aeaef8 | 125 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 126 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 127 | ledb=0; |
cobasjuan | 0:694030aeaef8 | 128 | printf("LED_AZUL\n\r"); |
cobasjuan | 0:694030aeaef8 | 129 | } |
cobasjuan | 0:694030aeaef8 | 130 | tiempo = 130; |
cobasjuan | 0:694030aeaef8 | 131 | n++; |
cobasjuan | 0:694030aeaef8 | 132 | } |
cobasjuan | 0:694030aeaef8 | 133 | if(tiempo == 40) { |
cobasjuan | 0:694030aeaef8 | 134 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 135 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 136 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 137 | } |
cobasjuan | 0:694030aeaef8 | 138 | } else if(finsec==1) { |
cobasjuan | 0:694030aeaef8 | 139 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 140 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 141 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 142 | fin_mostrar=1; |
cobasjuan | 0:694030aeaef8 | 143 | habmostrar=0; |
cobasjuan | 0:694030aeaef8 | 144 | paso_b=ESPERAR_B; |
cobasjuan | 0:694030aeaef8 | 145 | } |
cobasjuan | 0:694030aeaef8 | 146 | break; |
cobasjuan | 0:694030aeaef8 | 147 | } |
cobasjuan | 0:694030aeaef8 | 148 | } |
cobasjuan | 0:694030aeaef8 | 149 | |
cobasjuan | 0:694030aeaef8 | 150 | |
cobasjuan | 0:694030aeaef8 | 151 | void JUEGO() |
cobasjuan | 0:694030aeaef8 | 152 | { |
cobasjuan | 0:694030aeaef8 | 153 | static int paso_c=ESPERAR_C; |
cobasjuan | 0:694030aeaef8 | 154 | switch(paso_c) { |
cobasjuan | 0:694030aeaef8 | 155 | case ESPERAR_C: |
cobasjuan | 0:694030aeaef8 | 156 | if(habjug==1) { |
cobasjuan | 0:694030aeaef8 | 157 | paso_c=JUGANDO; |
cobasjuan | 0:694030aeaef8 | 158 | n=0; |
cobasjuan | 0:694030aeaef8 | 159 | finsec=0; |
cobasjuan | 0:694030aeaef8 | 160 | } |
cobasjuan | 0:694030aeaef8 | 161 | break; |
cobasjuan | 0:694030aeaef8 | 162 | case JUGANDO: |
cobasjuan | 0:694030aeaef8 | 163 | if(finsec == 0) { |
cobasjuan | 0:694030aeaef8 | 164 | if((P1 == 1 || P2 == 1 || P3 == 1) && traba == 0) { |
cobasjuan | 0:694030aeaef8 | 165 | traba=1; |
cobasjuan | 0:694030aeaef8 | 166 | if((secuencia[n] == 1 && P1 == 1) || (secuencia[n] == 2 && P2 == 1) || (secuencia[n] == 3 && P3 == 1)) { |
cobasjuan | 0:694030aeaef8 | 167 | n++; |
cobasjuan | 0:694030aeaef8 | 168 | } else { |
cobasjuan | 0:694030aeaef8 | 169 | printf("Game Over\n\r"); |
cobasjuan | 0:694030aeaef8 | 170 | perdiste=1; |
cobasjuan | 0:694030aeaef8 | 171 | principio=0; |
cobasjuan | 0:694030aeaef8 | 172 | habjug=0; |
cobasjuan | 0:694030aeaef8 | 173 | break; |
cobasjuan | 0:694030aeaef8 | 174 | } |
cobasjuan | 0:694030aeaef8 | 175 | if(cant < n) { |
cobasjuan | 0:694030aeaef8 | 176 | finsec=1; |
cobasjuan | 0:694030aeaef8 | 177 | printf("Enhorabuena!\n\r"); |
cobasjuan | 0:694030aeaef8 | 178 | } |
cobasjuan | 0:694030aeaef8 | 179 | } |
cobasjuan | 0:694030aeaef8 | 180 | } else if(finsec == 1) { |
cobasjuan | 0:694030aeaef8 | 181 | finjug=1; |
cobasjuan | 0:694030aeaef8 | 182 | habjug=0; |
cobasjuan | 0:694030aeaef8 | 183 | paso_c=ESPERAR_C; |
cobasjuan | 0:694030aeaef8 | 184 | } |
cobasjuan | 0:694030aeaef8 | 185 | break; |
cobasjuan | 0:694030aeaef8 | 186 | |
cobasjuan | 0:694030aeaef8 | 187 | } |
cobasjuan | 0:694030aeaef8 | 188 | } |
cobasjuan | 0:694030aeaef8 | 189 | |
cobasjuan | 0:694030aeaef8 | 190 | |
cobasjuan | 0:694030aeaef8 | 191 | void t() |
cobasjuan | 0:694030aeaef8 | 192 | { |
cobasjuan | 0:694030aeaef8 | 193 | if (tiempo > 0) |
cobasjuan | 0:694030aeaef8 | 194 | tiempo--; |
cobasjuan | 0:694030aeaef8 | 195 | } |
cobasjuan | 0:694030aeaef8 | 196 | |
cobasjuan | 0:694030aeaef8 | 197 | |
cobasjuan | 0:694030aeaef8 | 198 | void PULSADORES() |
cobasjuan | 0:694030aeaef8 | 199 | { |
cobasjuan | 0:694030aeaef8 | 200 | static int lectura=0, color=0; |
cobasjuan | 0:694030aeaef8 | 201 | float valor=tsi.readPercentage(); |
cobasjuan | 0:694030aeaef8 | 202 | if(tiempo == 0) { |
cobasjuan | 0:694030aeaef8 | 203 | tiempo=3; |
cobasjuan | 0:694030aeaef8 | 204 | if(valor != 0 && lectura == 0) { |
cobasjuan | 0:694030aeaef8 | 205 | lectura=1; |
cobasjuan | 0:694030aeaef8 | 206 | if(valor > 0.37 && valor < 0.73) { |
cobasjuan | 0:694030aeaef8 | 207 | P2=1; |
cobasjuan | 0:694030aeaef8 | 208 | } else if(valor <= 0.33) { |
cobasjuan | 0:694030aeaef8 | 209 | P1=1; |
cobasjuan | 0:694030aeaef8 | 210 | } else if(valor >= 0.77) { |
cobasjuan | 0:694030aeaef8 | 211 | P3=1; |
cobasjuan | 0:694030aeaef8 | 212 | } |
cobasjuan | 0:694030aeaef8 | 213 | } else if( valor == 0 && lectura == 1) { |
cobasjuan | 0:694030aeaef8 | 214 | ledr=1; |
cobasjuan | 0:694030aeaef8 | 215 | ledg=1; |
cobasjuan | 0:694030aeaef8 | 216 | ledb=1; |
cobasjuan | 0:694030aeaef8 | 217 | traba=0; |
cobasjuan | 0:694030aeaef8 | 218 | color=0; |
cobasjuan | 0:694030aeaef8 | 219 | lectura=0; |
cobasjuan | 0:694030aeaef8 | 220 | P1=0; |
cobasjuan | 0:694030aeaef8 | 221 | P2=0; |
cobasjuan | 0:694030aeaef8 | 222 | P3=0; |
cobasjuan | 0:694030aeaef8 | 223 | } |
cobasjuan | 0:694030aeaef8 | 224 | if(valor != 0 && principio > 0 && color == 0) { |
cobasjuan | 0:694030aeaef8 | 225 | color=1; |
cobasjuan | 0:694030aeaef8 | 226 | if(valor > 0.37 && valor < 0.73) { |
cobasjuan | 0:694030aeaef8 | 227 | printf("Tocaste el color VERDE\n\r"); |
cobasjuan | 0:694030aeaef8 | 228 | ledg=0; |
cobasjuan | 0:694030aeaef8 | 229 | } else if(valor <= 0.33) { |
cobasjuan | 0:694030aeaef8 | 230 | printf("Tocaste el color ROJO\n\r"); |
cobasjuan | 0:694030aeaef8 | 231 | ledr=0; |
cobasjuan | 0:694030aeaef8 | 232 | } else if(valor >= 0.77) { |
cobasjuan | 0:694030aeaef8 | 233 | printf("Tocaste el color AZUL\n\r"); |
cobasjuan | 0:694030aeaef8 | 234 | ledb=0; |
cobasjuan | 0:694030aeaef8 | 235 | } |
cobasjuan | 0:694030aeaef8 | 236 | } |
cobasjuan | 0:694030aeaef8 | 237 | } |
cobasjuan | 0:694030aeaef8 | 238 | } |