blinking leds with a momentary switch help!

ive been searching the depths of the internet for something that could help me figure this code out. i am fairly new to coding so as much simplicity in explanation as you can would be appreciated.

i am building a project and i would like to use my arduino to have 4 momentary buttons. i would like each one of these buttons to control a few blinking leds in sequence. where the problem starts is.... i would like the momentary button to be pushed and trigger the led sequence to run once. also that it doesn't matter whether the button is held or released. the sequence should run once all the way through, once triggered by the button.

if someone with greater knowledge could come up with the code that would be amazing. but any help at all is greatly appreciated

Hi and Welcome to ArduinoLand.

You'll get loads of help, but it's customary for you to go first. Post what you have already tried- code and circuit- and explain what's not working properly, then you'll get some advice.

But first, have you worked through the examples in the IDE, at File > Examples, or you can also see them here. In a project like that, there is no doubt that BlinkWithoutDelay will be your friend: embrace it sooner rather than later and save yourself a heap of trouble later.

After a lot of hair tearing out this is what i've come up with. It works, but i actually don't know why it works. the momentary button is pushed and the led blinks even if the button is released. just for learning purposes can anyone explain why this is working? :confused: :stuck_out_tongue:

const int buttonPin1 = (A3);     
const int ledPin1 =  (13);      
const int buttonPin2 = (A2);     
const int ledPin2 =  (12);
const int buttonPin3 = (A1);     
const int ledPin3 =  (11);      
const int buttonPin4 = (A0);     
const int ledPin4 =  (10);

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;


void setup() {
   pinMode(ledPin1, OUTPUT);      
   pinMode(buttonPin1, INPUT);  
   pinMode(ledPin2, OUTPUT);      
   pinMode(buttonPin2, INPUT);
   pinMode(ledPin3, OUTPUT);      
   pinMode(buttonPin3, INPUT);  
   pinMode(ledPin4, OUTPUT);      
   pinMode(buttonPin4, INPUT);  
}

void loop(){
 
 buttonState1 = digitalRead(buttonPin1);

 if (buttonState1 == HIGH) {     
       
 digitalWrite(ledPin1, HIGH);
 delay (100);
 digitalWrite(ledPin1, LOW); 
 delay (100);
 digitalWrite(ledPin1, HIGH);
 delay (100);
 digitalWrite(ledPin1, LOW); 
 delay (100);
 digitalWrite(ledPin1, HIGH);
 delay (100);
 digitalWrite(ledPin1, LOW); 
 delay(100);
 digitalWrite(ledPin1, HIGH);
 delay (100);
 digitalWrite(ledPin1, LOW); 
 delay(100);
 digitalWrite(ledPin1, HIGH);
 delay (100);
 digitalWrite(ledPin1, LOW); 
 delay(100);
 } 
 else {
   
   digitalWrite(ledPin1, LOW); 
 
 buttonState2 = digitalRead(buttonPin2);
 if (buttonState2 == HIGH) {     
      
 digitalWrite(ledPin2, HIGH);
 delay (100);
 digitalWrite(ledPin2, LOW); 
 delay (100);
 digitalWrite(ledPin2, HIGH);
 delay (100);
 digitalWrite(ledPin2, LOW); 
 delay (100);
 digitalWrite(ledPin2, HIGH);
 delay (100);
 digitalWrite(ledPin2, LOW); 
 delay(100);
 digitalWrite(ledPin2, HIGH);
 delay (100);
 digitalWrite(ledPin2, LOW); 
 delay(100);
 digitalWrite(ledPin2, HIGH);
 delay (100);
 digitalWrite(ledPin2, LOW); 
 delay(100);
 } 
 else {
  
   digitalWrite(ledPin2, LOW); 
   
    buttonState3 = digitalRead(buttonPin3);
   if (buttonState3 == HIGH) {     
      
  digitalWrite(ledPin3, HIGH);
  delay (100);
  digitalWrite(ledPin3, LOW); 
  delay (100);
  digitalWrite(ledPin3, HIGH);
  delay (100);
  digitalWrite(ledPin3, LOW); 
  delay (100);
  digitalWrite(ledPin3, HIGH);
  delay (100);
  digitalWrite(ledPin3, LOW); 
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay (100);
  digitalWrite(ledPin3, LOW); 
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay (100);
  digitalWrite(ledPin3, LOW); 
  delay(100);
 } 
 else {
  
   digitalWrite(ledPin3, LOW);
  
   buttonState4 = digitalRead(buttonPin4);
  if (buttonState4 == HIGH) {     
       
  digitalWrite(ledPin4, HIGH);
  delay (100);
  digitalWrite(ledPin4, LOW); 
  delay (100);
  digitalWrite(ledPin4, HIGH);
  delay (100);
  digitalWrite(ledPin4, LOW); 
  delay (100);
  digitalWrite(ledPin4, HIGH);
  delay (100);
  digitalWrite(ledPin4, LOW); 
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay (100);
  digitalWrite(ledPin4, LOW); 
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay (100);
  digitalWrite(ledPin4, LOW); 
  delay(100);
 } 
 else {
   
   digitalWrite(ledPin4, LOW);  
    }
   }
  }
 }
}

That's because it only looks at the button once per pass through loop(). If any button is high, then it kicks off a sequence containing a bunch of 100ms delays. During that sequence it's not looking at the button: it only looks again when it gets back to the top of loop. But to get to the top of loop it has to get to the bottom first, and that's held up by the delays.

Give this code a try, and then see what you can do with it. It is specifically designed on the one hand to blink LEDs, and on the other to respond when a button is pressed, and only (once) when it is pressed.

It should be fairly easy to adapt it to your purpose.

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

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
const int button1 =  4;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
char bstate1 = 0;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// 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;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 

  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    } 
    digitalWrite(led2Pin, led2State);
  } 

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