#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <IRremote.hpp>
#include <Keypad.h>
//#include <stdio.h>
//#include <string.h>
// Create an instance of LiquidCrystal_I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define that the infrared receiver is connected to digital pin 2 on the Arduino
#define IR_PIN 2
// Define keypad
const byte ROWS = 4;
const byte COLUMNS = 4;
char hexaKeys[ROWS][COLUMNS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 10, 9, 8, 7 }; // R1 > R2 > R3 > R4
byte columnPins[COLUMNS] = { 6, 5, 4, 3 }; // C1 > C2 > C3 > C4
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, columnPins, ROWS, COLUMNS); // Create an instance of Keypad
// Defining buttons on the remote
const int IR_BUTTON_OK = 168;
const int IR_BUTTON_UP = 2;
const int IR_BUTTON_DOWN = 152;
const int IR_BUTTON_LEFT = 224;
const int IR_BUTTON_RIGHT = 144;
const int IR_BUTTON_1 = 48;
const int IR_BUTTON_2 = 24;
const int IR_BUTTON_3 = 122;
const int IR_BUTTON_4 = 16;
const int IR_BUTTON_5 = 56;
const int IR_BUTTON_6 = 90;
const int IR_BUTTON_7 = 66;
const int IR_BUTTON_8 = 74;
const int IR_BUTTON_9 = 82;
const int IR_BUTTON_0 = 104;
const int IR_BUTTON_ASTERISK = -56;
const int IR_BUTTON_POUND = -55;
bool numLoadsOut[4] = { 0, 0, 0, 0 };
const int loadList[5] = { IR_BUTTON_1, IR_BUTTON_2, IR_BUTTON_3, IR_BUTTON_4, IR_BUTTON_5 };
const int buttonList[17] = {
IR_BUTTON_0,
IR_BUTTON_1,
IR_BUTTON_2,
IR_BUTTON_3,
IR_BUTTON_4,
IR_BUTTON_5,
IR_BUTTON_6,
IR_BUTTON_7,
IR_BUTTON_8,
IR_BUTTON_9,
IR_BUTTON_OK,
IR_BUTTON_LEFT,
IR_BUTTON_RIGHT,
IR_BUTTON_UP,
IR_BUTTON_DOWN,
IR_BUTTON_POUND,
IR_BUTTON_ASTERISK
};
unsigned long currentMillisLoadOne = 0;
unsigned long currentMillisLoadTwo = 0;
unsigned long currentMillisLoadThree = 0;
unsigned long currentMillisLoadFour = 0;
// Added from Tinkercad >
#define button 2
#define debounceTimeout 10
#define loadLimit 50
// < Added from Tinkercad
int previousCommand = 0;
int delayTime = 500;
bool receiverActive = true;
// Controlling the loads (they're off by default)
bool loadOneOff = true;
bool loadTwoOff = true;
bool loadThreeOff = true;
bool loadFourOff = true;
// Tells if load timers are currently active
bool loadOneTimerActive = false;
bool loadTwoTimerActive = false;
bool loadThreeTimerActive = false;
bool loadFourTimerActive = false;
// Tells if load timers were recently active
bool loadOneTimerRecentlyActive = false;
bool loadTwoTimerRecentlyActive = false;
bool loadThreeTimerRecentlyActive = false;
bool loadFourTimerRecentlyActive = false;
// Digital pins for the relays
const int relayOnePin = 22;
const int relayTwoPin = 23;
const int relayThreePin = 24;
const int relayFourPin = 25;
// Analog pins for the current transformers
const int ctOnePin = A1;
const int ctTwoPin = A2;
const int ctThreePin = A3;
const int ctFourPin = A4;
// Indicates which load's screen is currently active
int activeLoadScreen = 0;
// For load timers
int loadOneHours;
int loadOneMinutes;
int loadOneSeconds;
int loadTwoHours;
int loadTwoMinutes;
int loadTwoSeconds;
int loadThreeHours;
int loadThreeMinutes;
int loadThreeSeconds;
int loadFourHours;
int loadFourMinutes;
int loadFourSeconds;
// Indicates if a function is being run for the first time or consecutively
bool firstWelcomeScreenRun = true;
bool firstLoopRun = true;
bool firstLoadOneTimerRun = true;
bool firstLoadTwoTimerRun = true;
bool firstLoadThreeTimerRun = true;
bool firstLoadFourTimerRun = true;
// Added from Tinkercad >
bool out1 = 1;
bool out2 = 1;
bool out3 = 1;
bool out4 = 1;
bool delayRunning = false;
int loadsOut = 0;
int delayStart;
int selectedLoad = 1;
int buttonHoldDelay = 500;
// Load priorities
int loadOnePriority = 1;
int loadTwoPriority = 2;
int loadThreePriority = 3;
int loadFourPriority = 4;
// Current sensor values
float previousCurrentReading;
float sensorValue1 = 0;
float sensorValue2 = 0;
float sensorValue3 = 0;
float sensorValue4 = 0;
float total = 0;
//long int lastDebounceTime;
// < Added from Tinkercad
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Setting up the LCD
lcd.init();
lcd.backlight();
// Setting up the IR receiver
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
receiverActive = true;
// Added from Tinkercad >
//DDRB = B1111;
/*
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
*/
//pinMode(button, INPUT);
delayStart = millis();
//delayRunning = true;
// < Added from Tinkercad
// These digital pins are connected to the relays
pinMode(relayOnePin, OUTPUT);
pinMode(relayTwoPin, OUTPUT);
pinMode(relayThreePin, OUTPUT);
pinMode(relayFourPin, OUTPUT);
for (int i = relayOnePin; i <= relayFourPin; i++) {
digitalWrite(i, LOW); // Change in actual circuit
//delay(1000); // Change in actual circuit
}
} // setup()
void loop() {
// put your main code here, to run repeatedly:
int command;
int warningMessageDelayTime = 2000; // In milliseconds
int remoteDisableTime = 500; // In milliseconds
bool whileLoopBreak = false; // For breaking out of the welcome screen
// Calculating total current being pulled by all loads
total = sensorValue1 + sensorValue2 + sensorValue3 + sensorValue4; // Fix
CheckLoadStatuses();
// Run load timers if they're active >
CheckTimerStatuses();
if (loadOneTimerActive) {
Timer(1);
} else if (!loadOneTimerActive && loadOneTimerRecentlyActive) {
loadOneOff = !loadOneOff;
loadOneTimerRecentlyActive = false;
if (selectedLoad == IR_BUTTON_1) {
Serial.print("loadOneOff = ");
Serial.println(loadOneOff);
RetrieveLoadInfo(IR_BUTTON_1);
}
}
if (loadTwoTimerActive) {
Timer(2);
} else if (!loadTwoTimerActive && loadTwoTimerRecentlyActive) {
loadTwoOff = !loadTwoOff;
loadTwoTimerRecentlyActive = false;
if (selectedLoad == IR_BUTTON_2) {
RetrieveLoadInfo(IR_BUTTON_2);
}
}
if (loadThreeTimerActive) {
Timer(3);
} else if (!loadThreeTimerActive && loadThreeTimerRecentlyActive) {
loadThreeOff = !loadThreeOff;
loadThreeTimerRecentlyActive = false;
if (selectedLoad == IR_BUTTON_3) {
RetrieveLoadInfo(IR_BUTTON_3);
}
}
if (loadFourTimerActive) {
Timer(4);
} else if (!loadFourTimerActive && loadFourTimerRecentlyActive) {
loadFourOff = !loadFourOff;
loadFourTimerRecentlyActive = false;
if (selectedLoad == IR_BUTTON_4) {
RetrieveLoadInfo(IR_BUTTON_4);
}
}
// < Run load timers if they're active
// Checking if loop() is being run for the first time
if (firstLoopRun) {
command = IR_BUTTON_0;
firstLoopRun = 0;
Serial.println("loop()");
}
else {
command = ReceiveCommand(); // Retrieve the command from the remote
}
//if (command != previousCommand) { // Prevents duplicate commands from being sent (such as holding down a button)
//StartReceiver();
// Welcome screen
if (command == IR_BUTTON_0) {
// Repeatedly display the instructions
while (true) {
command = WelcomeScreen();
if (command != IR_BUTTON_0) {
firstWelcomeScreenRun = true; // The welcome screen will have a fresh run
// Check if the user pressed buttons 6 or 8 (which are invalid)
if (command == IR_BUTTON_6 || command == IR_BUTTON_8 ) {
Serial.println("Inside if statement");
InvalidCommand();
command = IR_BUTTON_0;
}
// Checks if the user pressed an invalid button other than 6 or 8
for (int i = 10; i < 17; i++) {
if (command == buttonList[i]) {
InvalidCommand();
command = IR_BUTTON_0;
break;
}
}
// Check if a load's button was pressed
for (int i = 1; i < 6; i++) {
if (command == buttonList[i]) {
RetrieveLoadInfo(command);
whileLoopBreak = true;
break;
}
}
if (whileLoopBreak) {
break;
}
// If the user wants to set a timer
if (command == IR_BUTTON_7) {
SetTimer();
break;
}
// If the user wants to set a load's priority
else if (command == IR_BUTTON_9) {
SetPriority(command);
break;
}
}
else {
firstWelcomeScreenRun = false; // The welcome screen has been running
}
} // while (true)
} // if (command == IR_BUTTON_0)
// Turning a load ON/OFF
else if (command == IR_BUTTON_OK) {
// Load 1
if (selectedLoad == IR_BUTTON_1) {
if (loadOneTimerActive) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 1's timer");
ScrollText("is currently active.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadOneOff) {
ScrollText("Turning ON Load 1", 0);
} else {
ScrollText("Turning OFF Load 1", 0);
}
ScrollText("will deactivate its timer.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadOneOff) {
lcd.print("Turn Load 1 ON?");
} else {
lcd.print("Turn Load 1 OFF?");
}
StartReceiver();
while (true) {
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadOneOff = !loadOneOff;
numLoadsOut[0] = !numLoadsOut[0];
loadOneHours = 0;
loadOneMinutes = 0;
loadOneSeconds = 0;
loadOneTimerActive = false;
loadOneTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = ScrollText("OK to continue, 0 to cancel", 1);
if (command == IR_BUTTON_OK) {
loadOneOff = !loadOneOff;
numLoadsOut[0] = !numLoadsOut[0];
loadOneHours = 0;
loadOneMinutes = 0;
loadOneSeconds = 0;
loadOneTimerActive = false;
loadOneTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadOneOff = !loadOneOff;
numLoadsOut[0] = !numLoadsOut[0];
loadOneHours = 0;
loadOneMinutes = 0;
loadOneSeconds = 0;
loadOneTimerActive = false;
loadOneTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
} // while (true)
} //if (loadOneTimerActive)
else {
loadOneOff = !loadOneOff;
numLoadsOut[0] = !numLoadsOut[0];
}
}
// Load 2
else if (selectedLoad == IR_BUTTON_2) {
if (loadTwoTimerActive) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 2's timer");
ScrollText("is currently active.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadTwoOff) {
ScrollText("Turning ON Load 2", 0);
} else {
ScrollText("Turning OFF Load 2", 0);
}
ScrollText("will deactivate its timer.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadOneOff) {
lcd.print("Turn Load 2 ON?");
} else {
lcd.print("Turn Load 2 OFF?");
}
StartReceiver();
while (true) {
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadTwoOff = !loadTwoOff;
numLoadsOut[1] = !numLoadsOut[1];
loadTwoHours = 0;
loadTwoMinutes = 0;
loadTwoSeconds = 0;
loadTwoTimerActive = false;
loadTwoTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = ScrollText("OK to continue, 0 to cancel", 1);
if (command == IR_BUTTON_OK) {
loadTwoOff = !loadTwoOff;
numLoadsOut[1] = !numLoadsOut[1];
loadTwoHours = 0;
loadTwoMinutes = 0;
loadTwoSeconds = 0;
loadTwoTimerActive = false;
loadTwoTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadTwoOff = !loadTwoOff;
numLoadsOut[1] = !numLoadsOut[1];
loadTwoHours = 0;
loadTwoMinutes = 0;
loadTwoSeconds = 0;
loadTwoTimerActive = false;
loadTwoTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
} // while (true)
} //if (loadTwoTimerActive)
else {
loadTwoOff = !loadTwoOff;
numLoadsOut[1] = !numLoadsOut[1];
}
}
// Load 3
else if (selectedLoad == IR_BUTTON_3) {
if (loadThreeTimerActive) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 3's timer");
ScrollText("is currently active.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadThreeOff) {
ScrollText("Turning ON Load 3", 0);
} else {
ScrollText("Turning OFF Load 3", 0);
}
ScrollText("will deactivate its timer.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadThreeOff) {
lcd.print("Turn Load 3 ON?");
} else {
lcd.print("Turn Load 3 OFF?");
}
StartReceiver();
while (true) {
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadThreeOff = !loadThreeOff;
numLoadsOut[2] = !numLoadsOut[2];
loadThreeHours = 0;
loadThreeMinutes = 0;
loadThreeSeconds = 0;
loadThreeTimerActive = false;
loadThreeTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = ScrollText("OK to continue, 0 to cancel", 1);
if (command == IR_BUTTON_OK) {
loadThreeOff = !loadThreeOff;
numLoadsOut[2] = !numLoadsOut[2];
loadThreeHours = 0;
loadThreeMinutes = 0;
loadThreeSeconds = 0;
loadThreeTimerActive = false;
loadThreeTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadThreeOff = !loadThreeOff;
numLoadsOut[2] = !numLoadsOut[2];
loadThreeHours = 0;
loadThreeMinutes = 0;
loadThreeSeconds = 0;
loadThreeTimerActive = false;
loadThreeTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
} // while (true)
} //if (loadThreeTimerActive)
else {
loadThreeOff = !loadThreeOff;
numLoadsOut[2] = !numLoadsOut[2];
}
}
// Load 4
else if (selectedLoad == IR_BUTTON_4) {
if (loadFourTimerActive) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 4's timer");
ScrollText("is currently active.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadFourOff) {
ScrollText("Turning ON Load 4", 0);
} else {
ScrollText("Turning OFF Load 4", 0);
}
ScrollText("will deactivate its timer.", 1);
delay(warningMessageDelayTime);
lcd.clear();
lcd.setCursor(0, 0);
if (loadFourOff) {
lcd.print("Turn Load 4 ON?");
} else {
lcd.print("Turn Load 4 OFF?");
}
StartReceiver();
while (true) {
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadFourOff = !loadFourOff;
numLoadsOut[3] = !numLoadsOut[3];
loadFourHours = 0;
loadFourMinutes = 0;
loadFourSeconds = 0;
loadFourTimerActive = false;
loadFourTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = ScrollText("OK to continue, 0 to cancel", 1);
if (command == IR_BUTTON_OK) {
loadFourOff = !loadFourOff;
numLoadsOut[3] = !numLoadsOut[3];
loadFourHours = 0;
loadFourMinutes = 0;
loadFourSeconds = 0;
loadFourTimerActive = false;
loadFourTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
command = CustomDelay(1000);
if (command == IR_BUTTON_OK) {
loadFourOff = !loadFourOff;
numLoadsOut[3] = !numLoadsOut[3];
loadFourHours = 0;
loadFourMinutes = 0;
loadFourSeconds = 0;
loadFourTimerActive = false;
loadFourTimerRecentlyActive = false;
break;
} else if (command == IR_BUTTON_0) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Cancelled");
delay(warningMessageDelayTime);
break;
}
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
} // while (true)
} //if (loadFourTimerActive)
else {
loadFourOff = !loadFourOff;
numLoadsOut[3] = !numLoadsOut[3];
}
}
RetrieveLoadInfo(selectedLoad); // Get the selected load's information
command = 0; // Reset command so that the current command isn't unwantedly repeated
delay(buttonHoldDelay);
} // else if (command == IR_BUTTON_OK)
// Scrolling to previous load
else if (command == IR_BUTTON_LEFT) {
Serial.println("Inside IR_BUTTON_LEFT");
Scroll(selectedLoad, 1);
command = 0; // Reset command so that the current command isn't unwantedly repeated
delay(buttonHoldDelay);
}
// Scrolling to next load
else if (command == IR_BUTTON_RIGHT) {
Serial.println("Inside IR_BUTTON_RIGHT");
Scroll(selectedLoad, 2);
command = 0; // Reset command so that the current command isn't unwantedly repeated
delay(buttonHoldDelay);
}
// Setting load priorities
else if (command == IR_BUTTON_9) {
SetPriority(command);
}
// Setting load timers
else if (command == IR_BUTTON_7) {
SetTimer();
}
// Checking load's timer
else if (command == IR_BUTTON_8) {
CheckTimer(selectedLoad);
}
// Just retrieving load information
else if (command == IR_BUTTON_1 || command == IR_BUTTON_2 || command == IR_BUTTON_3 || command == IR_BUTTON_4 || command == IR_BUTTON_5) {
RetrieveLoadInfo(command);
previousCommand = command;
}
// Invalid commands
else if (command == IR_BUTTON_6 || command == IR_BUTTON_POUND || command == IR_BUTTON_ASTERISK) {
InvalidCommand();
RetrieveLoadInfo(selectedLoad);
} // else
//} //if (command != previousCommand)
//StopReceiver();
//delay(remoteDisableTime);
} // loop()
// Change in actual circuit
int ReadCurrent() {
sensorValue1 = map(analogRead(ctOnePin), 0, 1023, 0, 15);
sensorValue2 = map(analogRead(ctTwoPin), 0, 1023, 0, 15);
sensorValue3 = map(analogRead(ctThreePin), 0, 1023, 0, 15);
sensorValue4 = map(analogRead(ctFourPin), 0, 1023, 0, 15);
}
void StartReceiver() {
IrReceiver.begin(IR_PIN);
receiverActive = true;
} // StartReceiver()
void StopReceiver() {
IrReceiver.end();
receiverActive = false;
} // StopReceiver()
void InvalidCommand() {
int holdTime = 2000; // Milliseconds
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Command");
delay(holdTime);
StartReceiver();
return;
} // InvalidCommand()
int ReceiveCommand() {
unsigned long rawCommand = 0;
char key = keypad.getKey();
int finalCommand = -1;
if (receiverActive) {
if (IrReceiver.decode()) // If some data is received
{
IrReceiver.resume();
rawCommand = IrReceiver.decodedIRData.command;
// Prevents random IR signals from being accepted
for (int i = 0; i < 17; i++) {
if (rawCommand == buttonList[i]) {
finalCommand = rawCommand;
}
}
}
// Looking for a press on the keypad
else if (key) {
switch (key) {
case '0':
finalCommand = IR_BUTTON_0;
break;
case '1':
finalCommand = IR_BUTTON_1;
break;
case '2':
finalCommand = IR_BUTTON_2;
break;
case '3':
finalCommand = IR_BUTTON_3;
break;
case '4':
finalCommand = IR_BUTTON_4;
break;
case '5':
finalCommand = IR_BUTTON_5;
break;
case '6':
finalCommand = IR_BUTTON_6;
break;
case '7':
finalCommand = IR_BUTTON_7;
break;
case '8':
finalCommand = IR_BUTTON_8;
break;
case '9':
finalCommand = IR_BUTTON_9;
break;
} // switch (key)
} // else if (key)
} // if (receiverActive);
return finalCommand;
} // ReceiveCommand()
void CheckTimerStatuses() {
// Checks if any load timers are currently active and marks them accordingly
if ((loadOneHours > 0 || loadOneMinutes > 0 || loadOneSeconds > 0)) {
loadOneTimerActive = true;
loadOneTimerRecentlyActive = true;
} else {
//Serial.println("Inside else statement");
loadOneTimerActive = false;
}
if (loadTwoHours > 0 || loadTwoMinutes > 0 || loadTwoSeconds > 0) {
loadTwoTimerActive = true;
loadTwoTimerRecentlyActive = true;
} else {
loadTwoTimerActive = false;
}
if (loadThreeHours > 0 || loadThreeMinutes > 0 || loadThreeSeconds > 0) {
loadThreeTimerActive = true;
loadTwoTimerRecentlyActive = true;
} else {
loadThreeTimerActive = false;
}
if (loadFourHours > 0 || loadFourMinutes > 0 || loadFourSeconds > 0) {
loadFourTimerActive = true;
loadTwoTimerRecentlyActive = true;
} else {
loadFourTimerActive = false;
}
} // CheckTimerStatuses()
void CheckLoadStatuses() {
if (loadOneOff) {
digitalWrite(relayOnePin, LOW); // Change in actual circuit
} else {
digitalWrite(relayOnePin, HIGH); // Change in actual circuit
}
if (loadTwoOff) {
digitalWrite(relayTwoPin, LOW); // Change in actual circuit
} else {
digitalWrite(relayTwoPin, HIGH); // Change in actual circuit
ReadCurrent();
}
if (loadThreeOff) {
digitalWrite(relayThreePin, LOW); // Change in actual circuit
} else {
digitalWrite(relayThreePin, HIGH); // Change in actual circuit
ReadCurrent();
}
if (loadFourOff) {
digitalWrite(relayFourPin, LOW); // Change in actual circuit
} else {
digitalWrite(relayFourPin, HIGH); // Change in actual circuit
}
} // CheckLoadStatuses()
void CheckTimer(int load) {
unsigned long currentMillis = millis();
int stayTime = 5000; // How long the program will stay in CheckTimer(); in milliseconds
bool firstRun = true;
int previousHour;
int previousMinute;
int previousSecond;
if (load == IR_BUTTON_1 || load == IR_BUTTON_2 || load == IR_BUTTON_3 || load == IR_BUTTON_4) {
while (millis() < currentMillis + stayTime) {
StopReceiver();
switch (load) {
case IR_BUTTON_1:
// Checks if any load timers are currently active and marks them accordingly
if ((loadOneHours > 0 || loadOneMinutes > 0 || loadOneSeconds > 0)) {
loadOneTimerActive = true;
loadOneTimerRecentlyActive = true;
} else {
loadOneTimerActive = false;
}
if (loadOneTimerActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L1 T Remaining:");
lcd.setCursor(4, 1);
if (loadOneHours < 10) {
lcd.print("0");
lcd.print(loadOneHours);
} else {
lcd.print(loadOneHours);
}
lcd.print(":");
if (loadOneMinutes < 10) {
lcd.print("0");
lcd.print(loadOneMinutes);
} else {
lcd.print(loadOneMinutes);
}
lcd.print(":");
if (loadOneSeconds < 10) {
lcd.print("0");
lcd.print(loadOneSeconds);
} else {
lcd.print(loadOneSeconds);
}
firstRun = false;
} // if (firstRun)
previousHour = loadOneHours;
previousMinute = loadOneMinutes;
previousSecond = loadOneSeconds;
Timer(1);
if (loadOneHours != previousHour || loadOneMinutes != previousMinute || loadOneSeconds != previousSecond) {
lcd.setCursor(4, 1);
if (loadOneHours < 10) {
lcd.print("0");
lcd.print(loadOneHours);
} else {
lcd.print(loadOneHours);
}
lcd.print(":");
if (loadOneMinutes < 10) {
lcd.print("0");
lcd.print(loadOneMinutes);
} else {
lcd.print(loadOneMinutes);
}
lcd.print(":");
if (loadOneSeconds < 10) {
lcd.print("0");
lcd.print(loadOneSeconds);
} else {
lcd.print(loadOneSeconds);
}
} // if (loadOneHours != previousHour || loadOneMinutes != previousMinute || loadOneSeconds != previousSecond)
} // if (loadOneTimerActive)
else if (!loadOneTimerActive && loadOneTimerRecentlyActive) {
goto endCheckTimer;
}
else if (!loadOneTimerActive && !loadOneTimerRecentlyActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L1 Timer is");
lcd.setCursor(0, 1);
lcd.print("Inactive!");
firstRun = false;
}
}
break; // Break out of case IR_BUTTON_1
case IR_BUTTON_2:
// Checks if any load timers are currently active and marks them accordingly
if ((loadTwoHours > 0 || loadTwoMinutes > 0 || loadTwoSeconds > 0)) {
loadTwoTimerActive = true;
loadTwoTimerRecentlyActive = true;
} else {
loadTwoTimerActive = false;
}
if (loadTwoTimerActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L2 T Remaining:");
lcd.setCursor(4, 1);
if (loadTwoHours < 10) {
lcd.print("0");
lcd.print(loadTwoHours);
} else {
lcd.print(loadTwoHours);
}
lcd.print(":");
if (loadTwoMinutes < 10) {
lcd.print("0");
lcd.print(loadTwoMinutes);
} else {
lcd.print(loadTwoMinutes);
}
lcd.print(":");
if (loadTwoSeconds < 10) {
lcd.print("0");
lcd.print(loadTwoSeconds);
} else {
lcd.print(loadTwoSeconds);
}
firstRun = false;
} // if (firstRun)
previousHour = loadTwoHours;
previousMinute = loadTwoMinutes;
previousSecond = loadTwoSeconds;
Timer(2);
if (loadTwoHours != previousHour || loadTwoMinutes != previousMinute || loadTwoSeconds != previousSecond) {
lcd.setCursor(4, 1);
if (loadTwoHours < 10) {
lcd.print("0");
lcd.print(loadTwoHours);
} else {
lcd.print(loadTwoHours);
}
lcd.print(":");
if (loadTwoMinutes < 10) {
lcd.print("0");
lcd.print(loadTwoMinutes);
} else {
lcd.print(loadTwoMinutes);
}
lcd.print(":");
if (loadTwoSeconds < 10) {
lcd.print("0");
lcd.print(loadTwoSeconds);
} else {
lcd.print(loadTwoSeconds);
}
} // if (loadTwoHours != previousHour || loadTwoMinutes != previousMinute || loadTwoSeconds != previousSecond)
} // if (loadTwoTimerActive)
else if (!loadTwoTimerActive && loadTwoTimerRecentlyActive) {
goto endCheckTimer;
}
else if (!loadTwoTimerActive && !loadTwoTimerRecentlyActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L2 Timer is");
lcd.setCursor(0, 1);
lcd.print("Inactive!");
firstRun = false;
}
}
break; // Break out of case IR_BUTTON_2
case IR_BUTTON_3:
// Checks if any load timers are currently active and marks them accordingly
if ((loadThreeHours > 0 || loadThreeMinutes > 0 || loadThreeSeconds > 0)) {
loadThreeTimerActive = true;
loadThreeTimerRecentlyActive = true;
} else {
loadThreeTimerActive = false;
}
if (loadThreeTimerActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L3 T Remaining:");
lcd.setCursor(4, 1);
if (loadThreeHours < 10) {
lcd.print("0");
lcd.print(loadThreeHours);
} else {
lcd.print(loadThreeHours);
}
lcd.print(":");
if (loadThreeMinutes < 10) {
lcd.print("0");
lcd.print(loadThreeMinutes);
} else {
lcd.print(loadThreeMinutes);
}
lcd.print(":");
if (loadThreeSeconds < 10) {
lcd.print("0");
lcd.print(loadThreeSeconds);
} else {
lcd.print(loadThreeSeconds);
}
firstRun = false;
} // if (firstRun)
previousHour = loadThreeHours;
previousMinute = loadThreeMinutes;
previousSecond = loadThreeSeconds;
Timer(3);
if (loadThreeHours != previousHour || loadThreeMinutes != previousMinute || loadThreeSeconds != previousSecond) {
lcd.setCursor(4, 1);
if (loadThreeHours < 10) {
lcd.print("0");
lcd.print(loadThreeHours);
} else {
lcd.print(loadThreeHours);
}
lcd.print(":");
if (loadThreeMinutes < 10) {
lcd.print("0");
lcd.print(loadThreeMinutes);
} else {
lcd.print(loadThreeMinutes);
}
lcd.print(":");
if (loadThreeSeconds < 10) {
lcd.print("0");
lcd.print(loadThreeSeconds);
} else {
lcd.print(loadThreeSeconds);
}
} // if (loadThreeHours != previousHour || loadThreeMinutes != previousMinute || loadThreeSeconds != previousSecond)
} // if (loadThreeTimerActive)
else if (!loadThreeTimerActive && loadThreeTimerRecentlyActive) {
goto endCheckTimer;
}
else if (!loadThreeTimerActive && !loadThreeTimerRecentlyActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L3 Timer is");
lcd.setCursor(0, 1);
lcd.print("Inactive!");
firstRun = false;
}
}
break; // Break out of case IR_BUTTON_3
case IR_BUTTON_4:
// Checks if any load timers are currently active and marks them accordingly
if ((loadFourHours > 0 || loadFourMinutes > 0 || loadFourSeconds > 0)) {
loadFourTimerActive = true;
loadFourTimerRecentlyActive = true;
} else {
loadFourTimerActive = false;
}
if (loadFourTimerActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L4 T Remaining:");
lcd.setCursor(4, 1);
if (loadFourHours < 10) {
lcd.print("0");
lcd.print(loadFourHours);
} else {
lcd.print(loadFourHours);
}
lcd.print(":");
if (loadFourMinutes < 10) {
lcd.print("0");
lcd.print(loadFourMinutes);
} else {
lcd.print(loadFourMinutes);
}
lcd.print(":");
if (loadFourSeconds < 10) {
lcd.print("0");
lcd.print(loadFourSeconds);
} else {
lcd.print(loadFourSeconds);
}
firstRun = false;
} // if (firstRun)
previousHour = loadFourHours;
previousMinute = loadFourMinutes;
previousSecond = loadFourSeconds;
Timer(4);
if (loadFourHours != previousHour || loadFourMinutes != previousMinute || loadFourSeconds != previousSecond) {
lcd.setCursor(4, 1);
if (loadFourHours < 10) {
lcd.print("0");
lcd.print(loadFourHours);
} else {
lcd.print(loadFourHours);
}
lcd.print(":");
if (loadFourMinutes < 10) {
lcd.print("0");
lcd.print(loadFourMinutes);
} else {
lcd.print(loadFourMinutes);
}
lcd.print(":");
if (loadFourSeconds < 10) {
lcd.print("0");
lcd.print(loadFourSeconds);
} else {
lcd.print(loadFourSeconds);
}
} // if (loadFourHours != previousHour || loadFourMinutes != previousMinute || loadFourSeconds != previousSecond)
} // if (loadFourTimerActive)
else if (!loadFourTimerActive && loadFourTimerRecentlyActive) {
goto endCheckTimer;
}
else if (!loadFourTimerActive && !loadFourTimerRecentlyActive) {
if (firstRun) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("L4 Timer is");
lcd.setCursor(0, 1);
lcd.print("Inactive!");
firstRun = false;
}
}
break; // Break out of case IR_BUTTON_4
} // switch (load)
} // while (millis() < currentMillis + stayTime)
}
endCheckTimer:
StartReceiver();
RetrieveLoadInfo(load);
}
void Timer(int load) {
switch (load) {
// Load 1
case 1:
if (firstLoadOneTimerRun) {
currentMillisLoadOne = millis();
firstLoadOneTimerRun = false;
}
if (millis() >= currentMillisLoadOne + 1000) {
firstLoadOneTimerRun = true; // Reset firstTimerRun
if (loadOneSeconds > 0) {
loadOneSeconds--;
}
else if (loadOneMinutes > 0 && loadOneSeconds == 0) {
loadOneSeconds = 59;
loadOneMinutes--;
}
else if (loadOneHours > 0 && loadOneMinutes == 0 && loadOneSeconds == 0) {
loadOneMinutes = 59;
loadOneSeconds = 59;
loadOneHours--;
}
Serial.print("Load 1 Timer: ");
Serial.print(loadOneHours);
Serial.print(":");
Serial.print(loadOneMinutes);
Serial.print(":");
Serial.println(loadOneSeconds);
}
break; // Break out of case 1
// Load 2
case 2:
if (firstLoadTwoTimerRun) {
currentMillisLoadTwo = millis();
firstLoadTwoTimerRun = false;
}
if (millis() >= currentMillisLoadTwo + 1000) {
firstLoadTwoTimerRun = true; // Reset firstTimerRun
if (loadTwoSeconds > 0) {
loadTwoSeconds--;
}
else if (loadTwoMinutes > 0 && loadTwoSeconds == 0) {
loadTwoSeconds = 59;
loadTwoMinutes--;
}
else if (loadTwoHours > 0 && loadTwoMinutes == 0 && loadTwoSeconds == 0) {
loadTwoMinutes = 59;
loadTwoSeconds = 59;
loadTwoHours--;
}
Serial.print("Load 2 Timer: ");
Serial.print(loadTwoHours);
Serial.print(":");
Serial.print(loadTwoMinutes);
Serial.print(":");
Serial.println(loadTwoSeconds);
}
break; // Break out of case 2
// Load 3
case 3:
if (firstLoadThreeTimerRun) {
currentMillisLoadThree = millis();
firstLoadThreeTimerRun = false;
}
if (millis() >= currentMillisLoadThree + 1000) {
firstLoadThreeTimerRun = true; // Reset firstTimerRun
if (loadThreeSeconds > 0) {
loadThreeSeconds--;
}
else if (loadThreeMinutes > 0 && loadThreeSeconds == 0) {
loadThreeSeconds = 59;
loadThreeMinutes--;
}
else if (loadThreeHours > 0 && loadThreeMinutes == 0 && loadThreeSeconds == 0) {
loadThreeMinutes = 59;
loadThreeSeconds = 59;
loadThreeHours--;
}
Serial.print("Load 3 Timer: ");
Serial.print(loadThreeHours);
Serial.print(":");
Serial.print(loadThreeMinutes);
Serial.print(":");
Serial.println(loadThreeSeconds);
}
break; // Break out of case 3
// Load 4
case 4:
if (firstLoadFourTimerRun) {
currentMillisLoadFour = millis();
firstLoadFourTimerRun = false;
}
if (millis() >= currentMillisLoadFour + 1000) {
firstLoadFourTimerRun = true; // Reset firstTimerRun
if (loadFourSeconds > 0) {
loadFourSeconds--;
}
else if (loadFourMinutes > 0 && loadFourSeconds == 0) {
loadFourSeconds = 59;
loadFourMinutes--;
}
else if (loadFourHours > 0 && loadFourMinutes == 0 && loadFourSeconds == 0) {
loadFourMinutes = 59;
loadFourSeconds = 59;
loadFourHours--;
}
Serial.print("Load 4 Timer: ");
Serial.print(loadFourHours);
Serial.print(":");
Serial.print(loadFourMinutes);
Serial.print(":");
Serial.println(loadFourSeconds);
}
break; // Break out of case 4
}
} // Timer()
void SetTimer() {
int command;
int delayTime = 2000; // In milliseconds
int errorDisplayTime = 3000; // In milliseconds
int blinkTime = 1000; // In milliseconds
int i;
int column;
int temp;
int buttonPressed = 0;
int actualSelectedLoad;
bool breakOutOfWhileLoop = 0;
String loadOneHoursString = "00";
String loadOneMinutesString = "00";
String loadOneSecondsString = "00";
String loadTwoHoursString = "00";
String loadTwoMinutesString = "00";
String loadTwoSecondsString = "00";
String loadThreeHoursString = "00";
String loadThreeMinutesString = "00";
String loadThreeSecondsString = "00";
String loadFourHoursString = "00";
String loadFourMinutesString = "00";
String loadFourSecondsString = "00";
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Load");
lcd.setCursor(0, 1);
lcd.print("Timer");
delay(delayTime);
StartReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
// Selecting a load
while (true) {
command = ReceiveCommand();
for (i = 0; i < 4; i++) {
if (command == loadList[i]) {
selectedLoad = command;
buttonPressed = i + 1;
actualSelectedLoad = buttonPressed;
lcd.setCursor(6, 1);
lcd.print(buttonPressed);
breakOutOfWhileLoop = 1;
StopReceiver();
delay(delayTime);
StartReceiver();
break; // Break out of for loop
} // if (command == buttonList[i])
} // for (i = 0; i < 4; i++)
if (breakOutOfWhileLoop) {
breakOutOfWhileLoop = 0;
break; // Break out of while loop
}
// Checks if the user pressed 0
if (command == buttonList[0]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a load");
lcd.setCursor(0, 1);
lcd.print("1-4");
command = -1;
delay(errorDisplayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
StartReceiver();
}
// Checks if the user presses a button other than 0 or 1-4
for (i = 5; i < 17; i++) {
if (command == buttonList[i]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a load");
lcd.setCursor(0, 1);
lcd.print("1-4");
command = -1;
delay(errorDisplayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
StartReceiver();
}
}
} // while (true)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Timer");
lcd.setCursor(4, 1);
lcd.print("00:00:00");
StopReceiver();
delay(blinkTime);
StartReceiver();
column = 4;
buttonPressed = -1;
// Setting the load's timer
while (true) {
lcd.setCursor(column, 1);
lcd.print(" ");
command = CustomDelay(blinkTime);
// Checking if the user pressed a button while in CustomDelay()
if (command != -1) {
for (i = 0; i < 17; i++) {
if (command == buttonList[i]) {
buttonPressed = i;
}
}
}
// Matching the button press to buttonList
for (i = 0; i < 17; i++) {
if (command == buttonList[i]) {
buttonPressed = i;
}
}
// If the user has not pressed a button yet, keep displaying "0"
if (buttonPressed < 0) {
//CustomDelay(1000);
lcd.setCursor(column, 1);
lcd.print("0");
command = CustomDelay(blinkTime);
// Checking if the user pressed a button while in CustomDelay()
if (command != -1) {
for (i = 0; i < 17; i++) {
if (command == buttonList[i]) {
buttonPressed = i;
goto showButton; // Skip the blink at the beginning of the loop
}
}
}
}
// If the user has pressed a button, check if it's valid
else {
showButton:
//CustomDelay(1000);
// If the user tries to input minutes or seconds greater than 59, don't allow it
if ((column == 7 || column == 10) && (buttonPressed == 6 || buttonPressed == 7 || buttonPressed == 8 || buttonPressed == 9 || buttonPressed == 10 || buttonPressed == 13 || buttonPressed == 14 || buttonPressed == 15 || buttonPressed == 16)) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press a button");
lcd.setCursor(0, 1);
lcd.print("0-5");
delay(errorDisplayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Timer");
lcd.setCursor(4, 1);
switch (selectedLoad) {
case IR_BUTTON_1:
lcd.print(loadOneHoursString);
lcd.print(":");
lcd.print(loadOneMinutesString);
lcd.print(":");
lcd.print(loadOneSecondsString);
break;
case IR_BUTTON_2:
lcd.print(loadTwoHoursString);
lcd.print(":");
lcd.print(loadTwoMinutesString);
lcd.print(":");
lcd.print(loadTwoSecondsString);
break;
case IR_BUTTON_3:
lcd.print(loadThreeHoursString);
lcd.print(":");
lcd.print(loadThreeMinutesString);
lcd.print(":");
lcd.print(loadThreeSecondsString);
break;
case IR_BUTTON_4:
lcd.print(loadFourHoursString);
lcd.print(":");
lcd.print(loadFourMinutesString);
lcd.print(":");
lcd.print(loadFourSecondsString);
break;
} // switch (selectedLoad)
StartReceiver();
buttonPressed = -1;
continue; // Skip the rest of the loop and move to the next iteration
} // if ((column == 7 || column == 10) && (buttonPressed == 6 || buttonPressed == 7 || buttonPressed == 8 || buttonPressed == 9 || buttonPressed == 10 || buttonPressed == 13 || buttonPressed == 14 || buttonPressed == 15 || buttonPressed == 16))
// If the user tries to press a non-numeric button other than left or right, don't allow it
else if ((column != 7 && column != 10) && (buttonPressed == 10 || buttonPressed == 13 || buttonPressed == 14 || buttonPressed == 15 || buttonPressed == 16)) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press a numeric");
lcd.setCursor(0, 1);
lcd.print("button 0-9");
delay(errorDisplayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Timer");
lcd.setCursor(4, 1);
switch (selectedLoad) {
case IR_BUTTON_1:
lcd.print(loadOneHoursString);
lcd.print(":");
lcd.print(loadOneMinutesString);
lcd.print(":");
lcd.print(loadOneSecondsString);
break;
case IR_BUTTON_2:
lcd.print(loadTwoHoursString);
lcd.print(":");
lcd.print(loadTwoMinutesString);
lcd.print(":");
lcd.print(loadTwoSecondsString);
break;
case IR_BUTTON_3:
lcd.print(loadThreeHoursString);
lcd.print(":");
lcd.print(loadThreeMinutesString);
lcd.print(":");
lcd.print(loadThreeSecondsString);
break;
case IR_BUTTON_4:
lcd.print(loadFourHoursString);
lcd.print(":");
lcd.print(loadFourMinutesString);
lcd.print(":");
lcd.print(loadFourSecondsString);
break;
} // switch (selectedLoad)
StartReceiver();
buttonPressed = -1;
continue; // Skip the rest of the loop and move to the next iteration
} // else if (buttonPressed == 10 || buttonPressed == 13 || buttonPressed == 14 || buttonPressed == 15 || buttonPressed == 16)
// Going to the left
else if (buttonPressed == 11) {
// Hours section
if (column == 4 || column == 5) {
buttonPressed = -1;
lcd.setCursor(column, 1);
// Left end of the timer (don't go before this)
if (column == 4) {
continue;
}
else if (column == 5) {
//buttonPressed = -1;
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneHoursString[1]);
break;
case 2:
lcd.print(loadTwoHoursString[1]);
break;
case 3:
lcd.print(loadThreeHoursString[1]);
break;
case 4:
lcd.print(loadFourHoursString[1]);
break;
}
column--;
}
continue;
}
// Minutes section
else if (column == 7 || column == 8) {
buttonPressed = -1;
lcd.setCursor(column, 1);
if (column == 7) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneMinutesString[0]);
break;
case 2:
lcd.print(loadTwoMinutesString[0]);
break;
case 3:
lcd.print(loadThreeMinutesString[0]);
break;
case 4:
lcd.print(loadFourMinutesString[0]);
break;
}
column -= 2;
}
else if (column == 8) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneMinutesString[1]);
break;
case 2:
lcd.print(loadTwoMinutesString[1]);
break;
case 3:
lcd.print(loadThreeMinutesString[1]);
break;
case 4:
lcd.print(loadFourMinutesString[1]);
break;
}
column--;
}
continue;
}
// Seconds section
else if (column == 10 || column == 11) {
buttonPressed = -1;
lcd.setCursor(column, 1);
if (column == 10) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneSecondsString[0]);
break;
case 2:
lcd.print(loadTwoSecondsString[0]);
break;
case 3:
lcd.print(loadThreeSecondsString[0]);
break;
case 4:
lcd.print(loadFourSecondsString[0]);
break;
}
column -= 2;
}
else if (column == 11) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneSecondsString[1]);
break;
case 2:
lcd.print(loadTwoSecondsString[1]);
break;
case 3:
lcd.print(loadThreeSecondsString[1]);
break;
case 4:
lcd.print(loadFourSecondsString[1]);
break;
}
column--;
}
continue;
}
}
// Going to the right
else if (buttonPressed == 12) {
// Hours section
if (column == 4 || column == 5) {
buttonPressed = -1;
lcd.setCursor(column, 1);
// Left end of the timer (don't go before this)
if (column == 4) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneHoursString[0]);
break;
case 2:
lcd.print(loadTwoHoursString[0]);
break;
case 3:
lcd.print(loadThreeHoursString[0]);
break;
case 4:
lcd.print(loadFourHoursString[0]);
break;
}
column++;
}
else if (column == 5) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneHoursString[1]);
break;
case 2:
lcd.print(loadTwoHoursString[1]);
break;
case 3:
lcd.print(loadThreeHoursString[1]);
break;
case 4:
lcd.print(loadFourHoursString[1]);
break;
}
column += 2;
}
continue;
}
// Minutes section
else if (column == 7 || column == 8) {
buttonPressed = -1;
lcd.setCursor(column, 1);
if (column == 7) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneMinutesString[0]);
break;
case 2:
lcd.print(loadTwoMinutesString[0]);
break;
case 3:
lcd.print(loadThreeMinutesString[0]);
break;
case 4:
lcd.print(loadFourMinutesString[0]);
break;
}
column++;
}
else if (column == 8) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneMinutesString[1]);
break;
case 2:
lcd.print(loadTwoMinutesString[1]);
break;
case 3:
lcd.print(loadThreeMinutesString[1]);
break;
case 4:
lcd.print(loadFourMinutesString[1]);
break;
}
column += 2;
}
continue;
}
// Seconds section
else if (column == 10 || column == 11) {
buttonPressed = -1;
lcd.setCursor(column, 1);
if (column == 10) {
switch (actualSelectedLoad) {
case 1:
lcd.print(loadOneSecondsString[0]);
break;
case 2:
lcd.print(loadTwoSecondsString[0]);
break;
case 3:
lcd.print(loadThreeSecondsString[0]);
break;
case 4:
lcd.print(loadFourSecondsString[0]);
break;
}
column++;
}
// Right end of the timer (don't go after this)
else if (column == 11) {
continue;
}
continue;
}
} // else if (buttonPressed == 12)
lcd.setCursor(column, 1);
lcd.print(buttonPressed);
switch (selectedLoad) {
// Load 1
case IR_BUTTON_1:
switch (column) {
case 4:
loadOneHoursString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 5:
loadOneHoursString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadOneHours = loadOneHoursString.toInt();
break;
case 7:
loadOneMinutesString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 8:
loadOneMinutesString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadOneMinutes = loadOneMinutesString.toInt();
break;
case 10:
loadOneSecondsString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 11:
loadOneSecondsString.setCharAt(1, ('0' + buttonPressed));
column++;
loadOneSeconds = loadOneSecondsString.toInt();
break;
} // switch (column)
break; // IR_BUTTON_1
// Load 2
case IR_BUTTON_2:
switch (column) {
case 4:
loadTwoHoursString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 5:
loadTwoHoursString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadTwoHours = loadTwoHoursString.toInt();
break;
case 7:
loadTwoMinutesString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 8:
loadTwoMinutesString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadTwoMinutes = loadTwoMinutesString.toInt();
break;
case 10:
loadTwoSecondsString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 11:
loadTwoSecondsString.setCharAt(1, ('0' + buttonPressed));
column++;
loadTwoSeconds = loadTwoSecondsString.toInt();
break;
} // switch (column)
break; // IR_BUTTON_2
// Load 3
case IR_BUTTON_3:
switch (column) {
case 4:
loadThreeHoursString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 5:
loadThreeHoursString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadThreeHours = loadThreeHoursString.toInt();
break;
case 7:
loadThreeMinutesString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 8:
loadThreeMinutesString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadThreeMinutes = loadThreeMinutesString.toInt();
break;
case 10:
loadThreeSecondsString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 11:
loadThreeSecondsString.setCharAt(1, ('0' + buttonPressed));
column++;
loadThreeSeconds = loadThreeSecondsString.toInt();
break;
} // switch (column)
break; // IR_BUTTON_3
// Load 4
case IR_BUTTON_4:
switch (column) {
case 4:
loadFourHoursString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 5:
loadFourHoursString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadFourHours = loadFourHoursString.toInt();
break;
case 7:
loadFourMinutesString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 8:
loadFourMinutesString.setCharAt(1, ('0' + buttonPressed));
column += 2;
loadFourMinutes = loadFourMinutesString.toInt();
break;
case 10:
loadFourSecondsString.setCharAt(0, ('0' + buttonPressed));
column++;
break;
case 11:
loadFourSecondsString.setCharAt(1, ('0' + buttonPressed));
column++;
loadFourSeconds = loadFourSecondsString.toInt();
break;
} // switch (column)
break; // IR_BUTTON_4
} // switch (selectedLoad)
if (column == 12) {
delay(delayTime);
break; // Break out of while loop
}
StopReceiver();
delay(450);
StartReceiver();
buttonPressed = -1;
} // else
} // while (true)
StopReceiver();
// Mark the load's timer as active
switch (actualSelectedLoad) {
case 1:
loadOneTimerActive = true;
break;
case 2:
loadTwoTimerActive = true;
break;
case 3:
loadThreeTimerActive = true;
break;
case 4:
loadFourTimerActive = true;
break;
}
Serial.print(loadTwoHours);
Serial.print(":");
Serial.print(loadTwoMinutes);
Serial.print(":");
Serial.println(loadTwoSeconds);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer set");
lcd.setCursor(0, 1);
lcd.print("successfully!");
delay(delayTime);
StartReceiver();
RetrieveLoadInfo(selectedLoad);
} // SetTimer()
void SetPriority(int command) {
//const int buttonList[4] = { 22, 25, 13, 12 }; // Buttons 1-4
bool breakOutOfWhileLoop = 0;
int i;
int temp;
int delayTime = 2000; // In milliseconds
int buttonPressed = 0; // Holds which physical button was pressed
int actualSelectedLoad;
String setLoadPriority;
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Load");
lcd.setCursor(0, 1);
lcd.print("Priority");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
StartReceiver();
// Selecting a load
while (true) {
command = ReceiveCommand();
// Checking if the user pressed a button 1-4
for (i = 1; i < 5; i++) {
if (command == buttonList[i]) {
selectedLoad = command;
buttonPressed = i;
actualSelectedLoad = buttonPressed;
lcd.setCursor(6, 1);
lcd.print(buttonPressed);
breakOutOfWhileLoop = 1;
StopReceiver();
delay(delayTime);
StartReceiver();
command = 0;
break; // Break out of for loop
} // if (command == buttonList[i])
} // for (i = 0; i < 4; i++)
if (breakOutOfWhileLoop) {
breakOutOfWhileLoop = 0;
break; // Break out of while loop
}
// Checks if the user pressed 0
if (command == buttonList[0]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a load");
lcd.setCursor(0, 1);
lcd.print("1-4");
command = -1;
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
StartReceiver();
}
// Checks if the user presses a button other than 0 or 1-4
for (i = 5; i < 17; i++) {
if (command == buttonList[i]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a load");
lcd.setCursor(0, 1);
lcd.print("1-4");
command = -1;
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Load");
lcd.setCursor(0, 1);
lcd.print("Load: ");
StartReceiver();
}
}
} // while (true)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Priority");
lcd.setCursor(0, 1);
lcd.print("P: ");
buttonPressed = 0;
// Setting the load's priority
while (true) {
command = ReceiveCommand();
// Checks if the user pressed 0
if (command == buttonList[0]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a");
lcd.setCursor(0, 1);
lcd.print("priority 1-4");
command = -1;
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Priority");
lcd.setCursor(0, 1);
lcd.print("P: ");
StartReceiver();
continue;
}
// Checks if the user pressed a button other than 0 or 1-4
for (i = 5; i < 17; i++) {
if (command == buttonList[i]) {
StopReceiver();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select a");
lcd.setCursor(0, 1);
lcd.print("priority 1-4");
command = -1;
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load ");
lcd.print(actualSelectedLoad);
lcd.print(" Priority");
lcd.setCursor(0, 1);
lcd.print("P: ");
StartReceiver();
continue;
}
}
for (i = 1; i < 5; i++) {
if (command == buttonList[i]) {
buttonPressed = i;
StopReceiver();
// Load 1 is selected
if (selectedLoad == IR_BUTTON_1) {
if (loadOnePriority == buttonPressed) {
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
ScrollText("Load 1's priority", 0);
lcd.setCursor(0, 1);
lcd.print("is already ");
lcd.print(buttonPressed);
lcd.print("!");
delay(delayTime);
}
else if (loadTwoPriority == buttonPressed) {
temp = loadTwoPriority;
loadTwoPriority = loadOnePriority;
loadOnePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 2's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadTwoPriority);
delay(delayTime);
}
else if (loadThreePriority == buttonPressed) {
temp = loadThreePriority;
loadThreePriority = loadOnePriority;
loadOnePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 3's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadThreePriority);
delay(delayTime);
}
else if (loadFourPriority == buttonPressed) {
temp = loadFourPriority;
loadFourPriority = loadOnePriority;
loadOnePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 4's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadFourPriority);
delay(delayTime);
}
}
// Load 2 is selected
else if (selectedLoad == IR_BUTTON_2) {
if (loadTwoPriority == buttonPressed) {
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
ScrollText("Load 2's priority", 0);
lcd.setCursor(0, 1);
lcd.print("is already ");
lcd.print(buttonPressed);
lcd.print("!");
delay(delayTime);
}
else if (loadOnePriority == buttonPressed) {
temp = loadOnePriority;
loadOnePriority = loadTwoPriority;
loadTwoPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 1's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadOnePriority);
delay(delayTime);
}
else if (loadThreePriority == buttonPressed) {
temp = loadThreePriority;
loadThreePriority = loadTwoPriority;
loadTwoPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 3's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadThreePriority);
delay(delayTime);
}
else if (loadFourPriority == buttonPressed) {
temp = loadFourPriority;
loadFourPriority = loadTwoPriority;
loadTwoPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 4's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadFourPriority);
delay(delayTime);
}
}
// Load 3 is selected
else if (selectedLoad == IR_BUTTON_3) {
if (loadThreePriority == buttonPressed) {
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
ScrollText("Load 3's priority", 0);
lcd.setCursor(0, 1);
lcd.print("is already ");
lcd.print(buttonPressed);
lcd.print("!");
delay(delayTime);
}
else if (loadOnePriority == buttonPressed) {
temp = loadOnePriority;
loadOnePriority = loadThreePriority;
loadThreePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 1's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadOnePriority);
delay(delayTime);
}
else if (loadTwoPriority == buttonPressed) {
temp = loadTwoPriority;
loadTwoPriority = loadThreePriority;
loadThreePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 2's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadTwoPriority);
delay(delayTime);
}
else if (loadFourPriority == buttonPressed) {
temp = loadFourPriority;
loadFourPriority = loadThreePriority;
loadThreePriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 4's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadFourPriority);
delay(delayTime);
}
}
// Load 4 is selected
else if (selectedLoad == IR_BUTTON_4) {
if (loadFourPriority == buttonPressed) {
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
ScrollText("Load 4's priority", 0);
lcd.setCursor(0, 1);
lcd.print("is already ");
lcd.print(buttonPressed);
lcd.print("!");
delay(delayTime);
}
else if (loadOnePriority == buttonPressed) {
temp = loadOnePriority;
loadOnePriority = loadFourPriority;
loadFourPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 1's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadOnePriority);
delay(delayTime);
}
else if (loadTwoPriority == buttonPressed) {
temp = loadTwoPriority;
loadTwoPriority = loadFourPriority;
loadFourPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 2's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadTwoPriority);
delay(delayTime);
}
else if (loadThreePriority == buttonPressed) {
temp = loadThreePriority;
loadThreePriority = loadFourPriority;
loadFourPriority = temp;
lcd.setCursor(3, 1);
lcd.print(buttonPressed);
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Command");
lcd.setCursor(0, 1);
lcd.print("Successful!");
delay(delayTime);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Load 3's P set");
lcd.setCursor(0, 1);
lcd.print("to ");
lcd.print(loadThreePriority);
delay(delayTime);
}
}
breakOutOfWhileLoop = 1;
break;
}
}
if (breakOutOfWhileLoop) {
breakOutOfWhileLoop = 0;
break;
}
} // while (true)
command = selectedLoad;
RetrieveLoadInfo(command);
StartReceiver();
} // SetPriority()
int CustomDelay(int interval) {
unsigned long currentMillis = millis();
int command = -1;
int i;
bool shouldWeBreak = 0;
while (millis() < currentMillis + interval) {
// Running load timer if active >
//CheckTimerStatuses();
// < Running load timer if active
command = ReceiveCommand(); // Retrieve a command from the remote
for (i = 0; i < 17; i++) {
if (command == buttonList[i]) {
command = buttonList[i];
return command;
}
}
}
return command;
} // CustomDelay()
int ScrollText(String str, int rowToPrint) {
int command = -1;
//const int buttonList[17] = { 64, 66, 74, 70, 21, 68, 67, 82, 22, 25, 13, 12, 24, 94, 8, 28, 90 };
int i, j = 0;
bool shouldWeBreak = 0;
for (i = 0; i < str.length(); i++) {
command = ReceiveCommand(); // Checking for a button press from the remote
if (command != -1) {
return command;
}
// Gradually printing the text from left to right
if (i < 15) {
lcd.setCursor(i, rowToPrint);
lcd.print(str.charAt(i));
command = CustomDelay(150);
if (command != -1) {
return command;
}
}
// Scrolling the text to the left
else {
for (i = 16; i < str.length(); i++) {
j++;
lcd.setCursor(0, rowToPrint);
lcd.print(str.substring(j, j + 16));
command = CustomDelay(250);
if (command != -1) {
return command;
}
}
}
} // for loop
return command;
} // ScrollText()
int WelcomeScreen() {
int interval = 2000;
int command = -1;
bool firstRun = 1;
//bool shouldWeBreak = 0;
// Welcome screen strings
String welcomeScreenInfo = "0: Welcome screen";
String loadScreensInfo = "1-4: Loads 1-4";
String overallInfo = "5: Overall system info";
String scrollingInfo = "< or >: Scroll left or right between loads";
String pInfo = "P = Load's Priority";
String setPriorityInfo = "9: Set Load Priority";
String tInfo = "T = Load Timer Active";
String setTimerInfo = "7: Set Load Timer";
// If this is the first run of WelcomeScreen()
if (firstRun == true) {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Welcome!");
}
// Instructions for returning to the welcome screen >
command = ScrollText(welcomeScreenInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Instructions for returning to the welcome screen
// Instructions for getting to specific load screens >
command = ScrollText(loadScreensInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Instructions for getting to specific load screens
// Instructions for scrolling between load screens >
command = ScrollText(scrollingInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Instructions for scrolling between load screens
// Intructions for getting to overall system information screen >
command = ScrollText(overallInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// <Intructions for getting to overall system information screen
// Instructions for setting load priorities >
command = ScrollText(setPriorityInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Instructions for setting load priorities
// Explanation of "P" >
command = ScrollText(pInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Explanation of "P"
// Instructions for setting load timers >
command = ScrollText(setTimerInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Instructions for setting load timers
// Explanation of "T" >
command = ScrollText(tInfo, 1);
if (command != -1) {
return command;
}
command = CustomDelay(interval);
if (command != -1) {
return command;
}
lcd.setCursor(0, 1);
lcd.print(" ");
// < Explanation of "T"
return IR_BUTTON_0;
} // WelcomeScreen()
void Scroll(int currentLoad, int direction) {
int newLoad;
const int commandList[7] = { -1, IR_BUTTON_1, IR_BUTTON_2, IR_BUTTON_3, IR_BUTTON_4, IR_BUTTON_5, -2 }; // -1 and -2 are placeholders to indicate the ends of the array
// Don't allow scrolling past screens 1 and 5
if (currentLoad == 1 || currentLoad == 5) {
return;
}
else {
for (int i = 0; i < 7; i++) {
if (commandList[i] == currentLoad) {
if (direction == 1 && commandList[i - 1] != -1) { // Scrolling left (to the previous load)
newLoad = commandList[i - 1];
}
else if (direction == 2 && commandList[i + 1] != -2) { // Scrolling right (to the next load)
newLoad = commandList[i + 1];
}
RetrieveLoadInfo(newLoad);
}
}
}
} // Scroll()
void RetrieveLoadInfo(int load) {
startOfRetrieveLoadInfo:
int holdTime = 2000; // In milliseconds
int currentReadingRefreshTime = 1000; // Time between current reading refreshes
int command = -1;
int previousCurrentReading = -1;
bool brokeOutOfWhileLoop = false;
int testCount = 0;
loadsOut = 0;
switch (load) {
// Load 1 is selected
case IR_BUTTON_1:
//while (stayInWhileLoop) {
getLoadOneInfo:
activeLoadScreen = 1; // Load 1's screen is currently active
selectedLoad = IR_BUTTON_1;
lcd.clear();
lcd.setCursor(0, 0);
if (loadOneTimerActive) {
lcd.print("Load 1: T P = ");
lcd.print(loadOnePriority);
}
else {
lcd.print("Load 1: P = ");
lcd.print(loadOnePriority);
}
// If Load 1 is ON
if (!loadOneOff) {
lcd.setCursor(0, 1);
lcd.print(" ");
/*
Serial.println("Inside if (!loadOneOff)");
Serial.print("loadOneTimerActive = ");
Serial.println(loadOneTimerActive);
Serial.print("loadOneTimerRecentlyActive = ");
Serial.println(loadOneTimerRecentlyActive);
*/
digitalWrite(relayOnePin, HIGH); // Turn Load 1 ON
previousCurrentReading = sensorValue1; // Used to determine when to update the current reading
// Refreshing the screen to update the load's current draw
while (true) {
/*
Serial.print("Inside while loop ");
Serial.println(testCount);
testCount++;
*/
// Running load timer if it's active >
CheckTimerStatuses();
// Runs the load's timer if it's active
if (loadOneTimerActive) {
Timer(1);
} else if (!loadOneTimerActive && loadOneTimerRecentlyActive) {
loadOneOff = !loadOneOff;
//Serial.println("Inside else if statement");
loadOneTimerRecentlyActive = false;
break; // Break out of the while loop
}
// < Running load timer if it's active
ReadCurrent();
// Refreshing the current reading if it changes
if (sensorValue1 != previousCurrentReading) {
lcd.setCursor(0, 1);
lcd.print(" "); // Erase the second row
lcd.setCursor(0, 1);
lcd.print(sensorValue1); // Print the new current reading
lcd.print("A");
previousCurrentReading = sensorValue1;
} // if (sensorValue1 == previousCurrentReading);
else {
lcd.setCursor(0, 1);
lcd.print(sensorValue1);
lcd.print("A");
} // else
command = CustomDelay(currentReadingRefreshTime);
// Turning Load 1 OFF
if (command == IR_BUTTON_OK) {
loadOneOff = true;
//goto turnLoadOneOff;
goto startOfRetrieveLoadInfo;
}
// Displaying the Welcome Screen
else if (command == IR_BUTTON_0) {
callWelcomeScreen:
command = WelcomeScreen();
// Inside WelcomeScreen()
switch (command) {
// Starting the Welcome Screen over
case IR_BUTTON_0:
goto callWelcomeScreen;
break;
// Fetching Load 1's info
case IR_BUTTON_1:
goto getLoadOneInfo;
break;
// Fetching Load 2's info
case IR_BUTTON_2:
goto getLoadTwoInfo;
break;
// Fetching Load 3's info
case IR_BUTTON_3:
goto getLoadThreeInfo;
break;
// Fetching Load 4's info
case IR_BUTTON_4:
goto getLoadFourInfo;
break;
// Fetching Load 5's info
case IR_BUTTON_5:
goto getSystemInfo;
break;
// Setting a load's timer
case IR_BUTTON_7:
SetTimer();
switch (selectedLoad) {
case IR_BUTTON_1:
goto getLoadOneInfo;
break;
case IR_BUTTON_2:
goto getLoadTwoInfo;
break;
case IR_BUTTON_3:
goto getLoadThreeInfo;
break;
case IR_BUTTON_4:
goto getLoadFourInfo;
break;
} // switch (selectedLoad)
break; // Break out of case IR_BUTTON_7
// Setting a load's priority
case IR_BUTTON_9:
SetPriority(command);
switch (selectedLoad) {
case IR_BUTTON_1:
goto getLoadOneInfo;
break;
case IR_BUTTON_2:
goto getLoadTwoInfo;
break;
case IR_BUTTON_3:
goto getLoadThreeInfo;
break;
case IR_BUTTON_4:
goto getLoadFourInfo;
break;
} // switch (selectedLoad)
// Invalid commands
default:
InvalidCommand();
goto callWelcomeScreen; // Start the Welcome Screen over
break;
} // switch (command)
} // else if (command == IR_BUTTON_0)
// Going to Load 2's info
else if (command == IR_BUTTON_2) {
goto getLoadTwoInfo;
}
// Going to Load 3's info
else if (command == IR_BUTTON_3) {
goto getLoadThreeInfo;
}
// Going to Load 4's info
else if (command == IR_BUTTON_4) {
goto getLoadFourInfo;
}
// Going to overall system info
else if (command == IR_BUTTON_5) {
goto getSystemInfo;
}
// Setting a load's timer
else if (command == IR_BUTTON_7) {
SetTimer();
switch (selectedLoad) {
case IR_BUTTON_1:
goto getLoadOneInfo;
break;
case IR_BUTTON_2:
goto getLoadTwoInfo;
break;
case IR_BUTTON_3:
goto getLoadThreeInfo;
break;
case IR_BUTTON_4:
goto getLoadFourInfo;
break;
} // switch (selectedLoad)
command = -1;
} // else if (command == IR_BUTTON_7)
// Checking Load 1's timer
else if (command == IR_BUTTON_8) {
CheckTimer(selectedLoad);
}
// Setting a load's priority
else if (command == IR_BUTTON_9) {
SetPriority(command);
switch (selectedLoad) {
case IR_BUTTON_1:
goto getLoadOneInfo;
break;
case IR_BUTTON_2:
goto getLoadTwoInfo;
break;
case IR_BUTTON_3:
goto getLoadThreeInfo;
break;
case IR_BUTTON_4:
goto getLoadFourInfo;
break;
} // switch (selectedLoad)
} // else if (command == IR_BUTTON_9)
} // while (true);
if (brokeOutOfWhileLoop) {
RetrieveLoadInfo(IR_BUTTON_1);
}
} // if (!loadOneOff)
// If Load 1 is OFF
//else {
turnLoadOneOff:
digitalWrite(relayOnePin, LOW); // Turn Load 1 OFF
sensorValue1 = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Load 1 is off");
//}
break; // Break out of case IR_BUTTON_1
// Load 2 is selected
case IR_BUTTON_2:
getLoadTwoInfo:
selectedLoad = IR_BUTTON_2;
lcd.clear();
lcd.setCursor(0, 0);
if (loadTwoTimerActive) {
lcd.print("Load 2: T P = ");
lcd.print(loadTwoPriority);
}
else {
lcd.print("Load 2: P = ");
lcd.print(loadTwoPriority);
}
// If Load 2 is ON
if (!loadTwoOff) {
digitalWrite(relayTwoPin, LOW); // Turn Load 2 ON
lcd.setCursor(0, 1);
lcd.print(sensorValue2);
lcd.print("A");
}
// If Load 2 is OFF
else {
digitalWrite(relayTwoPin, HIGH); // Turn Load 2 OFF
lcd.setCursor(0, 1);
lcd.print("Load 2 is off");
}
break; // Break out of case IR_BUTTON_2
// Load 3 is selected
case IR_BUTTON_3:
getLoadThreeInfo:
selectedLoad = IR_BUTTON_3;
lcd.clear();
lcd.setCursor(0, 0);
if (loadThreeTimerActive) {
lcd.print("Load 3: T P = ");
lcd.print(loadThreePriority);
}
else {
lcd.print("Load 3: P = ");
lcd.print(loadThreePriority);
}
// If Load 3 is ON
if (!loadThreeOff) {
digitalWrite(relayThreePin, LOW); // Turn Load 3 ON
lcd.setCursor(0, 1);
lcd.print(sensorValue3);
lcd.print("A");
}
// If Load 3 is OFF
else {
digitalWrite(relayThreePin, HIGH); // Turn Load 3 OFF
lcd.setCursor(0, 1);
lcd.print("Load 3 is off");
}
break;
// Load 4 is selected
case IR_BUTTON_4:
getLoadFourInfo:
selectedLoad = IR_BUTTON_4;
lcd.clear();
lcd.setCursor(0, 0);
if (loadFourTimerActive) {
lcd.print("Load 4: T P = ");
lcd.print(loadFourPriority);
}
else {
lcd.print("Load 4: P = ");
lcd.print(loadFourPriority);
}
// If Load 4 is ON
if (!loadFourOff) {
digitalWrite(relayFourPin, LOW); // Turn Load 4 ON
lcd.setCursor(0, 1);
lcd.print(sensorValue4);
lcd.print("A");
}
// If Load 4 is OFF
else {
digitalWrite(relayFourPin, HIGH); // Turn Load 4 OFF
lcd.setCursor(0, 1);
lcd.print("Load 4 is off");
}
break;
// Overall load information
case IR_BUTTON_5:
getSystemInfo:
selectedLoad = IR_BUTTON_5;
// Counting how many loads are on
for (int i = 0; i < 4; i++) {
if (numLoadsOut[i] == 1) {
loadsOut++;
}
}
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Total:");
lcd.setCursor(0, 1);
lcd.print(total);
lcd.print("A");
lcd.print(" LO = ");
lcd.print(loadsOut);
break;
} // switch (load)
} //RetrieveLoadInfo()