Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DebouncedIn PinDetect Pulse mbed
main.cpp
00001 #include "mbed.h" 00002 #include <Pulse.h> 00003 #include "DebouncedIn.h" 00004 #define off 1 00005 #define on 0 00006 00007 Serial pc(USBTX,USBRX); // Abre puerto serial 00008 Serial GSM(PTE0,PTE1); 00009 00010 DigitalOut LedVerde(LED2,off); 00011 DigitalOut LedRojo(LED1,off); 00012 DigitalOut LedAzul(LED3,off); 00013 //DigitalOut bomba(PTA17); // Pin para activar la bomba 00014 PulseInOut pulseIn(PTD4); // Generamos los pulsos 00015 00016 //DebouncedIn levelBalde(PTC17); // Sensor de nivel del balde 00017 //DebouncedIn levelTanque(PTC16); // Sensor de nivel en el tanque de almacenamiento 00018 AnalogIn input(PTC2); 00019 00020 int cPulsos; // Cuenta los pulsos 00021 int limBmax = 5; // Límite de agua máximo en el balde. 50 cm. 00022 //int limBmin = 1; // Límite de agua mínimo en el balde. 10 cm 00023 int i, j, k, m, num; 00024 int bomba = 0; // Vamos a generar 3 ciclos de llenado del tanque. Al tercer ciclo, el balde quedará vacío y se deberá detectar de nuevo flujo. 00025 bool control = true; // Para terminar el programa. 00026 00027 void pulsos() // Generación de pulsos 00028 { 00029 pulseIn.write_us(0, 200000); // 200 ms tiempo bajo 00030 cPulsos++; // Cuenta cuando hay un flanco de bajada 00031 pulseIn.write_us(1, 50000); // 50 ms arriba 00032 } 00033 00034 void sendData(float input) 00035 { 00036 wait(1); 00037 num = input*1000; //agrando el numero de cero a mil 00038 GSM.putc(num); 00039 pc.printf("Nivel del tanque: %f [m].\r\n", input); 00040 if(num < 256) { //debo generar dos casos a APP inventor solo me recibe hex asi: 0xhhhh (4 cifras) 00041 GSM.putc(0); //si el numero es hasta 255 se le ponen dos ceros adelante a la secuencia de bits 00042 GSM.putc(i); //luego la cifra menos significativa 00043 //pc.printf("Cero: %d\r\n", 0); 00044 //pc.printf("m: %d\r\n", m); 00045 } 00046 if(num >255) { //pero si es mayor a 255 las cifras deben ser convertidas a un hex de dos bytes de la siguiente forma 00047 j = num/256; //calculo la cifra mas significativa 00048 k = num - j*256; //calculo la cifra menos significativa 00049 GSM.putc(j); //las envio a la usart para que se las ponga al modulo bluetooth y la lleve al android 00050 GSM.putc(k); //mas significativa primero, menos despues si no no funciona!!! y con la orden PUTC solo asi le envia binarios 00051 //pc.printf("j: %d\r\n", j); 00052 //pc.printf("k: %d\r\n", k); 00053 } 00054 } 00055 00056 00057 int main() 00058 { 00059 GSM.baud(9600); // asigno baudios y configuro puerto serie de la usart 00060 GSM.format(8,Serial::None,1); 00061 wait(1); 00062 pc.printf("Ahorrador de agua\r\n"); 00063 pc.printf("El programa se esta iniciando\r\n"); 00064 wait(0.2); 00065 LedVerde = on; 00066 wait(0.2); 00067 LedVerde = off; 00068 wait(0.2); 00069 LedRojo = on; 00070 wait(0.2); 00071 LedRojo = off; 00072 wait(0.2); 00073 LedAzul = on; 00074 wait(0.2); 00075 LedAzul = off; 00076 wait(0.2); 00077 00078 00079 // Inicia el ciclo 00080 while(control) { 00081 waitPulse: // Acá leeríamos los pulsos 00082 00083 for(i = 0; i <= 3; i++) { 00084 pulsos(); 00085 pc.printf("Esperando senal de flujo...\r\n"); 00086 if (cPulsos == 3) { 00087 goto seguir; 00088 } else goto waitPulse; 00089 } 00090 seguir: 00091 bomba++; 00092 if(bomba == 3) { 00093 goto recierre; 00094 } else { 00095 cPulsos = 0; 00096 pc.printf("\r\n"); 00097 pc.printf("Se detecta flujo.\r\n"); 00098 LedVerde = on; 00099 wait(1); 00100 LedVerde = off; 00101 goto llenarBalde; 00102 } 00103 00104 00105 00106 llenarBalde: // Llenar balde donde está la bomba 00107 int ii = 0; 00108 while(ii <= limBmax) { // Ciclo para llenar el balde 00109 if(ii == limBmax) { 00110 pc.printf("El balde de almacenamiento se ha llenado. Se inicia el bombeo.\r\n"); 00111 goto llenarTanque; 00112 } else { 00113 pc.printf("Llenando balde\r\n"); 00114 wait(1); 00115 } 00116 ii++; 00117 } 00118 00119 00120 recierre: 00121 pc.printf("No hay agua para bombear al tanque.\r\n"); 00122 LedRojo = on; 00123 wait(2); 00124 LedRojo = off; 00125 wait(2); 00126 00127 pc.printf("Digite f para detectar el flujo, s para salir: "); 00128 char v = pc.getc(); 00129 00130 switch(v) { 00131 case 'f': 00132 bomba = 0; 00133 goto waitPulse; 00134 break; 00135 case 's': 00136 pc.printf("La bomba se apagara por seguridad.\r\n"); 00137 while(1){ 00138 LedRojo = on; 00139 wait(0.2); 00140 LedRojo = off; 00141 wait(0.2); 00142 } 00143 break; 00144 } 00145 00146 00147 llenarTanque: 00148 float limTmax = 1; 00149 for(float l = 0; l <= limTmax; l += 0.033333333) { 00150 if (l == limTmax) { // Significa que se ha llenado el tanque y se debe alertar. 00151 pc.printf("Se ha llenado el tanque. Abra la valvula del desague.\r\n"); 00152 goto final; 00153 } else { 00154 sendData(l); // Enviar dato a la aplicación de llenado de tanque 00155 } 00156 } 00157 00158 00159 final: 00160 LedRojo = on; 00161 wait(1); 00162 LedRojo = off; 00163 wait(1); 00164 00165 pc.printf("Digite 'a' para abrir la valvula del tanque: "); 00166 char c = pc.getc(); 00167 //char c = 'a'; // Agilidad 00168 pc.printf("\r\n"); 00169 pc.printf("Se inicia la descarga del tanque. \r\n"); 00170 00171 LedVerde = on; 00172 wait(1); 00173 LedVerde = off; 00174 wait(1); 00175 00176 if(c == 'a') { 00177 float limTmin = 0; 00178 for(float l1 = 1; l1 >= limTmin; l1 -= 0.033333333) { 00179 if (l1 == 0) { // Significa que se ha llenado el tanque y se debe alertar. 00180 pc.printf("Se ha liberado el tanque. Comprobando flujo...\r\n"); 00181 wait(1); 00182 goto llenarBalde; 00183 } else { 00184 sendData(l1); // Enviar dato a la aplicación de llenado de tanque 00185 } 00186 } 00187 } 00188 } 00189 } 00190 00191
Generated on Sat Jul 30 2022 20:20:30 by
1.7.2