Sistema de control de un cooler a lazo abierto y cerrado

Dependencies:   mbed DHT11

Files at this revision

API Documentation at this revision

Comitter:
tzuran
Date:
Wed May 29 22:07:41 2019 +0000
Parent:
0:da7b1c04a659
Commit message:
Version final

Changed in this revision

DHT11.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r da7b1c04a659 -r 472a0e143e52 DHT11.lib
--- a/DHT11.lib	Thu Sep 11 13:50:10 2014 +0000
+++ b/DHT11.lib	Wed May 29 22:07:41 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/s_inoue_mbed/code/DHT11/#056d1e9b428c
+https://os.mbed.com/users/fossum_13/code/DHT11/#5da6f6de3e42
diff -r da7b1c04a659 -r 472a0e143e52 TextLCD.lib
--- a/TextLCD.lib	Thu Sep 11 13:50:10 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/simon/code/TextLCD/#308d188a2d3a
diff -r da7b1c04a659 -r 472a0e143e52 main.cpp
--- a/main.cpp	Thu Sep 11 13:50:10 2014 +0000
+++ b/main.cpp	Wed May 29 22:07:41 2019 +0000
@@ -1,41 +1,155 @@
-/*
- * A program for the use of the DHT11, a temperature and humidity sensor
- * Shigenori Inoue, September 10, 2014
- */
- 
 #include "mbed.h"
-#include "TextLCD.h"
-#include "DHT11.h"
+#include "Dht11.h"
+
+//TICKERS CON FUNCIONES Y VARIABLES QUE SE USAN CON EL
+Ticker sec;
+void segundo();
+int mandar = 0;
+int contador = 0;
+
+//SENSOR DE TEMPERATURA
+Dht11 sensor(D2);
+int temperatura = 0;
+
+//ENTRADA DEL SENSOR DEL MOTOR
+DigitalIn rpm(D3);
+
+//SALIDA PWM PARA LA INTERFAZ DEL MOTOR
+PwmOut motor(D4);
+
+//ENTRADA ANALOGICA PARA EL PRESET
+AnalogIn preset(A0);
 
-// LEDs for debugging
-BusOut leds(LED4, LED3, LED2, LED1);
+//BOTON DE RESTART
+Ticker boton;
+DigitalIn restartButton(PTB8);
+void button_in();
+unsigned char state1 = 0x00;
+int button_output = 0;
 
-// LCD module
-TextLCD lcd(p25, p24, p12, p13, p14, p23);
+//MAQUINA DE ESTADOS CUENTA DE VUELTAS
+void ME_RPM();
+enum ME_RPM_ESTADOS { ESPERO_FA, ESPERO_FD };
+ME_RPM_ESTADOS ME_RPM_ESTADO;
+int cuenta_vueltas = 0;
 
-// Humidity sensor
-DHT11 d(p18);
+//MAQUINA DE ESTADOS GENERAL
+void ME_GENERAL();
+enum ME_GENERAL_ESTADOS { LAZO_A, LAZO_C };
+ME_GENERAL_ESTADOS ME_GENERAL_ESTADO;
 
-// The main function
+//VARIABLE QUE GUARDA EL NIVEL DE DUTY QUE CALCULA EL PROGRAMA
+float duty = 0;
+
 int main()
 {
-    int state;
+    //INICIO EL MOTOR PRENDIDO AL MAXIMO PARA QUE SE LE PUEDA BAJAR EL DUTY POSTERIORMENTE
+    
+    motor = 1.0;
+    wait(2);
+
+    //PULLUP PARA LA ENTRADA DEL BOTON DE CAMBIO DE MODO
+    restartButton.mode(PullUp);
 
-    lcd.cls();
-    lcd.locate(0, 0);
-    lcd.printf("DHT11 Humidity");
+    //TICKERS
+    sec.attach(&segundo, 1);
+    boton.attach(&button_in, 0.01);
 
-    while(true) {
-        state = d.readData();
+    //ESTADO INICIAL MAQUINAS DE ESTADO
+    ME_RPM_ESTADO = ESPERO_FA;
+    ME_GENERAL_ESTADO = LAZO_A;
 
-        if (state != DHT11::OK) {
-            lcd.locate(0, 1);
-            lcd.printf("Error: %d", state);
+    while(1) {
+        ME_RPM();
+        ME_GENERAL();
+        
+        //CALCULO DE DUTY PARA CADA LAZO
+        if(ME_GENERAL_ESTADO == LAZO_A) {
+            if(preset < 0.035) { 
+                duty = 0.035; // 200 RPM - 0.035
+            } else {
+                duty = preset;
+            }
         } else {
-            lcd.locate(0, 1);
-            lcd.printf("T: %dC, H: %d%%", d.readTemperature(), d.readHumidity());
+            duty = temperatura < 20 ? 0.035 : temperatura / 70.0;
         }
-        leds = leds + 1;
-        wait(2.0);
+
+        //ENVIO DE DATOS A LA PC
+        if(mandar) {
+            printf("T: %d, Modo: %d\r\n", sensor.getCelsius(), ME_GENERAL_ESTADO);
+            printf("RPM: %d Duty: %f\r\n", cuenta_vueltas * 60, duty * 100);
+            printf("Boton: %d\n\r", button_output);
+            cuenta_vueltas = 0;
+            mandar = 0;
+            motor = duty;
+        }
     }
 }
+
+//TICKER 1 seg
+void segundo()
+{
+    //FLAG DE ENVIO DE DATOS A LA PC
+    mandar = 1;
+    
+    //LECTURA DE LA TEMEPERATURA DEL SENSOR
+    sensor.read();
+    temperatura = sensor.getCelsius();
+    
+    //CONTADOR 1 seg
+    contador++;
+}
+
+void ME_RPM()
+{
+    switch(ME_RPM_ESTADO) {
+        case ESPERO_FA:
+            //SI SE DETECTA UN FLANCO ASCENDENTE CAMBIO DE MODO
+            if(rpm) {
+                ME_RPM_ESTADO = ESPERO_FD;
+                cuenta_vueltas++;
+            }
+            break;
+        case ESPERO_FD:
+            //SI SE DETECTA UN FLANCO DESCENDENTE CAMBIO DE MODO
+            if(!rpm) {
+                ME_RPM_ESTADO = ESPERO_FA;
+            }
+            break;
+    }
+
+}
+
+void ME_GENERAL()
+{
+    switch(ME_GENERAL_ESTADO) {
+        case LAZO_A:
+            //SI SE APRIETA EL BOTON Y YA PASO UN SEGUNDO DESDE EL ULTIMO CAMBIO DE MODO SE PRODUCE UN CAMBIO
+            if(button_output == 1 && contador > 1) {
+                ME_GENERAL_ESTADO = LAZO_C;
+                contador = 0;
+            }
+            break;
+        case LAZO_C:
+            //SI SE APRIETA EL BOTON Y YA PASO UN SEGUNDO DESDE EL ULTIMO CAMBIO DE MODO SE PRODUCE UN CAMBIO
+            if(button_output == 1 && contador > 1) {
+                ME_GENERAL_ESTADO = LAZO_A;
+                contador = 0;
+            }
+            break;
+    }
+}
+
+//MUESTREO DEL BOTON PARA ELIMINACION DEL REBOTE
+void button_in()
+{
+    state1 = state1 >> 1;
+
+    if(!restartButton) {
+        state1 |= 0x80;
+    }
+    if(state1 == 0xFF)
+        button_output = 1;
+    else if(state1 == 0)
+        button_output = 0;
+}
\ No newline at end of file