Michael Knudson / Mbed 2 deprecated Thermostat

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

main.cpp

Committer:
mknudson3
Date:
2020-04-13
Revision:
5:568a15151d11
Parent:
1:c27c61c0a1e0
Child:
6:65bbd44cabd7

File content as of revision 5:568a15151d11:

#include "mbed.h"
#include "rtos.h"
#include "uLCD_4DGL.h"

#define DEBOUNCE_TIME 10 // 10 * 0.5 (main loop) = 5 seconds

uLCD_4DGL uLCD(p9,p10,p17); // serial tx, serial rx, reset pin;

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;
}

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;
    
    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;
    
            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;
        }
    }
}