I made a relay timer program using Robin2's multiple things at once sketch.
I was really chuffed because it appeared to work and is a much nicer way of coding making separate functions to keep the loop from being filled with rubbish. However, although it works fine at 5000 intervals, when I up my interval to 60000 and 40000 respectively (I actually intend to have the interval at more like 5 minutes but this was just a test), it doesn't appear to work.
I even added a serial printout of the currentMillis to make sure it was making it up to 60000 (and it does), but my relays don't get switched.
Any ideas?
Heres my code, before I added the serial prints
const int relayPin_1 = 9; //relay output pins
const int relayPin_2 = 10;
const int relayInterval_1 = 60000; // intervals for timing relays
const int relayInterval_2 = 40000;
const int relayRunTime_1 = 5000;
const int relayRunTime_2 = 3000;
byte relayState_1 = HIGH; // for keeping track of ON/OFF
byte relayState_2 = HIGH;
unsigned long currentMillis = 0;
unsigned long previousRelayMillis_1 = 0;
unsigned long previousRelayMillis_2 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(relayPin_1, OUTPUT); //configuring relay pins
pinMode(relayPin_2, OUTPUT);
}
void loop() {
currentMillis = millis();
updateRelayState_1();
updateRelayState_2();
switchRelays();
}
void updateRelayState_1() {
if (relayState_1 == HIGH) {
if (currentMillis - previousRelayMillis_1 >= relayInterval_1) {
relayState_1 = LOW;
previousRelayMillis_1 += relayInterval_1;
}
} else {
if (currentMillis - previousRelayMillis_1 >= relayRunTime_1) {
relayState_1 = HIGH;
previousRelayMillis_1 += relayRunTime_1;
}
}
}
void updateRelayState_2() {
if(relayState_2 == HIGH) {
if(currentMillis - previousRelayMillis_2 >= relayInterval_2) {
relayState_2 = LOW;
previousRelayMillis_2 += relayInterval_2;
}
} else {
if (currentMillis - previousRelayMillis_2 >= relayRunTime_2) {
relayState_2 = HIGH;
previousRelayMillis_2 += relayRunTime_2;
}
}
}
void switchRelays() {
digitalWrite(relayPin_1, relayState_1);
digitalWrite(relayPin_2, relayState_2);
}