blink, then stay on?

Fair warning, I'm still new at this. Anyways, I'm trying to work a design that would handle sequential taillights and doing various other functions. I'm currently using a momentary switch to simulate receiving a signal from the brake pedal. I can get the lights to sequence upon press of the switch. I want the LEDs to sequence once and then stay on until the switch is released. Unfortunately, what I am getting is the LEDs continue to loop (which I understand, because it is in the loop() portion). I tried experimenting with adding another 'void BRAKES()' function, but then the LEDs would not go back off whenever the switch was released. Any help would be appreciated!


// Sequential Taillights Practice Sketch

// Set pin numbers const int rightblinker = 12; // names pin 12 "rightblinker" const int brakes = 11; // names pin 11 "brakes" const int leftblinker = 10; // names pin 10 "leftblinker" const int R3 = 7; // names pin 7 "R3", for 3rd right light const int R2 = 6; // names pin 6 "R2", for 2nd right light const int R1 = 5; // names pin 5 "R1", for 1st right light const int L1 = 4; // names pin 4 "L1", for 1st left light const int L2 = 3; // names pin 3 "L2", for 2nd left light const int L3 = 2; // names pin 2 "L3", for 3rd left light

int buttonState = 0; // initial button state of 0

void setup() { pinMode(rightblinker, INPUT); // designates "rightblinker" an input pinMode(brakes, INPUT); // designates "brakes" an input pinMode(leftblinker, INPUT); // designates "leftblinker" an input pinMode(R3, OUTPUT); // designates R3 as an output pinMode(R2, OUTPUT); // designates R2 as an output pinMode(R1, OUTPUT); // designates R1 as an output pinMode(L1, OUTPUT); // designates L1 as an output pinMode(L2, OUTPUT); // designates L2 as an output pinMode(L3, OUTPUT); // designates L3 as an output }

void loop() { buttonState = digitalRead(brakes); // first reads brakes

if (buttonState == HIGH) // if pressed { digitalWrite(R1, HIGH); // activates center two light digitalWrite(L1, HIGH); delay(175); digitalWrite(R2, HIGH); // activates middle two lights digitalWrite(L2, HIGH); delay(175); digitalWrite(R3, HIGH); // activates outer two lights digitalWrite(L3, HIGH); delay(400); }

By the looks of things you are trying to find edges not levels. By this I mean you want something to happen as the button is pressed, and another thing to happen as it is released. Here is a quick example:

void pressed(){


void released(){


void loop(){
  static byte oldButtonState = digitalRead(pin); //reads pin only on initialisation. It then remembers the value between loops.

  byte buttonState = digitalRead(pin);
  if (buttonState != oldButtonState){
    //detected an edge... (current state and old state are different)
    if (buttonState) {
      released(); //button transitioned from low to high as current state is high.
    } else {
      pressed();  //button transitioned from high to low as current state is low.
    oldButtonState = buttonState; //remember for next time

Your code simply says if the button state is "HIGH". This is true when it first occurs, but also every time loop repeats until the button is released.

Not exactly what I want, not looking for edges or levels. I only want something to happen while the button is pressed. What it want is:

When button is HELD: LED1 comes on Delay LED2 comes on Delay LED3 comes on All 3 LEDs stay ON until the button is released.

What I get is that after the 3rd LED comes on, it loops...all LEDs turn off, and start turning on again.

In other words:

You press the button, your three LEDs come on in turn with delays. (an edge)

While the button is held (what I meant by level), do nothing further. The LEDs are already on, so you don’t need to do anything.

When the button is released, turn them off again. (an edge)

What you are describing is actions which happen on an edge.
If you don’t believe me, try the example I gave and put the code you had (6 digitalWrite() statements and 3 delays()) into the pressed() function. Add into the released() function digitalWrite() statements which turn the LEDs off.

Then press and hold the button and see what happens :slight_smile:

Like I said, very didn't really understand the edge/level part. Code worked well, except one issue. Had to switch the 'pressed' and 'released'. After that it seems to work very well. Might need to put in some debouce, as it occasionally recycles upon release. I doubt that'll be an issue in the final application though, as it'd be reading a current and not a switch from the brakes. However, wouldn't hurt to have it in there either.

Thank you very much for the help! Mind if I message you for future help with this project?