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.
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)
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
}
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.