TP1_EJER3

Dependencies:   mbed tsi_sensor

Committer:
Charin
Date:
Tue Jun 04 02:09:27 2019 +0000
Revision:
1:5b92fb2cd769
Parent:
0:256d00799c21
TP1_EJER3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charin 0:256d00799c21 1 #include "mbed.h"
Charin 0:256d00799c21 2 #include "tsi_sensor.h"
Charin 0:256d00799c21 3
Charin 0:256d00799c21 4 /* This defines will be replaced by PinNames soon */
Charin 0:256d00799c21 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
Charin 0:256d00799c21 6 #define ELEC0 9
Charin 0:256d00799c21 7 #define ELEC1 10
Charin 0:256d00799c21 8 #elif defined (TARGET_KL05Z)
Charin 0:256d00799c21 9 #define ELEC0 9
Charin 0:256d00799c21 10 #define ELEC1 8
Charin 0:256d00799c21 11 #else
Charin 0:256d00799c21 12 #error TARGET NOT DEFINED
Charin 0:256d00799c21 13 #endif
Charin 1:5b92fb2cd769 14 Ticker contador;
Charin 1:5b92fb2cd769 15 Ticker titila;
Charin 0:256d00799c21 16
Charin 1:5b92fb2cd769 17 //Enumero los distintos casos de la Maquina de Estados
Charin 0:256d00799c21 18 enum maquina_estado_general {INICIO, CABLE1, CABLE2, CABLE3, CABLE4, WIN, LOOSER};
Charin 0:256d00799c21 19 maquina_estado_general maquina_general;
Charin 0:256d00799c21 20
Charin 0:256d00799c21 21 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
Charin 1:5b92fb2cd769 22
Charin 1:5b92fb2cd769 23 //Defino salidas utilizadas
Charin 0:256d00799c21 24 AnalogIn noise (PTC1);
Charin 0:256d00799c21 25 DigitalIn cable_0 (PTB0);
Charin 0:256d00799c21 26 DigitalIn cable_1 (PTB1);
Charin 0:256d00799c21 27 DigitalIn cable_2 (PTB2);
Charin 1:5b92fb2cd769 28 DigitalIn cable_3 (PTB3);
Charin 0:256d00799c21 29 DigitalOut verde (LED_GREEN);
Charin 0:256d00799c21 30 DigitalOut rojo (LED_RED);
Charin 1:5b92fb2cd769 31 DigitalOut buzzzer (PTC12);
Charin 1:5b92fb2cd769 32 BusOut Unidades (PTE2,PTE3,PTE4,PTE5);
Charin 1:5b92fb2cd769 33 BusOut Decenas (PTB8,PTB9,PTB10,PTB11);
Charin 0:256d00799c21 34
Charin 1:5b92fb2cd769 35 //Creo y defino vectores
Charin 0:256d00799c21 36 int vector_secuencia [4];
Charin 0:256d00799c21 37 int vector_orden_cables [4] = {-1, -1, -1, -1};
Charin 1:5b92fb2cd769 38
Charin 1:5b92fb2cd769 39 //Defino variables
Charin 0:256d00799c21 40 int ccero = 0;
Charin 0:256d00799c21 41 int cuno = 0;
Charin 0:256d00799c21 42 int cdos = 0;
Charin 0:256d00799c21 43 int ctres = 0;
Charin 0:256d00799c21 44 int sumador = 0;
Charin 1:5b92fb2cd769 45 int contadorun = 0;
Charin 1:5b92fb2cd769 46 int contadordec = 0;
Charin 1:5b92fb2cd769 47 int contadortotal = 60;
Charin 1:5b92fb2cd769 48 int mandar = 0;
Charin 1:5b92fb2cd769 49 int titilarun = 0;
Charin 1:5b92fb2cd769 50 int titilardec = 0;
Charin 1:5b92fb2cd769 51 int titilar = 0;
Charin 1:5b92fb2cd769 52 int x = 0;
Charin 1:5b92fb2cd769 53 int z = 0;
Charin 1:5b92fb2cd769 54 int a = 0;
Charin 0:256d00799c21 55
Charin 1:5b92fb2cd769 56 //Prototipo de funciones
Charin 0:256d00799c21 57 void secuencia();
Charin 0:256d00799c21 58 void maquina_general_sec ();
Charin 1:5b92fb2cd769 59 void orden_cables ();
Charin 0:256d00799c21 60 void suma ();
Charin 1:5b92fb2cd769 61 void funcion_tiempo ();
Charin 1:5b92fb2cd769 62 void funcion_titila ();
Charin 1:5b92fb2cd769 63 void buzzer ();
Charin 0:256d00799c21 64 int cablesDesconectados(int n);
Charin 0:256d00799c21 65
Charin 0:256d00799c21 66 int main(void)
Charin 0:256d00799c21 67 {
Charin 1:5b92fb2cd769 68 //Activo Pull Up interno
Charin 0:256d00799c21 69 cable_0.mode (PullUp);
Charin 0:256d00799c21 70 cable_1.mode (PullUp);
Charin 0:256d00799c21 71 cable_2.mode (PullUp);
Charin 0:256d00799c21 72 cable_3.mode (PullUp);
Charin 0:256d00799c21 73
Charin 1:5b92fb2cd769 74 //Salida que mide ruido para la semilla de la funcion rand
Charin 0:256d00799c21 75 srand (int(noise * 10000));
Charin 0:256d00799c21 76
Charin 0:256d00799c21 77 secuencia ();
Charin 0:256d00799c21 78 verde = 1;
Charin 0:256d00799c21 79 rojo = 1;
Charin 1:5b92fb2cd769 80 contador.attach (&funcion_tiempo, 1);
Charin 1:5b92fb2cd769 81 titila.attach (&funcion_titila, 0.5);
Charin 0:256d00799c21 82
Charin 0:256d00799c21 83 while (true) {
Charin 1:5b92fb2cd769 84
Charin 0:256d00799c21 85 maquina_general_sec ();
Charin 1:5b92fb2cd769 86 orden_cables ();
Charin 1:5b92fb2cd769 87
Charin 0:256d00799c21 88 }
Charin 0:256d00799c21 89 }
Charin 0:256d00799c21 90
Charin 1:5b92fb2cd769 91 /*Funcion que crea la secuencia del orden que hay que sacar los cables
Charin 1:5b92fb2cd769 92 y los guarda en un vector evitando que se repita el numero*/
Charin 0:256d00799c21 93 void secuencia ()
Charin 0:256d00799c21 94 {
Charin 0:256d00799c21 95 vector_secuencia [0] = rand () %4;
Charin 0:256d00799c21 96 for (int i = 1; i < 4; i++) {
Charin 0:256d00799c21 97 int randomm = rand () %4;
Charin 1:5b92fb2cd769 98 int aux = 1;
Charin 1:5b92fb2cd769 99 for(int j = 0; j < i; j++)
Charin 1:5b92fb2cd769 100 if(vector_secuencia[j] == randomm)
Charin 1:5b92fb2cd769 101 aux = 0;
Charin 1:5b92fb2cd769 102 if(aux)
Charin 1:5b92fb2cd769 103 vector_secuencia[i] = randomm;
Charin 0:256d00799c21 104 else
Charin 0:256d00799c21 105 i--;
Charin 0:256d00799c21 106 }
Charin 0:256d00799c21 107
Charin 0:256d00799c21 108 }
Charin 0:256d00799c21 109
Charin 1:5b92fb2cd769 110 //Maquina de Estado general para arranque, pasaje de un cable a otro y reinicio
Charin 0:256d00799c21 111 void maquina_general_sec ()
Charin 0:256d00799c21 112 {
Charin 0:256d00799c21 113 switch (maquina_general) {
Charin 0:256d00799c21 114
Charin 0:256d00799c21 115 default:
Charin 0:256d00799c21 116 maquina_general = INICIO;
Charin 0:256d00799c21 117 break;
Charin 1:5b92fb2cd769 118 //Se arranca apretando el tsi y con los display en 60seg. (TSI tambien para reiniciar)
Charin 0:256d00799c21 119 case INICIO:
Charin 1:5b92fb2cd769 120 rojo = 1;
Charin 1:5b92fb2cd769 121 verde = 1;
Charin 1:5b92fb2cd769 122 buzzzer = 0;
Charin 1:5b92fb2cd769 123 if (tsi.readPercentage() != 0) {
Charin 1:5b92fb2cd769 124 a = 0;
Charin 1:5b92fb2cd769 125 contadortotal = 60;
Charin 1:5b92fb2cd769 126 x = 1;
Charin 1:5b92fb2cd769 127 mandar = 1;
Charin 1:5b92fb2cd769 128 z = 0;
Charin 0:256d00799c21 129 maquina_general = CABLE1;
Charin 0:256d00799c21 130 }
Charin 0:256d00799c21 131 break;
Charin 1:5b92fb2cd769 132 //Compara el primer cable con la secuencia creada
Charin 0:256d00799c21 133 case CABLE1:
Charin 0:256d00799c21 134 if (vector_secuencia [0] == vector_orden_cables [0] && vector_orden_cables [0] != -1) {
Charin 0:256d00799c21 135 maquina_general = CABLE2;
Charin 0:256d00799c21 136 }
Charin 0:256d00799c21 137 if (vector_secuencia [0] != vector_orden_cables [0] && vector_orden_cables [0] != -1) {
Charin 0:256d00799c21 138 maquina_general = LOOSER;
Charin 0:256d00799c21 139 }
Charin 0:256d00799c21 140
Charin 0:256d00799c21 141 break;
Charin 1:5b92fb2cd769 142 //Compara el segundo cable con la secuencia creada
Charin 0:256d00799c21 143 case CABLE2:
Charin 0:256d00799c21 144 if (vector_secuencia [1] == vector_orden_cables [1] && vector_orden_cables [1] != -1) {
Charin 0:256d00799c21 145 maquina_general = CABLE3;
Charin 0:256d00799c21 146 }
Charin 0:256d00799c21 147 if (vector_secuencia [1] != vector_orden_cables [1] && vector_orden_cables [1] != -1) {
Charin 0:256d00799c21 148 maquina_general = LOOSER;
Charin 0:256d00799c21 149 }
Charin 0:256d00799c21 150
Charin 0:256d00799c21 151 break;
Charin 1:5b92fb2cd769 152 //Compara el tercer cable con la secuencia creada
Charin 0:256d00799c21 153 case CABLE3:
Charin 0:256d00799c21 154 if (vector_secuencia [2] == vector_orden_cables [2] && vector_orden_cables [2] != -1) {
Charin 0:256d00799c21 155 maquina_general = CABLE4;
Charin 0:256d00799c21 156 }
Charin 0:256d00799c21 157 if (vector_secuencia [2] != vector_orden_cables [2] && vector_orden_cables [2] != -1) {
Charin 0:256d00799c21 158 maquina_general = LOOSER;
Charin 0:256d00799c21 159 }
Charin 0:256d00799c21 160 break;
Charin 1:5b92fb2cd769 161 //Compara el cuarto cable con la secuencia creada
Charin 0:256d00799c21 162 case CABLE4:
Charin 0:256d00799c21 163 if (vector_secuencia [3] == vector_orden_cables [3] && vector_orden_cables [3] != -1) {
Charin 0:256d00799c21 164 maquina_general = WIN;
Charin 0:256d00799c21 165 }
Charin 0:256d00799c21 166 if (vector_secuencia [3] != vector_orden_cables [3] && vector_orden_cables [3] != -1) {
Charin 0:256d00799c21 167 maquina_general = LOOSER;
Charin 0:256d00799c21 168 }
Charin 0:256d00799c21 169 break;
Charin 1:5b92fb2cd769 170 //Si el orden es correcto titilan los display y led verde
Charin 0:256d00799c21 171 case WIN:
Charin 1:5b92fb2cd769 172 z = 1;
Charin 1:5b92fb2cd769 173 rojo=1;
Charin 1:5b92fb2cd769 174 if (titilar %2 == 0) {
Charin 1:5b92fb2cd769 175 Unidades = titilarun;
Charin 1:5b92fb2cd769 176 Decenas = titilardec;
Charin 1:5b92fb2cd769 177 buzzzer = 1;
Charin 1:5b92fb2cd769 178 verde = 0;
Charin 1:5b92fb2cd769 179 } else if(titilar %2 != 0) {
Charin 1:5b92fb2cd769 180 Unidades = 15;
Charin 1:5b92fb2cd769 181 Decenas = 15;
Charin 1:5b92fb2cd769 182 buzzzer = 0;
Charin 1:5b92fb2cd769 183 verde = 1;
Charin 1:5b92fb2cd769 184 }
Charin 1:5b92fb2cd769 185
Charin 1:5b92fb2cd769 186 if (tsi.readPercentage() != 0) {
Charin 1:5b92fb2cd769 187 maquina_general = INICIO;
Charin 1:5b92fb2cd769 188 for(int k = 0; k < 4; k++)
Charin 1:5b92fb2cd769 189 vector_orden_cables[k] = -1;
Charin 1:5b92fb2cd769 190 secuencia();
Charin 1:5b92fb2cd769 191
Charin 1:5b92fb2cd769 192 }
Charin 1:5b92fb2cd769 193
Charin 0:256d00799c21 194 break;
Charin 1:5b92fb2cd769 195 //Si el orden es incorrecto titilan los display y led rojo
Charin 0:256d00799c21 196 case LOOSER:
Charin 1:5b92fb2cd769 197 z = 1;
Charin 1:5b92fb2cd769 198 if(tsi.readPercentage() == 0) {
Charin 1:5b92fb2cd769 199 if (titilar %2 == 0) {
Charin 1:5b92fb2cd769 200 Unidades = titilarun;
Charin 1:5b92fb2cd769 201 Decenas = titilardec;
Charin 1:5b92fb2cd769 202 buzzzer = 1;
Charin 1:5b92fb2cd769 203 rojo = 0;
Charin 1:5b92fb2cd769 204 } else if(titilar %2 != 0) {
Charin 1:5b92fb2cd769 205 Unidades = 15;
Charin 1:5b92fb2cd769 206 Decenas = 15;
Charin 1:5b92fb2cd769 207 buzzzer = 0;
Charin 1:5b92fb2cd769 208 rojo = 1;
Charin 1:5b92fb2cd769 209 }
Charin 1:5b92fb2cd769 210 }
Charin 1:5b92fb2cd769 211 if (tsi.readPercentage() != 0) {
Charin 1:5b92fb2cd769 212 maquina_general = INICIO;
Charin 1:5b92fb2cd769 213 for(int k = 0; k < 4; k++)
Charin 1:5b92fb2cd769 214 vector_orden_cables[k] = -1;
Charin 1:5b92fb2cd769 215 secuencia();
Charin 1:5b92fb2cd769 216 }
Charin 0:256d00799c21 217 break;
Charin 0:256d00799c21 218
Charin 0:256d00799c21 219 }
Charin 0:256d00799c21 220 }
Charin 1:5b92fb2cd769 221 //Funcion que guarda el orden de los cables que se sacan
Charin 1:5b92fb2cd769 222 void orden_cables ()
Charin 0:256d00799c21 223 {
Charin 0:256d00799c21 224
Charin 0:256d00799c21 225 if (cable_0 != 0 && cablesDesconectados(0) == 0) {
Charin 0:256d00799c21 226 ccero = 0;
Charin 0:256d00799c21 227 vector_orden_cables [a] = ccero;
Charin 0:256d00799c21 228 a++;
Charin 0:256d00799c21 229 printf ("vector orden cables [%d] = 0", &a);
Charin 0:256d00799c21 230
Charin 0:256d00799c21 231 }
Charin 0:256d00799c21 232 if (cable_1 != 0 && cablesDesconectados(1) == 0) {
Charin 0:256d00799c21 233 cuno = 1;
Charin 0:256d00799c21 234 vector_orden_cables [a] = cuno;
Charin 0:256d00799c21 235 a++;
Charin 0:256d00799c21 236 printf ("vector orden cables [%d] = 1", &a);
Charin 0:256d00799c21 237
Charin 0:256d00799c21 238 }
Charin 0:256d00799c21 239 if (cable_2 != 0 && cablesDesconectados(2) == 0) {
Charin 0:256d00799c21 240 cdos = 2;
Charin 0:256d00799c21 241 vector_orden_cables [a] = cdos;
Charin 0:256d00799c21 242 a++;
Charin 0:256d00799c21 243 printf ("vector orden cables [%d] = 2", &a);
Charin 0:256d00799c21 244 }
Charin 1:5b92fb2cd769 245 if (cable_3 != 0 && cablesDesconectados(3) == 0) {
Charin 0:256d00799c21 246 ctres = 3;
Charin 0:256d00799c21 247 vector_orden_cables [a] = ctres;
Charin 0:256d00799c21 248 a++;
Charin 0:256d00799c21 249 printf ("vector orden cables [%d] = 3", &a);
Charin 0:256d00799c21 250 }
Charin 0:256d00799c21 251 }
Charin 1:5b92fb2cd769 252 //Funcion que interpreta la desconexion de los cables
Charin 0:256d00799c21 253 int cablesDesconectados(int n)
Charin 0:256d00799c21 254 {
Charin 0:256d00799c21 255
Charin 0:256d00799c21 256 for(int j = 0; j < 4; j++) {
Charin 0:256d00799c21 257 if(vector_orden_cables[j] == n)
Charin 0:256d00799c21 258 return 1;
Charin 0:256d00799c21 259 }
Charin 0:256d00799c21 260 return 0;
Charin 0:256d00799c21 261 }
Charin 1:5b92fb2cd769 262 //Funcion de cuenta regresiva en los display
Charin 1:5b92fb2cd769 263 void funcion_tiempo()
Charin 1:5b92fb2cd769 264 {
Charin 1:5b92fb2cd769 265
Charin 1:5b92fb2cd769 266 if(contadortotal == 0)
Charin 1:5b92fb2cd769 267 maquina_general = LOOSER;
Charin 1:5b92fb2cd769 268
Charin 1:5b92fb2cd769 269 if(!z && x && contadortotal > 0)
Charin 1:5b92fb2cd769 270 contadortotal--;
Charin 1:5b92fb2cd769 271
Charin 1:5b92fb2cd769 272 Unidades = contadortotal % 10;
Charin 1:5b92fb2cd769 273 titilarun = Unidades;
Charin 1:5b92fb2cd769 274 Decenas = contadortotal / 10;
Charin 1:5b92fb2cd769 275 titilardec = Decenas;
Charin 1:5b92fb2cd769 276
Charin 1:5b92fb2cd769 277
Charin 1:5b92fb2cd769 278 printf("%d %d %d %d || %d %d %d %d\n\r", vector_orden_cables[0], vector_orden_cables[1], vector_orden_cables[2], vector_orden_cables[3], vector_secuencia[0], vector_secuencia[1], vector_secuencia[2], vector_secuencia[3]);
Charin 1:5b92fb2cd769 279
Charin 1:5b92fb2cd769 280 }
Charin 1:5b92fb2cd769 281
Charin 1:5b92fb2cd769 282 void funcion_titila ()
Charin 1:5b92fb2cd769 283 {
Charin 1:5b92fb2cd769 284 titilar++;
Charin 1:5b92fb2cd769 285 }