Relay Switching using millis timer

Hello,
Currently I am working on a project that will switch relays for a given duration. To explain, a user will input the desired relay to switch open to closed (from an 8 channel relay) and its duration using Nextion touchscreen. There are a total of four countdown timers before the program is over.
Problem: The relays will switch open once its countdown timer is over as I wanted it to. However, sometimes the program does not work. As in, it will stop working midway of the countdown. I will have to reset the program and try again. There is no pattern to when it is successful or not ( it's 50/50 chance).
I am not sure whether this is a coding problem, faulty Arduino pins, or coding issue. I have attached the code below. The focus is on the loop statement but I attached the entire code for clarification. Any assistance or recommendations is greatly appreciated

void loop() {

  nexLoop(nex_listen_list);
  time1 = LP1;
  time2 = LP2;
  time3 = LP3;
  time4 = LP4;
  R1 = R_1;
  R2 = R_2;
  R3 = R_3;
  R4 = R_4;

  while ( (test1 == false) && (timeToStart == 0) ) { // Once the START is Initiated.
    nexLoop(nex_listen_list);
  }

  if (timeToStart == 0) {
    timeToStart = millis();
  }

  // Check if millis() falls within time range, and run function. If not, kill function.
  if ( (millis() >= timeToStart) && (millis() < timeToStart + time1) ) {
    resistorTest(R1);
     
  }


  // CODE REPEATS FOR DIFFERENT TIMES AND RESISTOR VALUES.

  else if ( (millis() >= timeToStart + time1) && (millis() < timeToStart + time1 + time2) ) {
    resistorTest(R2);
  }

  
  else if ( (millis() >= timeToStart + time1 + time2) && (millis() < timeToStart + time1 + time2 + time3) ) {
    resistorTest(R3);
  }


  else if ( (millis() >= timeToStart + time1 + time2 + time3) && (millis() < timeToStart + time1 + time2 + time3 + time4) ) {
    resistorTest(R4);
       
  }

  else {
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    test1 = false;
    timeToStart = 0;
    
    
  }


}

March_27_Load_Code.ino (12 KB)

Don't post snippets (Snippets R Us!)

weird stuff that happens only from time to time when you use multiple relays could be a linked to power issues

Note that it's best to test time using subtraction rather than addition if you want to handle correctly the moment when you overflow 32 bits variables and millis goes back to 0. So instead of doing if ( (millis() >= timeToStart + time1) ...prefer thisif ( (millis() - timeToStart >=  time1) ...

If you are using multiple timers that start and stop, check out my tutorial on
How to write Timers and Delays in Arduino it includes a simple millisDelay class that keep track of each timer/delay for you.

variables that use function millis() always should have type unsigned long.

relay-coils cause a voltage-spike when switched off. Any coil should have a free-wheeling diode to suppress the voltage-spikes. If you use a ready to use relay-shield the shield might have freewheeling-diodes. If it is one of the super-cheep chinese one it might not.

calculating time-differences should always be done
if (currentTime - previoustime > timeperiod)
the counter of millis() rolls over from max to zero after 49 days
and then only the calculation with
if (currentTime - previoustime > timeperiod)
delivers a correct value

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.