Pausing a sketch for a random period of time

Using Arduino Uno with Windows 7. New to programming. I have a sketch ( attached) which is blinking my LEDS at just the right amount of time. I would like to pause the sketch after it runs once and then have the sketch start up after a period of time I can control, probably from 5 to 10 seconds. I have tried random number generator sketches but I don't know where to put them in the program so that they will work properly. Thanks for help. Rich Carney

// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  12;
const int led3Pin =  11;
const int led4Pin =  10;
const int led5Pin =   9;

int led1State = LOW;        // initialize the LED
int led2State = LOW;
int led3State = LOW;
int led4State = LOW;
int led5State = LOW;

unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long count4 = 0;
unsigned long count5 = 0;

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);      
  pinMode(led3Pin, OUTPUT); 
  pinMode(led4Pin, OUTPUT);      
  pinMode(led5Pin, OUTPUT); 
}

void loop() {
  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count1, 700UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 

  if (timeout(&count2, 1000UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    } 
    digitalWrite(led2Pin, led2State);
  } 

  if (timeout(&count3, 200UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW; 
    } 
    digitalWrite(led3Pin, led3State);
}

 if (timeout(&count4, 600UL )) {
    if (led4State == LOW) {
      led4State = HIGH;
    }
    else {
      led4State = LOW; 
    } 
    digitalWrite(led4Pin, led4State);
 }
  if (timeout(&count5, 90UL )) {
    if (led5State == LOW) {
      led5State = HIGH;
    }
    else {
      led5State = LOW; 
    } 
    digitalWrite(led5Pin, led5State);
  
  }
}

moderator added code tags - # button above smileys

this will help us more that you.

http://forum.arduino.cc/index.php/topic,148850.0.html

pay attention to #7

as for your problem, load the sketch 'blink without delay' notice that you use a value for the timing, just figure out what you want to use. you can use that in a similar way.

    if (led5State == LOW) {
      led5State = HIGH;
    }
    else {
      led5State = LOW; 
    }

can be written as

    led5State = !led5State;     // invert the current state

probably the same sketch size but it saves you some typing ;)

Since the core of that sketch is my code, I suppose I should really take an interest. :D

The problem is that you say "I would like to pause the sketch after it runs once". Since it is blinking a number of LEDs (repeatedly) at different intervals, there is no real meaning to "runs once". By the time one LED performs a blink, another will have blinked a number of times given your settings and it may take some time before all LEDs are simultaneously off.

So - do please explain more clearly how you wish to start and stop the blinking?

Paul, the effect I am after is lightening in a cloud. I want it to "flicker" as you would expect to see it in nature. Not a bolt of lightening though. The multiple LEDs are giving me the desired effect, coming on at different times, for different periods. The problem is, it does not stop. What I want to do it stop the sketch after it has run long enough to give me the desired effect (as you pointed out, I guess not once but for a period of time), and then restart the sketch after another period of time. To give the best effect, I would like the "off) period to be random (Say between 5 and 10 seconds). I know this is a lot. I have been working on what you gave me last time to get to where I am now but I get nothing but faults when I enter my own code to try this new stuff. Thanks if you can help. Rich Carney

Hi Rich

You want the gaps between the lightning to be random in the range 5 to 10 seconds. How about the bursts of lightning? How long do you want the lightning to be "on" for?

One way to build on your existing code would be like this:

  1. Add a global variable boolean lightningOn;.

  2. Leave your existing code (that controls the flickering) unchanged, except for ...

  3. Where you call digitalWrite(led1Pin, led1State); for each of your LEDs, put them in if statements:

if (lightningOn)
{
    digitalWrite(led1Pin, led1State);
}
else
{
    digitalWrite(led1Pin, LOW); // Assuming LOW turns LED off
}

So led1State etc will still be flickering on and off, but the global variable will override them and force the LED off.

  1. Add additional timeouts to alternate lightningOn to be true or false. The timeout that sets the variable true needs to use a random number to give the 5 to 10 seconds duration.

Regards

Ray