prototype/prototype/prototype.ino
2023-06-12 09:18:39 -05:00

128 lines
2.7 KiB
C++

#include <LiquidCrystal_I2C.h>
#include <millisDelay.h>
#include <IRremote.h>
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();
}
}