I am trying to program my Arduino Uno board for a 24 hour cycle. I have the Arduino board and two relays. The aim is; every 24 hours relay 1 has to switch on for 5 seconds, and 1 second later relay 2 has to switch on for 15 seconds. I have a sketch for a 12 hour cycle, and that works fine, but when I try for a 24 hour cycle, by changing the const unsigned int totalTime from 43200 seconds to 86400 seconds, everything goes wrong.
24 hour sketch - what is wrong?
//turn relays on and off at certain times.
//when the digital output pin is HIGH, then the relay is off.
//when the digital output pin is LOW, then the relay is on.
enum actioEnum {
RELAY1_ON,
RELAY1_OFF,
RELAY2_ON,
RELAY2_OFF,
};
struct tableStruct{
unsigned int time;
actionEnum action;
}
const table [] = {
{0,RELAY1_ON},
{5,RELAY1_OFF},
// remaining 86395 for a total of 86400.
{0,RELAY2_OFF}, // not needed, it is already off.
{6,RELAY2_ON},
{6+15,RELAY2_OFF},
// remaining 86379 for a total of 86400.
};
const unsigned int totalTime = 86400; // total amount for sequence
unsigned int seconds;
unsigned long previousMillis;
const unsigned long interval = 1000; // 1000 ms; // 1000 ms is one second
const int relay1Pin = 2;
const int relay2Pin = 3;
void setup() {
serial.begin(9600);
// the relay is off when the pin is high.
// turn output high before setting the pinMode.
// then the output changes from a floating pin to high,
// and the relays will not be activated.
digitalWrite (relay1Pin, HIGH);
pinMode (relay1Pin, OUTPUT);
digitalWrite (relay2Pin, HIGH);
pinMode (relay2Pin, OUTPUT);
}
void loop () {
// a one second timer with millis
if (millis() - previousMillis >= interval) {
previousMillis += interval;
// scan table
for ( int i = 0; i < int (sizeof(table) / sizeof (tableStruct)); i++) {
if (table [i] . time == seconds) {
switch (table [i] . action) {
case RELAY1_ON:
Serial.println("Relay 1 on");
digitalWrite (relay1Pin, LOW); // low is relay on.
break;
case RELAY1_OFF:
Serial.println("Relay 1 off");
digitalWrite (relay1Pin, HIGH); // high is relay off.
break;
case RELAY2_ON:
Serial.println("Relay 2 on");
digitalWrite (relay2Pin, LOW); // low is relay on.
break;
case RELAY2_OFF:
Serial.println ("Relay 2 off");
digitalWrite (relay2Pin, HIGH); // high is relay off.
break;
default:
Serial.println ("Error, a bug in the scketch");
}
}
}
seconds++;
if (seconds >= totalTime) {
seconds = 0;
}
}
}