Alternative to Delay()

HI guys trying to blink 4 led in a sequence (random) on button ON I am using Delay(), but i came across this:

"While it is easy to create a blinking LED with the delay() function, and many sketches use short delays for such tasks as switch debouncing, the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt"

it is bringing it to a halt or till it ends cycle. I am looking to interrrupt the sequence as soon as i turn button OFF.

any help please on how the code could be simply.

TIA

I don't really get what you mean? Do you mean that you can't read the button because of the delays in your program? If so you could use an interrupt for that (pin 2 & 3 on Arduino Uno)

2 button on pin 2 and pin3

pin 2 = pressed the led on , as soon as i release or press pin 3 (digital) it should break loop.

  • how would you do that with interrupt , thanks,
boolean pressed = false;
void setup(){
pinMode(INT0,INPUT_PULLUP); //interrupt on pin 2, internal pullup ativated
attachInterrup(0,press,FALLING); 
}

void loop(){
while(pressed==1){
LEDloop();
}
//write all led's low here for when the button is released
}

void press(){
detachInterrupt(0);
pressed = 1; 
attachInterrupt(0,release,RISING);

}

void release(){
detachInterrupt(0);
pressed = 0; 
attachInterrupt(0,press,FALLING);
}

void LEDloop(){
//here comes your led sketch with delays
}

I will try it ! Thanks Bud.

The words immediately following the text you quoted...

"For alternative approaches to controlling timing see the millis() function and the sketch sited below. "

Can you please explain the Interrupt Part on Pin2 ? so it can be set for Pin 2 and Pin 3 ?

The demo several things at a time illustrates the use of millis() to manage timing. It flashes 3 LEDs. I will leave it as your homework exercise to extend it to 4 or more.

Using interrupts to get around the limitations of the delay() function is like hiring an orchestra because you don't know how to work your CD player.

...R

thanks Robin2,

exactly what I am looking for - multi process without halting another. like using different threads..

I shall apply it and report back.