I'm not sure how everything is wired up. I did the wiring a few years back. It should be very simple, I'm using the grove interface. I just have an LCD screen and two relays. I don't remember exactly how the relays were wired in, but I think they just break the circuit in a regular outlet. They make a clicking noise every time the relay state changes. I believe this is the one I used https://www.seeedstudio.com/Grove-Relay.html If the wiring is the issue then fixing this may be beyond me for now...I can't invest too much time into this project.
I did a couple sessions updating the code. I hope it is more readable and maybe conventional now. I did away with the addition as suggested and switched to more of a comparison range (see timerSequenceFunctionality() )
Going to try to run this for a day or so and see if it freezes up again.
int relayOne = 3; // base shield pin number for Relay 1 // Connect the LED to Arduino digital pin 4, Grove socket D4
int relayTwo = 4; // base shield pin number for Relay 2
const long faeOnEnd = 60000;
const long humOnEnd = 150000;
const long allOffEnd = 1200000;
const long resetEnd = 1203000; // reset screen
unsigned long previousDisplayMillis = 0;
int currentDisplayInterval = 1000;
int faeRelayOneState = LOW;
int currentState = 2; //immediately advances to 0
char *lcdStrings[] = { " fae ON ", " hum ON ", " all OFF" };
// rgb screen
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
int colorR = 255;
int colorG = 255;
int colorB = 0;
unsigned long currentMillis = 0;
void setup() {
pinMode(relayOne, OUTPUT); // setup for Relay 1 "initialize ledPin as an output."
pinMode(relayTwo, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// set screen background color
lcd.setRGB(colorR, colorG, colorB);
//Serial.begin(9600);
}
void loop() {
timerSequencerFunctionality();
displayTimer();
}
void timerSequencerFunctionality() {
incrementTime();
if ((currentMillis <= faeOnEnd) && (currentMillis >= 0)) {
changeState(0);
} else if ((currentMillis <= humOnEnd) && (currentMillis > faeOnEnd)) {
changeState(1);
} else if ((currentMillis <= allOffEnd) && (currentMillis > humOnEnd)) {
changeState(2);
} else if ((currentMillis <= resetEnd) && (currentMillis > allOffEnd)) {
changeState(3);
}
}
void incrementTime() {
currentMillis = millis() % resetEnd;
}
void changeState(int c) {
if (currentState != c) {
currentState = c;
currentStateFunction();
};
}
void currentStateFunction() {
switch (currentState) {
case 0: // fae
lcdPrintAllEnd(allOffEnd);
relayChange(relayTwo, LOW, relayOne, HIGH);
lcdChange(0, faeOnEnd);
break;
case 1: //run hum
relayChange(relayTwo, HIGH, relayOne, LOW); // turn on hum, turn off fae
lcdChange(1, humOnEnd);
break;
case 2: //all off
relayChange(relayTwo, LOW, relayOne, LOW); // turn off hum
lcdChange(2, allOffEnd);
break;
case 3:
lcd.clear();
}
}
void relayChange(int relay2, int relay2value, int relay1, int relay1value) {
digitalWrite(relay2, relay2value);
digitalWrite(relay1, relay2value);
}
void lcdChange(int z, const long c) {
lcd.setCursor(8, 0); // set text print position
lcd.print(lcdStrings[z]); // display text
lcd.setCursor(0, 1); // display the current states ending time
lcd.print(c / 1000);
}
void lcdPrintAllEnd(const long c) {
lcd.setCursor(8, 1);
lcd.print(c / 1000);
}
void displayTimer() {
if ((currentMillis - previousDisplayMillis) >= currentDisplayInterval) {
previousDisplayMillis = currentMillis;
if (currentState != 3) {
lcd.setCursor(0, 0);
lcd.print(currentMillis / 1000);
}
lcd.setRGB(random(0, 255), random(0, 255), random(0, 255)); // set screen to random color;
}
}