Michael Knudson / Mbed 2 deprecated Thermostat

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE

Revision:
5:568a15151d11
Parent:
1:c27c61c0a1e0
Child:
6:65bbd44cabd7
--- a/main.cpp	Tue Jun 04 16:05:58 2013 +0100
+++ b/main.cpp	Mon Apr 13 15:00:47 2020 +0000
@@ -1,24 +1,64 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "uLCD_4DGL.h"
 
-DigitalOut LEDs[4] = {
-    DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
-};
+#define DEBOUNCE_TIME 10 // 10 * 0.5 (main loop) = 5 seconds
+
+uLCD_4DGL uLCD(p9,p10,p17); // serial tx, serial rx, reset pin;
 
-void blink(void const *n) {
-    LEDs[(int)n] = !LEDs[(int)n];
+BusOut leds(LED1, LED2, LED3, LED4);
+DigitalOut relay(p21);
+AnalogIn tmp36(p19);
+AnalogIn pot(p20);
+
+int currTemp, desiredTemp;
+bool isHeating;
+
+// Helper function
+// convert tmp36 reading to degrees fahrenheit
+int tmp2f() {
+    return (tmp36 * 3.3 - 0.5) * 180 + 32;
 }
 
-int main(void) {
-    RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
-    RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
-    RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
-    RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
+void attemptToggle(Timer* timer, bool conditional) {
+    if (!*timer) {
+        timer->start();
+    } else if (!conditional) {
+        timer->stop();
+        timer->reset();
+    } else if (*timer >= DEBOUNCE_TIME) {
+        timer->stop();
+        timer->reset();
+        isHeating = !isHeating;
+    }
+}
+
+int main() {
+    
+    uLCD.printf("\nCurrent temp:\n\nDesired temp:");
+            
+    bool shouldHeat = false;
+    isHeating = false;
     
-    led_1_timer.start(2000);
-    led_2_timer.start(1000);
-    led_3_timer.start(500);
-    led_4_timer.start(250);
+    Timer main, on, off;
+    main.start();
+    while(1) {
+        if (main.read_ms() > 500) {
+            main.reset();
+            currTemp = tmp2f();
+            desiredTemp = 90-pot.read()*35;
+            shouldHeat = currTemp < desiredTemp;
     
-    Thread::wait(osWaitForever);
+            uLCD.locate(14, 1);
+            uLCD.printf("%d", currTemp);
+            uLCD.locate(14, 3);
+            uLCD.printf("%d", desiredTemp);
+            
+            
+            if (isHeating) attemptToggle(&off, !shouldHeat);
+            if (!isHeating) attemptToggle(&on, shouldHeat);
+            
+            leds = relay = isHeating;
+        }
+    }
 }