From 8f435970bdd36f3aea80eabf8d4750610f58f0d3 Mon Sep 17 00:00:00 2001 From: Danny Oh Date: Mon, 12 Jun 2023 09:18:39 -0500 Subject: [PATCH] Initial commit --- prototype/prototype.ino | 128 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 prototype/prototype.ino diff --git a/prototype/prototype.ino b/prototype/prototype.ino new file mode 100644 index 0000000..feab29d --- /dev/null +++ b/prototype/prototype.ino @@ -0,0 +1,128 @@ +#include +#include +#include + +LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display +millisDelay timer; + +const bool DEBUG = true; +// Define IR receiver pin +const int IR_RECEIVE_PIN = 2; +// LCD +const int LCD_COLS = 16; +const int LCD_ROWS = 2; + +String getMinutes() { + unsigned long minutes = timer.remaining() / (60 * 1000l); + if (minutes < 10) { + return "0" + String(minutes); + } + return String(minutes); +} + +String getSeconds() { + unsigned long seconds = timer.remaining() % (60 * 1000l) / 1000l; + if (seconds < 10) { + return "0" + String(seconds); + } + return String(seconds); +} + +void lcdUpdate() { + if (timer.isRunning()) { + lcd.setCursor(0, 1); + lcd.print(String(getMinutes()) + ":" + String(getSeconds())); + } else { + lcd.setCursor(0, 1); + lcd.print("No Timer Set"); + } +} + +void lcdClearRow(int line) { + for (int i = 0; i < LCD_COLS; i++) { + lcd.setCursor(i, line); + lcd.print(' '); + } +} + +void powerOn() { + lcdClearRow(0); + lcd.setCursor(0, 0); + lcd.print("Power ON"); +} + +void powerOff() { + lcdClearRow(0); + lcd.setCursor(0, 0); + lcd.print("Power OFF"); +} + +void startTimer(int minutes) { + if (timer.isRunning()) { + timer.stop(); + } else { + timer.start(minutes * 60 * 1000l); + lcdClearRow(1); + } +} + +void addTimerMinutes(int minutes) { + if (timer.isRunning()) { + long millis = (minutes * 60 * 1000l); + if (millis < 0 && timer.remaining() <= abs(millis)) { + timer.finish(); + } else { + timer.start(timer.remaining() + millis); + } + } +} + +void setup() { + Serial.begin(9600); + + lcd.init(); + lcd.clear(); + lcd.backlight(); // Make sure backlight is on + lcd.setCursor(0, 0); + lcd.print("Power OFF"); + lcd.setCursor(0, 1); + lcd.print("No Timer Set"); + + // Initialize the IR receiver + IrReceiver.begin(IR_RECEIVE_PIN, true); + + startTimer(30); +} + +void loop() { + lcdUpdate(); + + if (IrReceiver.decode()) { + if (DEBUG) { + Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); // Print "old" raw data + // Print IR signal info + IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line + IrReceiver.printIRSendUsage(&Serial); // Print the statement required to send this data + // Enable IR receiver for the next signal + } + + switch (IrReceiver.decodedIRData.decodedRawData) { + case 0xEE11CD32: // up + addTimerMinutes(10); + break; + case 0xEB14CD32: // down + addTimerMinutes(-10); + break; + case 0x9C63CD32: // sleep timer + powerOn(); + startTimer(30); + default: + break; + } + IrReceiver.resume(); + } + + if (timer.justFinished()) { + powerOff(); + } +} \ No newline at end of file