# How to implement a relay delay on, instant off

Hi, I am building a voltage comparator that switches two relays depending on analogue input voltages.

It all works, but I can't find a simple way to implement a delay for switching each relay on delayed by a second, but instantly off, by using millis().

I will have two boolean values - relayA and relayB = true or false And two boolean delay values relayADelay and relayBDelay

I've tried this but it's not quite right:

if(relayA == 0){digitalWrite(0, LOW); //A relay off } if(relayB == 0){digitalWrite(1, LOW); //B relay off }

if(relayA == 1 && relayADelay == 0){ timerA = millis(); relayADelay = 1; } if(relayA == 1 && millis() - timerA > delayOn){ digitalWrite(0, HIGH); //A relay on after delay relayADelay = 0; } if(relayB == 1 && relayBDelay == 0){ timerB = millis(); relayBDelay = 1; } if(relayB == 1 && millis() - timerB > delayOn){ digitalWrite(1, HIGH); //B relay on after delay relayBDelay = 0; }

You could arrange it differently to get a more clear solution.

3 different parts in your case, and they all run all the time in loop().

1) input reads the analog voltages and sets variables to signal the desired relay states.

2) process looks for a change in the desired state of each relay. if so it then sets up start time and interval (either 0 or 1000) for the to-change relay.

3) output for each relay. if interval == 0 then shut it off else if millis() - start >= interval then turn it on

And that should handle it... almost, the output code will need flags to not keep turning the relays on every 1 second, perhaps just keep track of state and not run the time check if relay state is ON.

PS -- something that you should know, the else statement.

That is real basic, knowing the tools at that level (raw beginner stuff) can save you a lot of time and frustration. Did you know that the main Arduino site is full of references and tutorials just to help you get set up and prepared?

The main Reference Page.

Look in the Learning popdown in the bar at the top of every page (even the forum) and start checking and bookmarking the pages (be sure to make a folder in bookmarks or the marks you make could be hard to find later) after a look. Then when you use the IDE, open a browser and then in tabs as many pages as you need to be able to look up what you don't know. Online help, easier than using books and on most browsers you can size the text up to read easier.

mazellan: I will have two boolean values - relayA and relayB = true or false

I am a great believer that meaningful variable names make it easy to follow the logic of a project.

In this case `relayA` is NOT the state of the relay - it is the state of something else - something that was determined based on your analogRead()s.

At the very least it should be `desiredRelayAState` and `desiredRelayBState`

In any case, I think this may be a more logical way to tackle the problem

``````if(desiredRelayAState == 0){
digitalWrite(relayAPin, LOW)
relayAStartMillis = millis(); // reset the clock
}

if (millis() - relayAStartMillis >= interval) {
digitalWrite(relayAPin, LOW)
}
``````

and do the same thing for the other relay

I am assuming the relay should only go ON if the on-state continues throughout the interval.

...R