I am trying to get relays to trigger at random and a quick youtube search reveals a code that does just that. However, I need 1 of those relays to give me a 1 second pulse at a random time between say 2 mins and 4 mins (example)
This is what I am starting with - any help would be very much appreciated
So this one relay isn't entirely random. That will screw up the ability to control everything with delay. You need to focus on getting the one relay to work right first. You could control it with BlinkWithoutDelay.ino example logic, but by changing the off interval to your random 2-4 minutes.
You can't easily do multiple relays with delay() since while one delay is running, the others aren't. A 2-4 minute delay() would mean that NOTHING happens for 2 to 4 minutes.
You will have to switch to millis() timers for scheduling turning relays on and off.
By 'trigger' do you mean 'toggle'? If not, how long does each relay stay on after it has been 'triggered'? We know that one relay stays on for one second but you don't say anything about the other three,
Thanks for getting back to me. The other 3 relays I think I have figured out....i think lol. It's just the fourth which I need a random 1 sec pulse somewhere in the time range of 2 to 4 mins.
Using millis makes sense as the first 3 relays will be using the delay parameter so the will be 2 different sets of commands running at the same time. I am totally new to programming anything like this but the way my mind works, once I see how something works it all makes sense and I can build from there. Once I get the first bit of success I will learn quickly but I don't know where to start on this
The code for 4 relays is at the top of this thread. I didn't format it as per forum rules but I am still getting used to the forum. That code works fine for those 4 relays (or as many as I want really)
I just need a way to get a separate relay to give me a 1 sec pulse within a given time range. The first set of relays use the delay parameter which will delay the entire sequence so another user has suggested using millis along side (or instead of?) delay.
If I sound like I don't know what I am talking about, well, it's because I don't know what I am talking about lol. I am totally new to this.
It's OK to go back and edit a previous post for formatting (unless maybe someone has commented on your formatting). It's not OK to edit/alter that previous post for content since doing that makes a hash of any posts discussing the code. If the code changes put up a new post with the modified code..
random() gives you a random signed (!) long value back (can be in a range or full range for a long value).
What do you do with it - up to you:
it could mean minutes, but also seconds ...
just bear in mind if the value returned is negative: use ABS() to make it positive (for time value):
do not call a Wait(), Delay() with a "negative time"
You do it already right (using min. and max.) - where is the problem?
Just check if delay() uses parameter in milli-seconds, for sure not minutes.
For minutes you have to multiply your random() result ( * 1000 * 60, assuming delay() takes milli-sec).
The code linked in Pulse a relay at a random time - #3 by Idahowalker is a about doing several things at once is a good way to handle the separate timed relay versus the others.
However you'd need to change the delay()s in your current code for the other relays to use the millis() paradigm with something like this untested edit:
What if a 'delay(100)' was in a loop of ten, inside a loop of sixty, which was also in a loop of random, two to four iterations (you might recognize it as a buslist)... squeeze a command or two every hundredth delay would be like taking a slice of bread from the middle of a loaf... i could try drawing it in ascii.
I am confused (surprise surprise)
I see how relays 6 7 8 9 are doing random stuff via delay but I don't see how the millis interacts. If we assign one of those relays (or even a separate new relay) to be associated with the millis behavior then I would be able to see that?
The task can easily be realised with a structured array.
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it ?
The nice thing about the millis() paradigm versus delay() is that millis does not interact while delays do interact. If you have the special relay using delay for a 1 second on and 2-4 minutes off, delay takes all the attention of the processor while it quickly tests every microsecond whether the time is up. Meanwhile, the other relays' random behavior would be waiting for the special relay's delay to finish. Millis() wise, the loop() happens fast, and each chunk takes turns checking the clock to see if it is time to do anything, quickly passing control to the next chunk if not.
So to add a special relay to the code I posted, you add another millis-based chunk that toggles with the desired timing. Repurposing RELAY1 you get something like this untested chunk:
// Special 1 sec on/2-4 minute off relay
if (currentMillis - lastSpecial > intervalSpecial) {
lastSpecial = currentMillis;
digitalWrite(relays[0], !digitalRead(relays[0]));
intervalSpecial = digitalRead(relays[0])?1000:random(120000,240000);
}
(This chunk uses the sneaky "ternary operator" (x?y:z) to toggle the delays between on and off.)