How to run light sequence with button

Hello,

I'm trying to run a sequence of blinks with a button push but can only manage to get it to go while the button is pushed. I just took the example Button code and added a digitalWrite(LOW) and delay() command and repeated it a few times manually by rewriting the lines (not a for loop). I also changed the else() command to HIGH. I wanted it to run through the entire sequence of blinks with a momentary press of the button, but instead it ran through it only as long as the button is pressed. Since that was the case I simplified the code to just one HIGH/LOW cycle and it blinked as long as the button is pressed which kind of works for this application.

Originally I thought I should use the Debounce example code as a framework, but couldn't figure out how to add a sequence of digitalWrite() blinks..

IDEALLY, the LED would be off when the microcontroller powers up, and then when the button is pressed it would blink for a few cycles then stay on.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(300);
    digitalWrite(ledPin, LOW);
    delay(300);
 
    
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);

  }
}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ā€˜actual’ wiring.
Give links to components.


Avoid using delay( ) like it was the plague.

When the switch press is detected, set a Flag.

When the Flag is set, service State Machine code.

Steps (cases) in the state machine will control the LED states.

The last case will reset the Flag.

Flags sounds like a good thing to learn next, unfortunately I need to get this to work before then.

There, you are an expert on using Flags now.

bool myFlag = false;

. . .

//if we are not currently handling a LED sequence, was the switch closed ?
if (myFlag == false && buttonState == HIGH) 
{
    //enable State Machine checking
    myFlag = true;
}

//if enabled, check the State Machine
if (myFlag == true)
{
    checkStateMachine() ;
}

Always show us a good schematic of your proposed circuit.

I’m not as bright as you think. I’ll PayPal you if you can write the code I need today though. It just needs to do one thing. When powered on the led is off. When the button is pressed it flashes 6x and then stays on. It should also use delay()s for the sequence so I can tweak it or add to it later. Easy money. It’ll go in a Nano Every but that shouldn’t matter.

Why ? :thinking:

Because it’s due in the morning. This isn’t a hobby project. I’m modifying a prop that needs to be back on set soon.

If this was a hobby project many would help.

Since it is a assignment and if you are allowed to pay for someone to do your work, get the moderator to move this to the Jobs and paid Consultancy forum.

It’s upsetting they only gave you 1 day to do this, but then it’s only a 15 minute job.

Is this for a school test ?

It’s for a television show.

Will power up the PC to see if we can help.

1 Like

What LED sequence is needed (with timing) ?

It’s a ā€œfrequency finder.ā€ The device has a button that when pushed the LED is supposed to blink for a few seconds and then stay on. I’m modifying an existing prop and can’t change the button because it’s already been established.

Like, 6 blinks with a delay(300) between. That way I can tweak it later if needed.

Literally you could

  if (buttonState == HIGH) {
    // flash LED 5 times
    for (int ii = 0; ii < 5; ii++) {
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      delay(300);
    }

    digitalWrite(ledPin, HIGH);  // now on permanent 
  
    while (1);     // infinite loop hangs here until reset
  }

Hit the reset button to start all over. You could wire a reset button of your own across to the REsET pin on your board.

a7

How is the switch wired on pin 2 (two) now ?

How is the LED wired now ?

The switch is wired from 5v to pin 2 with a resistor from ground going to pin 2 as well. The LED is connected to ground and pin 13 with a resistor.

I was thinking of something like this but I can’t add any buttons for the reset. I’ll play around with this later. Thx.

Please say what is ever going to end the period of "it would blink for a few cycles then stay on" forever on-ness.

You blink then leave the light on. What ' when should the light go off or the system prepare for another execution of blink and stay on?

Reset or a power cycle would do it for the hack I posted.

If anyone just writes it and take your money,s/he's gonna ask the same question.

a7

It would be power cycled to reset.

Larry is working on it now. I will study yours for my own edification though.

Do you need the ON and OFF timing to be adjustable ?