Timer program not working with higher intervals

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();

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);

You have your relay intervals declared as int. An int value can't be greater than 32767.

Thank you Todd!

Is there any point in me leaving my runTime values as ints if they're always going to be lower than that?

Thank you Todd!

Is there any point in me leaving my runTime values as ints if they're always going to be lower than that?

Yes, it will save you some memory. If you want to also save memory for the intervals of 40000 and 60000, you can use unsigned int, instead of long.

as long as you don't really run out of memory all variables that store a value from millis should be defined as unsigned long.

Question to the specialists:
which part of an unsigned long is stored into an unsigned int





the upper part or the lower part?

best regards Stefan

I would say the lower 16 bits, 15 to 0 (the right half of an unsigned long).

Yes, an implicit down cast by truncation will work even for signed types. e.g.

11111111111111111111111111111110 (-2)
1111111111111110 (-2)

As with the unsigned types, the range can't be exceeded safely.