Flash an LED until a button is pressed

Hi,

Here’s my desired behaviour in pseudo-code using two buttons and one LED:

LED = off;
ButtonOne = LOW;
ButtonTwo = LOW;

if (ButtonOne == HIGH) {

1 low -> LED flashes
Button 1 low -> LED off
Button 1 low -> LED flashes
Button 2 low -> LED on

if(LED on && buttonOne) {
    LED = off;
}

Your pseudo code is flawed. Try again

LED = off;
ButtonOne = LOW;
ButtonTwo = LOW;
ButtonOne = digitalRead(1);
if (ButtonOne == HIGH && ButtonTwo == LOW) {

flash LED;
}
else
    {
      ButtonTwo = digitalRead(2);
      if (ButtonTwo ==HIGH)
          { LED=off}
        }

To actually flash an LED until a button is pressed, if that's ALL you want to do, doesn't take much code. Just flash the LED until an interrupt fires. Then turn off the LED and loop forever in the interrupt service routine.

// untested code

// hook the button up between the ground and pin 2, and we'll use INT0 with the internal pull-up

const int LEDPin = 13;

void setup()
{
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); // Enable internal pull-up
  pinMode(LEDPin, OUTPUT);
  attachInterrupt(0, interruptServiceRoutine, LOW);
}

void loop()
{
  digitalWrite(LEDPin, HIGH);
  delay(1000);  // or whatever
  digitalWrite(LEDPin, LOW);
  delay(1000);
}

void interruptServiceRoutine()
{
  digitalWrite(LEDPin, LOW);
  while(1);  // Loop forever
}

Interrupts remain disabled so the LED should stay off.

I would use the switch case construct:

int state =1;
loop(){
switch (state){
case 1:
  blink-without-delay //see example in Playground
  check switches
  if 'turn off' switch pressed, state = 2
  if 'turn on' switch pressed, state = 3
  break;
case 2:
  turn off LED
  check switches 
  if 'blink' switch pressed,  state = 1
  if 'turn on' switch pressed, state = 3
  break;
case 3:
  turn on LED
  check switches.....etc., etc.
 }
}