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: mbed mbed-rtos 4DGL-uLCD-SE
main.cpp@5:568a15151d11, 2020-04-13 (annotated)
- Committer:
- mknudson3
- Date:
- Mon Apr 13 15:00:47 2020 +0000
- Revision:
- 5:568a15151d11
- Parent:
- 1:c27c61c0a1e0
- Child:
- 6:65bbd44cabd7
initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| emilmont | 1:c27c61c0a1e0 | 1 | #include "mbed.h" |
| emilmont | 1:c27c61c0a1e0 | 2 | #include "rtos.h" |
| mknudson3 | 5:568a15151d11 | 3 | #include "uLCD_4DGL.h" |
| emilmont | 1:c27c61c0a1e0 | 4 | |
| mknudson3 | 5:568a15151d11 | 5 | #define DEBOUNCE_TIME 10 // 10 * 0.5 (main loop) = 5 seconds |
| mknudson3 | 5:568a15151d11 | 6 | |
| mknudson3 | 5:568a15151d11 | 7 | uLCD_4DGL uLCD(p9,p10,p17); // serial tx, serial rx, reset pin; |
| emilmont | 1:c27c61c0a1e0 | 8 | |
| mknudson3 | 5:568a15151d11 | 9 | BusOut leds(LED1, LED2, LED3, LED4); |
| mknudson3 | 5:568a15151d11 | 10 | DigitalOut relay(p21); |
| mknudson3 | 5:568a15151d11 | 11 | AnalogIn tmp36(p19); |
| mknudson3 | 5:568a15151d11 | 12 | AnalogIn pot(p20); |
| mknudson3 | 5:568a15151d11 | 13 | |
| mknudson3 | 5:568a15151d11 | 14 | int currTemp, desiredTemp; |
| mknudson3 | 5:568a15151d11 | 15 | bool isHeating; |
| mknudson3 | 5:568a15151d11 | 16 | |
| mknudson3 | 5:568a15151d11 | 17 | // Helper function |
| mknudson3 | 5:568a15151d11 | 18 | // convert tmp36 reading to degrees fahrenheit |
| mknudson3 | 5:568a15151d11 | 19 | int tmp2f() { |
| mknudson3 | 5:568a15151d11 | 20 | return (tmp36 * 3.3 - 0.5) * 180 + 32; |
| emilmont | 1:c27c61c0a1e0 | 21 | } |
| emilmont | 1:c27c61c0a1e0 | 22 | |
| mknudson3 | 5:568a15151d11 | 23 | void attemptToggle(Timer* timer, bool conditional) { |
| mknudson3 | 5:568a15151d11 | 24 | if (!*timer) { |
| mknudson3 | 5:568a15151d11 | 25 | timer->start(); |
| mknudson3 | 5:568a15151d11 | 26 | } else if (!conditional) { |
| mknudson3 | 5:568a15151d11 | 27 | timer->stop(); |
| mknudson3 | 5:568a15151d11 | 28 | timer->reset(); |
| mknudson3 | 5:568a15151d11 | 29 | } else if (*timer >= DEBOUNCE_TIME) { |
| mknudson3 | 5:568a15151d11 | 30 | timer->stop(); |
| mknudson3 | 5:568a15151d11 | 31 | timer->reset(); |
| mknudson3 | 5:568a15151d11 | 32 | isHeating = !isHeating; |
| mknudson3 | 5:568a15151d11 | 33 | } |
| mknudson3 | 5:568a15151d11 | 34 | } |
| mknudson3 | 5:568a15151d11 | 35 | |
| mknudson3 | 5:568a15151d11 | 36 | int main() { |
| mknudson3 | 5:568a15151d11 | 37 | |
| mknudson3 | 5:568a15151d11 | 38 | uLCD.printf("\nCurrent temp:\n\nDesired temp:"); |
| mknudson3 | 5:568a15151d11 | 39 | |
| mknudson3 | 5:568a15151d11 | 40 | bool shouldHeat = false; |
| mknudson3 | 5:568a15151d11 | 41 | isHeating = false; |
| emilmont | 1:c27c61c0a1e0 | 42 | |
| mknudson3 | 5:568a15151d11 | 43 | Timer main, on, off; |
| mknudson3 | 5:568a15151d11 | 44 | main.start(); |
| mknudson3 | 5:568a15151d11 | 45 | while(1) { |
| mknudson3 | 5:568a15151d11 | 46 | if (main.read_ms() > 500) { |
| mknudson3 | 5:568a15151d11 | 47 | main.reset(); |
| mknudson3 | 5:568a15151d11 | 48 | currTemp = tmp2f(); |
| mknudson3 | 5:568a15151d11 | 49 | desiredTemp = 90-pot.read()*35; |
| mknudson3 | 5:568a15151d11 | 50 | shouldHeat = currTemp < desiredTemp; |
| emilmont | 1:c27c61c0a1e0 | 51 | |
| mknudson3 | 5:568a15151d11 | 52 | uLCD.locate(14, 1); |
| mknudson3 | 5:568a15151d11 | 53 | uLCD.printf("%d", currTemp); |
| mknudson3 | 5:568a15151d11 | 54 | uLCD.locate(14, 3); |
| mknudson3 | 5:568a15151d11 | 55 | uLCD.printf("%d", desiredTemp); |
| mknudson3 | 5:568a15151d11 | 56 | |
| mknudson3 | 5:568a15151d11 | 57 | |
| mknudson3 | 5:568a15151d11 | 58 | if (isHeating) attemptToggle(&off, !shouldHeat); |
| mknudson3 | 5:568a15151d11 | 59 | if (!isHeating) attemptToggle(&on, shouldHeat); |
| mknudson3 | 5:568a15151d11 | 60 | |
| mknudson3 | 5:568a15151d11 | 61 | leds = relay = isHeating; |
| mknudson3 | 5:568a15151d11 | 62 | } |
| mknudson3 | 5:568a15151d11 | 63 | } |
| emilmont | 1:c27c61c0a1e0 | 64 | } |
