Programa que lee comandos por el puerto serial y reproduce un tono según el comando recibido. Adicionalmente identifica colores con el sensor TC3200 y envía el color leído a través del puerto serial al PC.
main.cpp@0:38dbd24805dd, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 05:18:41 2021 +0000
- Revision:
- 0:38dbd24805dd
Programa finalizado y funcional.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:38dbd24805dd | 1 | #include "mbed.h" |
CCastrop1012 | 0:38dbd24805dd | 2 | #include "scolor_TCS3200.h" |
CCastrop1012 | 0:38dbd24805dd | 3 | |
CCastrop1012 | 0:38dbd24805dd | 4 | |
CCastrop1012 | 0:38dbd24805dd | 5 | //****************************************************************************** |
CCastrop1012 | 0:38dbd24805dd | 6 | // Definicion de Periféricos |
CCastrop1012 | 0:38dbd24805dd | 7 | |
CCastrop1012 | 0:38dbd24805dd | 8 | /// Puerto Serial |
CCastrop1012 | 0:38dbd24805dd | 9 | Serial SerialPort(SERIAL_TX, SERIAL_RX, 9600); |
CCastrop1012 | 0:38dbd24805dd | 10 | |
CCastrop1012 | 0:38dbd24805dd | 11 | /// PWM OUTPUTS |
CCastrop1012 | 0:38dbd24805dd | 12 | PwmOut Buzzer(LED1); |
CCastrop1012 | 0:38dbd24805dd | 13 | |
CCastrop1012 | 0:38dbd24805dd | 14 | // Temporizadores |
CCastrop1012 | 0:38dbd24805dd | 15 | Ticker LengthTonoBuzzer; |
CCastrop1012 | 0:38dbd24805dd | 16 | |
CCastrop1012 | 0:38dbd24805dd | 17 | // SENSOR DE COLOR |
CCastrop1012 | 0:38dbd24805dd | 18 | scolor_TCS3200 SENSOR_COLOR (PA_8, PB_10, PB_4, PB_5, PB_3); |
CCastrop1012 | 0:38dbd24805dd | 19 | |
CCastrop1012 | 0:38dbd24805dd | 20 | |
CCastrop1012 | 0:38dbd24805dd | 21 | |
CCastrop1012 | 0:38dbd24805dd | 22 | //****************************************************************************** |
CCastrop1012 | 0:38dbd24805dd | 23 | /// Declarar Variables Globales |
CCastrop1012 | 0:38dbd24805dd | 24 | |
CCastrop1012 | 0:38dbd24805dd | 25 | |
CCastrop1012 | 0:38dbd24805dd | 26 | uint8_t programa_ejecutar = 0; // Variable que almacena la ORDEN (Telemetria ó Telecomando) |
CCastrop1012 | 0:38dbd24805dd | 27 | // enviada desde el CoolTerm |
CCastrop1012 | 0:38dbd24805dd | 28 | |
CCastrop1012 | 0:38dbd24805dd | 29 | |
CCastrop1012 | 0:38dbd24805dd | 30 | long red; //Almacenan el Tiempo que dura el CicloUtil de la frecuencia |
CCastrop1012 | 0:38dbd24805dd | 31 | long blue; //Generada por el Sensor TSC3200, al leer el componente |
CCastrop1012 | 0:38dbd24805dd | 32 | long green; //R, G, B o Clear del color que tenga en Frente |
CCastrop1012 | 0:38dbd24805dd | 33 | long clear; |
CCastrop1012 | 0:38dbd24805dd | 34 | |
CCastrop1012 | 0:38dbd24805dd | 35 | uint8_t coolterm_comand; // Se almacenan Todos los Caracteres recibidos por el SerialPort. |
CCastrop1012 | 0:38dbd24805dd | 36 | |
CCastrop1012 | 0:38dbd24805dd | 37 | // No usada por Ahora |
CCastrop1012 | 0:38dbd24805dd | 38 | uint8_t i = 0; // Variable usada como Indice de los vectores, |
CCastrop1012 | 0:38dbd24805dd | 39 | // indica la posicion del vector en donde se reciben |
CCastrop1012 | 0:38dbd24805dd | 40 | // ó se almacenan los datos |
CCastrop1012 | 0:38dbd24805dd | 41 | |
CCastrop1012 | 0:38dbd24805dd | 42 | |
CCastrop1012 | 0:38dbd24805dd | 43 | uint8_t duracion_tono = 1; // Variable que almacena el Tiempo que es ESCUCHARÁ el Tono |
CCastrop1012 | 0:38dbd24805dd | 44 | uint8_t tipo_tono = 1; // Variable que almacena el Tono que se desee escuchar |
CCastrop1012 | 0:38dbd24805dd | 45 | // siendo |
CCastrop1012 | 0:38dbd24805dd | 46 | #define TONO_DO 0x01 /// si tipo_tono == 0x01, se escuchará un DO |
CCastrop1012 | 0:38dbd24805dd | 47 | #define TONO_RE 0x02 /// si tipo_tono == 0x02, se escuchará un RE |
CCastrop1012 | 0:38dbd24805dd | 48 | #define TONO_MI 0x03 /// si tipo_tono == 0x03, se escuchará un MI |
CCastrop1012 | 0:38dbd24805dd | 49 | #define TONO_SI 0x04 /// si tipo_tono == 0x04, se escuchará un SI |
CCastrop1012 | 0:38dbd24805dd | 50 | |
CCastrop1012 | 0:38dbd24805dd | 51 | #define DO 3.78 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN DO |
CCastrop1012 | 0:38dbd24805dd | 52 | #define RE 3.36 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN RE |
CCastrop1012 | 0:38dbd24805dd | 53 | #define MI 3.03 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN MI |
CCastrop1012 | 0:38dbd24805dd | 54 | #define SI 2.02 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN SI |
CCastrop1012 | 0:38dbd24805dd | 55 | |
CCastrop1012 | 0:38dbd24805dd | 56 | //****************************************************************************** |
CCastrop1012 | 0:38dbd24805dd | 57 | // COMANDOS |
CCastrop1012 | 0:38dbd24805dd | 58 | |
CCastrop1012 | 0:38dbd24805dd | 59 | #define iniciar_telemetria 0xFE |
CCastrop1012 | 0:38dbd24805dd | 60 | #define iniciar_telecomando 0xFF |
CCastrop1012 | 0:38dbd24805dd | 61 | |
CCastrop1012 | 0:38dbd24805dd | 62 | #define telemetria_1 0x01 // |
CCastrop1012 | 0:38dbd24805dd | 63 | #define telemcomando_1 0x01 |
CCastrop1012 | 0:38dbd24805dd | 64 | |
CCastrop1012 | 0:38dbd24805dd | 65 | #define CMD_rojo 0x01 |
CCastrop1012 | 0:38dbd24805dd | 66 | #define CMD_azul 0x02 |
CCastrop1012 | 0:38dbd24805dd | 67 | #define CMD_verde 0x03 |
CCastrop1012 | 0:38dbd24805dd | 68 | #define CMD_clear 0x04 |
CCastrop1012 | 0:38dbd24805dd | 69 | #define ColorNoIdentificado 0x05 |
CCastrop1012 | 0:38dbd24805dd | 70 | |
CCastrop1012 | 0:38dbd24805dd | 71 | uint8_t color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:38dbd24805dd | 72 | |
CCastrop1012 | 0:38dbd24805dd | 73 | //**************************************************************************** |
CCastrop1012 | 0:38dbd24805dd | 74 | // Prototipo de funciones |
CCastrop1012 | 0:38dbd24805dd | 75 | |
CCastrop1012 | 0:38dbd24805dd | 76 | void ReadPort(void); // Lee el puerto Serial |
CCastrop1012 | 0:38dbd24805dd | 77 | void MainConfig(void); // Configuracion Inicial de los Perifericos del uC |
CCastrop1012 | 0:38dbd24805dd | 78 | void Buzzer_Tone(uint8_t tipo_tono, uint8_t duracion_tono); // configura el tono y la duracion escuchada a travez del Buzzer |
CCastrop1012 | 0:38dbd24805dd | 79 | void leer_color(void); // funcion que retorna los componentes |
CCastrop1012 | 0:38dbd24805dd | 80 | // RGB y Clear del color leido |
CCastrop1012 | 0:38dbd24805dd | 81 | |
CCastrop1012 | 0:38dbd24805dd | 82 | |
CCastrop1012 | 0:38dbd24805dd | 83 | //**************************************************************************** |
CCastrop1012 | 0:38dbd24805dd | 84 | // Funciones |
CCastrop1012 | 0:38dbd24805dd | 85 | |
CCastrop1012 | 0:38dbd24805dd | 86 | |
CCastrop1012 | 0:38dbd24805dd | 87 | |
CCastrop1012 | 0:38dbd24805dd | 88 | void ReadPort() |
CCastrop1012 | 0:38dbd24805dd | 89 | { |
CCastrop1012 | 0:38dbd24805dd | 90 | if(SerialPort.writable()) SerialPort.abort_write(); |
CCastrop1012 | 0:38dbd24805dd | 91 | coolterm_comand = SerialPort.getc(); |
CCastrop1012 | 0:38dbd24805dd | 92 | |
CCastrop1012 | 0:38dbd24805dd | 93 | if (coolterm_comand == iniciar_telemetria) programa_ejecutar = iniciar_telemetria; /// El programa que se ejecutará será Telemetria |
CCastrop1012 | 0:38dbd24805dd | 94 | if (coolterm_comand == iniciar_telecomando) programa_ejecutar = iniciar_telecomando; |
CCastrop1012 | 0:38dbd24805dd | 95 | |
CCastrop1012 | 0:38dbd24805dd | 96 | |
CCastrop1012 | 0:38dbd24805dd | 97 | } |
CCastrop1012 | 0:38dbd24805dd | 98 | |
CCastrop1012 | 0:38dbd24805dd | 99 | |
CCastrop1012 | 0:38dbd24805dd | 100 | int main() |
CCastrop1012 | 0:38dbd24805dd | 101 | { |
CCastrop1012 | 0:38dbd24805dd | 102 | Buzzer.write(0); ///configura el ciclo util |
CCastrop1012 | 0:38dbd24805dd | 103 | SerialPort.attach(&ReadPort, Serial::RxIrq); //// se Habilita la interrupcion serial o recepcion de datos |
CCastrop1012 | 0:38dbd24805dd | 104 | //SerialPort.printf("Hello World, System Run !!\n"); // mensaje inicial |
CCastrop1012 | 0:38dbd24805dd | 105 | |
CCastrop1012 | 0:38dbd24805dd | 106 | |
CCastrop1012 | 0:38dbd24805dd | 107 | while(1) |
CCastrop1012 | 0:38dbd24805dd | 108 | { |
CCastrop1012 | 0:38dbd24805dd | 109 | // Esperamos hasta que se GENERE una INTERUPCION y |
CCastrop1012 | 0:38dbd24805dd | 110 | // coolterm_comand reciba un comando ya sea iniciar_telemetria ó iniciar_telecomandos |
CCastrop1012 | 0:38dbd24805dd | 111 | // Por lo que generará que programa_ejecutar sea igual al comando recivido |
CCastrop1012 | 0:38dbd24805dd | 112 | |
CCastrop1012 | 0:38dbd24805dd | 113 | // AHORA: |
CCastrop1012 | 0:38dbd24805dd | 114 | // SI EL PROGRAMA ESCOGIDO ES INICIAR_TELEMETRIA |
CCastrop1012 | 0:38dbd24805dd | 115 | if (programa_ejecutar == iniciar_telemetria) |
CCastrop1012 | 0:38dbd24805dd | 116 | { |
CCastrop1012 | 0:38dbd24805dd | 117 | // ESPERAMOS A RECBIR UN COMANDO DESDE EL COOLTERM DIFERENTE AL que YA hemos RECIBIDO |
CCastrop1012 | 0:38dbd24805dd | 118 | while(coolterm_comand == iniciar_telemetria) wait_ms(1); // esperamos mientras se recibe otro dato |
CCastrop1012 | 0:38dbd24805dd | 119 | |
CCastrop1012 | 0:38dbd24805dd | 120 | //// Desactivamos la interrupcion serial o recepcion de datos PORQUE NO NECESITAMOS recibir mas datos por AHORA |
CCastrop1012 | 0:38dbd24805dd | 121 | SerialPort.attach(NULL, Serial::RxIrq); |
CCastrop1012 | 0:38dbd24805dd | 122 | |
CCastrop1012 | 0:38dbd24805dd | 123 | // SEGÚN LA TELEMETRIA SELECCIONADA (0X01, 0X02 .... 0XN) ENTONCES SE EJECUTA UNA FUNCIÓN |
CCastrop1012 | 0:38dbd24805dd | 124 | switch (coolterm_comand) |
CCastrop1012 | 0:38dbd24805dd | 125 | { |
CCastrop1012 | 0:38dbd24805dd | 126 | |
CCastrop1012 | 0:38dbd24805dd | 127 | case telemetria_1: |
CCastrop1012 | 0:38dbd24805dd | 128 | // Ejecutamos la Funcion LeerColor(); |
CCastrop1012 | 0:38dbd24805dd | 129 | leer_color(); |
CCastrop1012 | 0:38dbd24805dd | 130 | |
CCastrop1012 | 0:38dbd24805dd | 131 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 132 | |
CCastrop1012 | 0:38dbd24805dd | 133 | // si no fue ninguno de los valores anteriores entonces: |
CCastrop1012 | 0:38dbd24805dd | 134 | default: SerialPort.printf("telemetria desconocida, inicie nuevamente !!\n"); |
CCastrop1012 | 0:38dbd24805dd | 135 | |
CCastrop1012 | 0:38dbd24805dd | 136 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 137 | |
CCastrop1012 | 0:38dbd24805dd | 138 | } |
CCastrop1012 | 0:38dbd24805dd | 139 | |
CCastrop1012 | 0:38dbd24805dd | 140 | |
CCastrop1012 | 0:38dbd24805dd | 141 | // Re inicializamos nuestras variables de control a sus valores iniciales |
CCastrop1012 | 0:38dbd24805dd | 142 | // Para no seguir entrando a las sentencias IF |
CCastrop1012 | 0:38dbd24805dd | 143 | programa_ejecutar = 0; coolterm_comand = 0; |
CCastrop1012 | 0:38dbd24805dd | 144 | |
CCastrop1012 | 0:38dbd24805dd | 145 | |
CCastrop1012 | 0:38dbd24805dd | 146 | //// HABILITAMOS NUEVAMENTE la interrupcion serial o recepcion de datos |
CCastrop1012 | 0:38dbd24805dd | 147 | SerialPort.attach(&ReadPort, Serial::RxIrq); |
CCastrop1012 | 0:38dbd24805dd | 148 | } |
CCastrop1012 | 0:38dbd24805dd | 149 | |
CCastrop1012 | 0:38dbd24805dd | 150 | |
CCastrop1012 | 0:38dbd24805dd | 151 | |
CCastrop1012 | 0:38dbd24805dd | 152 | //// SI EL PROGRAMA ESCOGIDO ES INICIAR_TELECOMANDO |
CCastrop1012 | 0:38dbd24805dd | 153 | if (programa_ejecutar == iniciar_telecomando) |
CCastrop1012 | 0:38dbd24805dd | 154 | { |
CCastrop1012 | 0:38dbd24805dd | 155 | // ESPERAMOS A RECBIR UN COMANDO DESDE EL COOLTERM DIFERENTE AL que YA hemos RECIBIDO |
CCastrop1012 | 0:38dbd24805dd | 156 | while(coolterm_comand == iniciar_telecomando) wait_ms(1); // esperamos mientras se recibe otro dato |
CCastrop1012 | 0:38dbd24805dd | 157 | |
CCastrop1012 | 0:38dbd24805dd | 158 | |
CCastrop1012 | 0:38dbd24805dd | 159 | // SEGÚN EL TELECOMANDO SELECCIONADO (0X01, 0X02 .... 0XN) ENTONCES SE EJECUTA UNA FUNCIÓN |
CCastrop1012 | 0:38dbd24805dd | 160 | switch (coolterm_comand) |
CCastrop1012 | 0:38dbd24805dd | 161 | { |
CCastrop1012 | 0:38dbd24805dd | 162 | |
CCastrop1012 | 0:38dbd24805dd | 163 | case telemcomando_1: |
CCastrop1012 | 0:38dbd24805dd | 164 | |
CCastrop1012 | 0:38dbd24805dd | 165 | // ESPERAMOS A RECBIR UN COMANDO DESDE EL COOLTERM DIFERENTE AL ULTIMO RECIBIDO |
CCastrop1012 | 0:38dbd24805dd | 166 | while(coolterm_comand == telemcomando_1) wait_ms(1); |
CCastrop1012 | 0:38dbd24805dd | 167 | duracion_tono = coolterm_comand; // lo almacenamos en: duracion_tono |
CCastrop1012 | 0:38dbd24805dd | 168 | |
CCastrop1012 | 0:38dbd24805dd | 169 | // ESPERAMOS A RECBIR UN COMANDO DESDE EL COOLTERM DIFERENTE AL ULTIMO RECIBIDO |
CCastrop1012 | 0:38dbd24805dd | 170 | while(coolterm_comand == duracion_tono) wait_ms(1); |
CCastrop1012 | 0:38dbd24805dd | 171 | tipo_tono = coolterm_comand; // lo almacenamos en: tipo_tono |
CCastrop1012 | 0:38dbd24805dd | 172 | |
CCastrop1012 | 0:38dbd24805dd | 173 | //// Desactivamos la interrupcion serial o recepcion de datos PORQUE NO NECESITAMOS recibir mas datos por AHORA |
CCastrop1012 | 0:38dbd24805dd | 174 | SerialPort.attach(0, Serial::RxIrq); |
CCastrop1012 | 0:38dbd24805dd | 175 | // Ejecutamos la Funcion LeerColor(); |
CCastrop1012 | 0:38dbd24805dd | 176 | Buzzer_Tone(tipo_tono, duracion_tono); |
CCastrop1012 | 0:38dbd24805dd | 177 | |
CCastrop1012 | 0:38dbd24805dd | 178 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 179 | |
CCastrop1012 | 0:38dbd24805dd | 180 | // si no fue ninguno de los valores anteriores entonces: |
CCastrop1012 | 0:38dbd24805dd | 181 | default: SerialPort.abort_read(); |
CCastrop1012 | 0:38dbd24805dd | 182 | SerialPort.printf("TeleComando desconocido, inicie nuevamente !!\n"); |
CCastrop1012 | 0:38dbd24805dd | 183 | |
CCastrop1012 | 0:38dbd24805dd | 184 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 185 | |
CCastrop1012 | 0:38dbd24805dd | 186 | } |
CCastrop1012 | 0:38dbd24805dd | 187 | |
CCastrop1012 | 0:38dbd24805dd | 188 | |
CCastrop1012 | 0:38dbd24805dd | 189 | // Re inicializamos nuestras variables de control a sus valores iniciales |
CCastrop1012 | 0:38dbd24805dd | 190 | // Para no seguir entrando a las sentencias IF |
CCastrop1012 | 0:38dbd24805dd | 191 | programa_ejecutar = 0; coolterm_comand = 0; |
CCastrop1012 | 0:38dbd24805dd | 192 | |
CCastrop1012 | 0:38dbd24805dd | 193 | |
CCastrop1012 | 0:38dbd24805dd | 194 | //// HABILITAMOS NUEVAMENTE la interrupcion serial o recepcion de datos |
CCastrop1012 | 0:38dbd24805dd | 195 | SerialPort.attach(&ReadPort, Serial::RxIrq); |
CCastrop1012 | 0:38dbd24805dd | 196 | |
CCastrop1012 | 0:38dbd24805dd | 197 | } // Finaliza el IF |
CCastrop1012 | 0:38dbd24805dd | 198 | |
CCastrop1012 | 0:38dbd24805dd | 199 | |
CCastrop1012 | 0:38dbd24805dd | 200 | |
CCastrop1012 | 0:38dbd24805dd | 201 | |
CCastrop1012 | 0:38dbd24805dd | 202 | }// Finaliza el WHILE |
CCastrop1012 | 0:38dbd24805dd | 203 | |
CCastrop1012 | 0:38dbd24805dd | 204 | |
CCastrop1012 | 0:38dbd24805dd | 205 | } // Finaliza el main |
CCastrop1012 | 0:38dbd24805dd | 206 | |
CCastrop1012 | 0:38dbd24805dd | 207 | void Buzzer_Tone(uint8_t tipo_tono, uint8_t duracion_tono) |
CCastrop1012 | 0:38dbd24805dd | 208 | { |
CCastrop1012 | 0:38dbd24805dd | 209 | |
CCastrop1012 | 0:38dbd24805dd | 210 | |
CCastrop1012 | 0:38dbd24805dd | 211 | switch (tipo_tono) |
CCastrop1012 | 0:38dbd24805dd | 212 | { |
CCastrop1012 | 0:38dbd24805dd | 213 | |
CCastrop1012 | 0:38dbd24805dd | 214 | case TONO_DO: Buzzer.period_ms(DO); |
CCastrop1012 | 0:38dbd24805dd | 215 | //SerialPort.printf("Tono Seleccionado DO!!\n"); |
CCastrop1012 | 0:38dbd24805dd | 216 | |
CCastrop1012 | 0:38dbd24805dd | 217 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 218 | |
CCastrop1012 | 0:38dbd24805dd | 219 | case TONO_RE: Buzzer.period_ms(RE); |
CCastrop1012 | 0:38dbd24805dd | 220 | //SerialPort.printf("Tono Seleccionado RE!!\n"); |
CCastrop1012 | 0:38dbd24805dd | 221 | |
CCastrop1012 | 0:38dbd24805dd | 222 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 223 | |
CCastrop1012 | 0:38dbd24805dd | 224 | case TONO_MI: Buzzer.period_ms(MI); |
CCastrop1012 | 0:38dbd24805dd | 225 | //SerialPort.printf("Tono Seleccionado MI!!\n"); |
CCastrop1012 | 0:38dbd24805dd | 226 | |
CCastrop1012 | 0:38dbd24805dd | 227 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 228 | |
CCastrop1012 | 0:38dbd24805dd | 229 | case TONO_SI: Buzzer.period_ms(SI); |
CCastrop1012 | 0:38dbd24805dd | 230 | //SerialPort.printf("Tono Seleccionado SI!!\n"); |
CCastrop1012 | 0:38dbd24805dd | 231 | |
CCastrop1012 | 0:38dbd24805dd | 232 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 233 | |
CCastrop1012 | 0:38dbd24805dd | 234 | // si no fue ninguno de los valores anteriores entonces: |
CCastrop1012 | 0:38dbd24805dd | 235 | default: SerialPort.printf("teleComando desconocido, inicie nuevamente !!\n"); |
CCastrop1012 | 0:38dbd24805dd | 236 | |
CCastrop1012 | 0:38dbd24805dd | 237 | break; // salimos del SWITCH |
CCastrop1012 | 0:38dbd24805dd | 238 | |
CCastrop1012 | 0:38dbd24805dd | 239 | } |
CCastrop1012 | 0:38dbd24805dd | 240 | // COMO EL CICLO UTIL DEL BUZZER ESTABA EN 0, POR LO CUAL NO SONABA |
CCastrop1012 | 0:38dbd24805dd | 241 | // SE PONE AL 50% DEL PERIODO |
CCastrop1012 | 0:38dbd24805dd | 242 | Buzzer.write(0.5); |
CCastrop1012 | 0:38dbd24805dd | 243 | // SE ESPERA DURANTE EN TIEMPO INGRESADO (EN SEGUNDOS ) |
CCastrop1012 | 0:38dbd24805dd | 244 | wait(duracion_tono); |
CCastrop1012 | 0:38dbd24805dd | 245 | |
CCastrop1012 | 0:38dbd24805dd | 246 | // Se Reinicializa el Periodo y el Ciclo útil de la señal PWM |
CCastrop1012 | 0:38dbd24805dd | 247 | // que va al Buzzer |
CCastrop1012 | 0:38dbd24805dd | 248 | Buzzer.period_ms(1); |
CCastrop1012 | 0:38dbd24805dd | 249 | Buzzer.write(0); |
CCastrop1012 | 0:38dbd24805dd | 250 | |
CCastrop1012 | 0:38dbd24805dd | 251 | |
CCastrop1012 | 0:38dbd24805dd | 252 | |
CCastrop1012 | 0:38dbd24805dd | 253 | } |
CCastrop1012 | 0:38dbd24805dd | 254 | |
CCastrop1012 | 0:38dbd24805dd | 255 | |
CCastrop1012 | 0:38dbd24805dd | 256 | |
CCastrop1012 | 0:38dbd24805dd | 257 | |
CCastrop1012 | 0:38dbd24805dd | 258 | void leer_color() |
CCastrop1012 | 0:38dbd24805dd | 259 | { |
CCastrop1012 | 0:38dbd24805dd | 260 | |
CCastrop1012 | 0:38dbd24805dd | 261 | red = SENSOR_COLOR.ReadRed(); // OBTENEMOS EL TIEMPO DEL CICLO UTIL DE LA FRECUENCIA DE SALIDA |
CCastrop1012 | 0:38dbd24805dd | 262 | green = SENSOR_COLOR.ReadGreen(); |
CCastrop1012 | 0:38dbd24805dd | 263 | blue = SENSOR_COLOR.ReadBlue(); |
CCastrop1012 | 0:38dbd24805dd | 264 | clear = SENSOR_COLOR.ReadClear(); |
CCastrop1012 | 0:38dbd24805dd | 265 | |
CCastrop1012 | 0:38dbd24805dd | 266 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:38dbd24805dd | 267 | |
CCastrop1012 | 0:38dbd24805dd | 268 | red *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:38dbd24805dd | 269 | blue *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:38dbd24805dd | 270 | green *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:38dbd24805dd | 271 | clear *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:38dbd24805dd | 272 | |
CCastrop1012 | 0:38dbd24805dd | 273 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:38dbd24805dd | 274 | |
CCastrop1012 | 0:38dbd24805dd | 275 | |
CCastrop1012 | 0:38dbd24805dd | 276 | ////////////////////////////////////////////////////////////// |
CCastrop1012 | 0:38dbd24805dd | 277 | //// identificar azul |
CCastrop1012 | 0:38dbd24805dd | 278 | |
CCastrop1012 | 0:38dbd24805dd | 279 | |
CCastrop1012 | 0:38dbd24805dd | 280 | if(red <=42 && red >=24) |
CCastrop1012 | 0:38dbd24805dd | 281 | { |
CCastrop1012 | 0:38dbd24805dd | 282 | if(green >= 20 && green <= 28 ) |
CCastrop1012 | 0:38dbd24805dd | 283 | { |
CCastrop1012 | 0:38dbd24805dd | 284 | if(blue >= 10 && blue <= 16) |
CCastrop1012 | 0:38dbd24805dd | 285 | { |
CCastrop1012 | 0:38dbd24805dd | 286 | color_identificado = CMD_azul; |
CCastrop1012 | 0:38dbd24805dd | 287 | printf ( "0x0%1x\n ", CMD_azul); |
CCastrop1012 | 0:38dbd24805dd | 288 | Buzzer.period_ms(DO); |
CCastrop1012 | 0:38dbd24805dd | 289 | Buzzer.write(0.5); |
CCastrop1012 | 0:38dbd24805dd | 290 | wait(4); |
CCastrop1012 | 0:38dbd24805dd | 291 | Buzzer.write(0); |
CCastrop1012 | 0:38dbd24805dd | 292 | |
CCastrop1012 | 0:38dbd24805dd | 293 | } |
CCastrop1012 | 0:38dbd24805dd | 294 | } |
CCastrop1012 | 0:38dbd24805dd | 295 | } |
CCastrop1012 | 0:38dbd24805dd | 296 | |
CCastrop1012 | 0:38dbd24805dd | 297 | |
CCastrop1012 | 0:38dbd24805dd | 298 | |
CCastrop1012 | 0:38dbd24805dd | 299 | |
CCastrop1012 | 0:38dbd24805dd | 300 | ///////////////////////////////////////////////////////////// |
CCastrop1012 | 0:38dbd24805dd | 301 | /// identificar rojo |
CCastrop1012 | 0:38dbd24805dd | 302 | if(red <= 12 ) |
CCastrop1012 | 0:38dbd24805dd | 303 | { |
CCastrop1012 | 0:38dbd24805dd | 304 | if(green >= 10 && green <= 28 ) |
CCastrop1012 | 0:38dbd24805dd | 305 | { |
CCastrop1012 | 0:38dbd24805dd | 306 | if(blue >= 18 && blue <= 24) |
CCastrop1012 | 0:38dbd24805dd | 307 | { |
CCastrop1012 | 0:38dbd24805dd | 308 | color_identificado = CMD_rojo; |
CCastrop1012 | 0:38dbd24805dd | 309 | printf ( "0x0%1x\n ", CMD_rojo ); |
CCastrop1012 | 0:38dbd24805dd | 310 | Buzzer.period_ms(RE); |
CCastrop1012 | 0:38dbd24805dd | 311 | Buzzer.write(0.5); //PERIODO UTIL |
CCastrop1012 | 0:38dbd24805dd | 312 | wait(4); //TIEMPO ACTIVO DEL BUZZER |
CCastrop1012 | 0:38dbd24805dd | 313 | Buzzer.write(0.0); |
CCastrop1012 | 0:38dbd24805dd | 314 | } |
CCastrop1012 | 0:38dbd24805dd | 315 | } |
CCastrop1012 | 0:38dbd24805dd | 316 | |
CCastrop1012 | 0:38dbd24805dd | 317 | if(green < 10 && green >= 6 ) |
CCastrop1012 | 0:38dbd24805dd | 318 | { |
CCastrop1012 | 0:38dbd24805dd | 319 | if(blue <= 12 ) |
CCastrop1012 | 0:38dbd24805dd | 320 | { |
CCastrop1012 | 0:38dbd24805dd | 321 | color_identificado = CMD_clear; |
CCastrop1012 | 0:38dbd24805dd | 322 | printf ( "0x0%1x \n ", CMD_clear ); |
CCastrop1012 | 0:38dbd24805dd | 323 | Buzzer.period_ms(MI); |
CCastrop1012 | 0:38dbd24805dd | 324 | Buzzer.write(0.5); |
CCastrop1012 | 0:38dbd24805dd | 325 | wait(4); |
CCastrop1012 | 0:38dbd24805dd | 326 | Buzzer.write(0); |
CCastrop1012 | 0:38dbd24805dd | 327 | } |
CCastrop1012 | 0:38dbd24805dd | 328 | |
CCastrop1012 | 0:38dbd24805dd | 329 | } |
CCastrop1012 | 0:38dbd24805dd | 330 | |
CCastrop1012 | 0:38dbd24805dd | 331 | } |
CCastrop1012 | 0:38dbd24805dd | 332 | |
CCastrop1012 | 0:38dbd24805dd | 333 | |
CCastrop1012 | 0:38dbd24805dd | 334 | if(green >= 36 && green <= 44 ) |
CCastrop1012 | 0:38dbd24805dd | 335 | { |
CCastrop1012 | 0:38dbd24805dd | 336 | if(red >= 40 && red <= 50 ) |
CCastrop1012 | 0:38dbd24805dd | 337 | |
CCastrop1012 | 0:38dbd24805dd | 338 | { |
CCastrop1012 | 0:38dbd24805dd | 339 | color_identificado = CMD_verde; |
CCastrop1012 | 0:38dbd24805dd | 340 | printf ( "0x0%1x \n ", CMD_verde ); |
CCastrop1012 | 0:38dbd24805dd | 341 | Buzzer.period_ms(SI); |
CCastrop1012 | 0:38dbd24805dd | 342 | Buzzer.write(0.5); |
CCastrop1012 | 0:38dbd24805dd | 343 | wait(4); |
CCastrop1012 | 0:38dbd24805dd | 344 | Buzzer.write(0); |
CCastrop1012 | 0:38dbd24805dd | 345 | |
CCastrop1012 | 0:38dbd24805dd | 346 | |
CCastrop1012 | 0:38dbd24805dd | 347 | } |
CCastrop1012 | 0:38dbd24805dd | 348 | } |
CCastrop1012 | 0:38dbd24805dd | 349 | |
CCastrop1012 | 0:38dbd24805dd | 350 | if (color_identificado == ColorNoIdentificado) |
CCastrop1012 | 0:38dbd24805dd | 351 | { |
CCastrop1012 | 0:38dbd24805dd | 352 | |
CCastrop1012 | 0:38dbd24805dd | 353 | |
CCastrop1012 | 0:38dbd24805dd | 354 | printf ( "0x0%1x \n ", ColorNoIdentificado); |
CCastrop1012 | 0:38dbd24805dd | 355 | Buzzer.period_ms(10); |
CCastrop1012 | 0:38dbd24805dd | 356 | Buzzer.write(0.5); |
CCastrop1012 | 0:38dbd24805dd | 357 | wait(4); |
CCastrop1012 | 0:38dbd24805dd | 358 | Buzzer.write(0); |
CCastrop1012 | 0:38dbd24805dd | 359 | |
CCastrop1012 | 0:38dbd24805dd | 360 | |
CCastrop1012 | 0:38dbd24805dd | 361 | } |
CCastrop1012 | 0:38dbd24805dd | 362 | |
CCastrop1012 | 0:38dbd24805dd | 363 | color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:38dbd24805dd | 364 | } |
CCastrop1012 | 0:38dbd24805dd | 365 | |
CCastrop1012 | 0:38dbd24805dd | 366 | |
CCastrop1012 | 0:38dbd24805dd | 367 |