Hello everyone.
I wrote a program a couple years ago that counts time and changes the state of some grove relays at specified times. It also sends some data to a grove LCD screen to monitor everything.
The program works fine except that it intermittently freezes and I have to reset the board. I would like the program to run indefinitely, as it is controlling fans and humidifiers to grow Oyster mushrooms, so when it goes down and I don't notice it right away things can dry out and I lose potential yields.
My current theory is that after a certain point the variable I have holding the millis() time gets maxed out somehow after the program runs for a while, but I don't really think that's true because it is controlled by a modulo which should keep the number low. If that's not the problem I wonder if it is something like that.
Board is Arduino UNO.
Sorry if I am missing any relevant technical details, I wrote the program a couple years ago and haven't done any work with Arudino since. I really hope there is an easy answer to run a program in this fashion.
I really appreciate any help anyone can offer. Thank you!
Here is the code:
// Relay 1 is controlling fae, currently will be right side of double gang box
// Relay 2 is controlling humidifier, which I took out of the code on 2021-01-21
// transfered to internal hard drive on HP laptop new linux install February 14, 2022
// board is Arduino Uno
// if using on fresh install, add user to dialout group
// last updated 03/06/22
const int relayOne = 3; // base shield pin number for Relay 1 // Connect the LED to Arduino digital pin 4, Grove socket D4
const int relayTwo = 4; // base shield pin number for Relay 2
int changeRelay = 0;
//timer for fae
const long faeOnInterval = 60000; // 60 sec fae
const long humOnInterval = 90000; // 90 sec hum
const long allOffInterval = 1050000; // 17.5 minutes all off, 20 minutes total cycle
unsigned long timeToNextEvent = 0;
unsigned long totalSequenceRunTime = 0;
unsigned long previousDisplayMillis = 0;
unsigned long currentDisplayInterval = 1000;
unsigned long currentInterval = 0; //current time, could delay start time
unsigned long previousMillis = 0; // will store last time LED was updated
int faeRelayOneState = LOW;
int currentState = 2; //immediately advances to 0
// rgb screen
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
int colorR = 255;
int colorG = 255;
int colorB = 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! fun.
lcd.setRGB(colorR, colorG, colorB);
totalSequenceRunTime = faeOnInterval + humOnInterval + allOffInterval;
}
void loop() {
timerSequencerFunctionality();
}
void timerSequencerFunctionality() {
unsigned long currentMillis = millis() % totalSequenceRunTime;
//display timer
if ((unsigned long)(currentMillis - previousDisplayMillis) >= currentDisplayInterval) {
lcd.setCursor(0, 0);
lcd.print(currentMillis);
lcd.setCursor(0, 1);
lcd.print(timeToNextEvent);
lcd.setCursor(8, 1);
lcd.print(totalSequenceRunTime);
previousDisplayMillis = currentMillis;
if (currentState == 2)
{
lcd.setRGB(random(0, 255), random(0, 255), random(0, 255)); // set screen to random color;
}
}
if ((unsigned long)(currentMillis - previousMillis) >= currentInterval) {
currentState = (currentState + 1) % 3;
lcd.clear();
switch (currentState) {
case 0: // fae
digitalWrite(relayTwo, LOW); // turn off hum
faeRelayOneState = HIGH; // turns relay to ON
currentInterval = faeOnInterval;
timeToNextEvent = (currentMillis + faeOnInterval) % totalSequenceRunTime;
lcd.setCursor(8, 0); // set text print position
lcd.print(" fae ON "); // display text
lcd.setRGB(random(0, 255), random(0, 255), random(0, 255)); // set screen to random color;
//}
break;
case 1: //run hum
digitalWrite(relayTwo, HIGH); // turn on hum
faeRelayOneState = LOW; // turns relay to OFF
currentInterval = humOnInterval;
timeToNextEvent = (currentMillis + humOnInterval) % totalSequenceRunTime;
lcd.setCursor(8, 0); // set text print position
lcd.print(" hum On "); // display text
lcd.setRGB(random(0, 255), random(0, 255), random(0, 255)); // set screen to random color;
break;
case 2: //all off
digitalWrite(relayTwo, LOW); //shut off hum relay
currentInterval = allOffInterval;
timeToNextEvent = (currentMillis + allOffInterval) % totalSequenceRunTime;
lcd.setCursor(8, 0); // set text print position
lcd.print(" all OFF"); // display text
break;
}
previousMillis = currentMillis;
digitalWrite(relayOne, faeRelayOneState);
}
}
I hope someone has some ideas.
Thank you!