Multiple Random Relays

I'm designing myself an arduino controlled relay box using the sparkfun Beefcake relay kits for an installation I have upcoming this wednesday. I'm very comfortable with all the physical aspects of my project and everything works fine except I'm having a difficult time learning the programming, I've been fighting it all week, thinking I had enough time.

I am comfortable with basic programming, but I am trying to get 5 relays (pins 9-13) to fire for random lengths of time with random pauses between them. I'd like it to be non-sequential and have each relay have a mind of it's own so to speak. I'm trying to get them to fire sparsely (Off for a minute to five minutes, on for 5-45 seconds). Them overlapping is a desired outcome, but I'd also like the relays to exhibit a quiet, slow pace.

Right now I'm using this code, found from a previous forum post, but I'll be the first to admit that I don't totally understand it, as I've been manipulating the random integers (I know I'm not looking for 1000 to 5000, they're just placeholders) and I can't really get it to stay on/off for the desired times.

#define numberOfLEDs 5
long nextFlash[5];
int ledPin[] = { 9, 10, 11, 12, 13}; // LED pins to use.
int ledState[5];

void setup(){
 for(int i = 0; i<numberOfLEDs; i++){
   pinMode(ledPin[i],OUTPUT);
   ledState[i] = LOW;
   digitalWrite(ledPin[i], LOW); // all LEDs off
     nextFlash[i] = millis() +random(1000, 5000);
 } 
}

void loop(){
for(int i = 0; i<numberOfLEDs; i++){
if(millis() > nextFlash[i]){
  if(ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
  digitalWrite(ledPin[i],ledState[i]);
  nextFlash[i] = millis()+random(1000, 5000 ) ; // next toggle random time
} }
}

Help a new guy out

I guess what I'm looking for is how to delay individual pins for random times without using the delay command and pausing everything.

That code would make a lot more sense if each { was on a new line, and each } was on it's own line, and if Tools + Auto Format was used.

The array sizes should all be based on numberOfLEDs, not 5 or allowing the compiler to count the initializers.

Compound statements should be spread down over multiple lines, not across the page on one line.

That code does something. It appears as though it does what you want.

You want it to do something. Clearly what it does is not what you want.

What is actually does is not clear, and how that differs from what you want is not clear. If you clear up the confusion, we'll help you clear up the code.

The code I have fires the relays for random intervals with random pauses, but It doesn't give me control to set the intervals I need. The two random values in the code are for the max/min for the firing but I don't know how to introduce a random interval for off length without using delay.

Sorry about the formatting I'm just getting started. What do you mean about array sizes? Also, auto format was used.

I guess what I'm looking for is how to delay individual pins for random times without using the delay command and pausing everything.

Two ways:

  1. use a timer to "tick": you keep track of the starting "tick" of a relay and test to see if the desired length of time has passed. The timer can be a hardware timer, or a loop counter if the loop's duration is constant; I prefer a hardware timer for consistency. This approach can track multiple delays with one timer.

  2. use individual timer for each delay: you simply need to decrement the desired passage and when it reaches zero, you know that your time is up. This requires multiple timers for multiple delays.

This requires multiple timers for multiple delays.

Or ditch the whole timer idea.

OP, how would YOU perform what you want to do? You have code that chooses a random relay to toggle, so, clearly you know how to use arrays and the random function. All you need is another array of intervals, chosen randomly when a new interval is required (because a relay has been chosen to be turned on), and an array of lastTimeOn times.

Then, on each pass through loop, see if a relay is on (lastTimeOn[n] is non-zero). If so, see if now - then is greater than the desired on time for that relay. If so, turn it off and set lastOnTime[n] and interval[n] to 0.