Millis (s) Function to Control Keyes Relay

I am currently trying to use a Keyes_SRly to turn a probe on and off. I have been able to turn the probe on and off every 1 second using the https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay Blink Without Delay tutorial. However, I need to ensure my probe collects data for a second and is then turned off for 10 minutes using the relay. I am not posting my whole code because it is very long, but I have posted relevant portions. I would appreciate any tips on how to go about turning the relay on for a second and then off for 10 minutes.

const int relay_pin (8);
bool relaystate = false;
unsigned long previousMillis = 0;
unsigned long interval = 1000;

void setup()
{
 
 pinMode(relay_pin,OUTPUT);
  digitalWrite (relay_pin, relaystate);

void loop()
{
unsigned long currentMillis = millis();

if ((unsigned long)(currentMillis - previousMillis) >= interval) {
relaystate = !relaystate;
}
digitalWrite (relay_pin,relaystate);
  previousMillis = millis();

This is my first time posting here, so I am still getting used to posting protocol. Thanks again!

read this:

https://forum.arduino.cc/index.php?topic=223286.0

The problem with your snippet is not only that it is incomplete and formatting is a mess, you're also setting previousMillis every time loop() runs. That's not going to work out well.

Not sure if that's intended as closing } are missing.

Usually "the problem is in the part you didn't post" but here there are other problems with the parts you did post making it still useless.

Indeed, write a short sketch that demonstrates the problem (and with a little luck in doing so you can get it to behave the way you need it to already, after which it's a matter of integrating it in your big sketch).

there are many ways of doing timing.
this is one way of doing it.

you have to alter the digital write for your names.

Since you said, turn the relay on for 1 minute, then off for 10, the cycles is 11 minutes.

unsigned long LastTime, duration ;
int timePulse  ;
int relay ; // 


void setup()   /************************ SETUP  ************************/
{
      LastTime = millis() ;


      timePulse  = 650;  //  650 is 650 seconds if timpulse increments once per second.
                         // this will turn the relay on in 10 seconds after start or reset
} /* --------------------  END SETUP -----------------------*/


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
    duration = millis()-LastTime;  // used as the timer 
  if (duration >= 1000)   // one second interval
               {
	timePulse  ++;  // increments once per second
	LastTime = millis();   // re-sets the timer
                }  


if(timePulse  == 0 ) {  digitalWrite(relay,HIGH) ;  }   turns relay on 
if(timePulse  == 60) {  digitalWrite(relay, LOW);  }  turns relay off after 1 minute
if(timePulse  >= 660 {  timePulse  = 0;  }   resets after 11 minutes


// rest of your stuff goes here


} //  /* ------------------  END LOOP -------------------- */

Hey Dave,

@dave-in-nj thank you for the help! The relay is working fine now, quick question though, how do I define the time in milliseconds instead of seconds. I.e. one of my other sensors needs to be on for 500 ms but off for 2000 ms (2 seconds). I would really appreciate the help! Thank you :slight_smile:

List your timing
sensor 1 on for 60 seconds
off for 10 minutes
Sensor 2 on every 1/2 second
off every 2 second

pokemon12:
.... how do I define the time in milliseconds instead of seconds. I.e. one of my other sensors needs to be on for 500 ms but off for 2000 ms (2 seconds). I would really appreciate the help! Thank you :slight_smile:

https://www.arduino.cc/en/pmwiki.php?n=Reference/Millis