Im trying to blink a series of LED’s until a button is pushed. I was trying to use a DO While command but you have to hold down the button until the code comes around to the button check. Anyone want to help me simplify this?
void loop()
{
do
{
analogWrite(led1, 75); // Turn ON the LED
analogWrite(led2, 75); // Turn ON the LED
analogWrite(led3, 75); // Turn ON the LED
analogWrite(led4, 75); // Turn ON the LED
analogWrite(led5, 75); // Turn ON the LED
analogWrite(led_low, 0); // Turn off the LED
delay(750); // Wait for .75 seconds
analogWrite(led1, 255); // Turn ON the LED
analogWrite(led2, 255); // Turn ON the LED
analogWrite(led3, 255); // Turn ON the LED
analogWrite(led4, 255); // Turn ON the LED
analogWrite(led5, 255); // Turn ON the LED
delay(750); // Wait for .75 seconds
val = digitalRead(button5);
val = digitalRead(button4);
} while (val = 0);
This can be done many ways. The way I like to use is to git rid of the delay you have and use a comparrison against millis() to determine when to swtich.
I actually wrote a library a while back which may suite your needs: https://github.com/harrisonhjones/LEDEffect
The following code (as an example) would blink the LEDs if the button is not pressed and stop blinking them while the button is pressed.
#include <LEDEffect.h> // May require #include "LEDEffect.h" instead if the compiler complains
LEDEffect led1(A0);
LEDEffect led2(A1);
LEDEffect led3(A2);
LEDEffect led4(A3);
LEDEffect led5(A4);
LEDEffect led6(A5);
void setup() {
}
void loop() {
led1.update(); // Call update as often as possible for smooth animation
led2.update(); // Call update as often as possible for smooth animation
led3.update(); // Call update as often as possible for smooth animation
led4.update(); // Call update as often as possible for smooth animation
led5.update(); // Call update as often as possible for smooth animation
led6.update(); // Call update as often as possible for smooth animation
if(digitalRead(btn1))
{
led1.off();
led2.off();
led3.off();
led4.off();
led5.off();
led6.off();
}
else
{
led1.blink(250); // Set the LEDs a blink'n
led2.blink(250); // Set the LEDs a blink'n
led3.blink(250); // Set the LEDs a blink'n
led4.blink(250); // Set the LEDs a blink'n
led5.blink(250); // Set the LEDs a blink'n
led6.blink(250); // Set the LEDs a blink'n
}
}