Looping LED patterns and using a button to break the loop

I have been trying to combine a few different basic codes to achieve a switch/case style code where each case loops (an LED pattern) and a momentary push button can break the loop and move to the next case (which will loops a different LED pattern).

Im not sure if switch/case is right for this, any advice or sample codes similar to what I'm after? My searches have all run dry

Basic concept:

check for button
run LED loop 1 (looped)
check for button
run LED loop 2 (looped)
check for button
run LED loop 3 (looped)

MatthewUser:
I have been trying to combine a few different basic codes to achieve a switch/case style code where each case loops (an LED pattern) and a momentary push button can break the loop and move to the next case (which will loops a different LED pattern).

Im not sure if switch/case is right for this, any advice or sample codes similar to what I'm after? My searches have all run dry

Basic concept:

check for button
run LED loop 1 (looped)
check for button
run LED loop 2 (looped)
check for button
run LED loop 3 (looped)

\

Your question is too vague to be answerable.

Say you have a function isButtonPressed:

bool isButtonPressed()
{
  return digitalRead(pinNumber);
}

If you're using a pull-up resistor and the pushbutton grounds the input then you'd need to invert the return statement:

bool isButtonPressed()
{
  return !digitalRead(pinNumber);
}

If you then have a global that remembers the pattern state:

typedef enum 
{
  pattern1,
  pattern2,
  pattern3
} patterns;

patterns patternState;

And your loop code might look something like this:

void loop()
{
  switch (patternState)
  {
    case pattern1:
      while (!isButtonPressed());
        //Code to flash LED in pattern 1;
    break;

     case pattern2:
      while (!isButtonPressed());
        //Code to flash LED in pattern 2;
    break;

    case pattern3:
      while (!isButtonPressed());
        //Code to flash LED in pattern 3;
   break;
 }

Since each pattern has a while loop, it needs a check inside the loop to see if the button was pressed.

Hello,

Post your code. I suspect that you are using delays for your sequences. Right?

Thanks DuncanC, I haven't tried the While function yet. I will give this a go and post an update soon

Your arduino isn't multithreaded, so you must avoid blocking code (such as while() and delay()).

uix is right.

Is much more simple if you implement this in the "BlinkWithoutDelay" fashion. If you push the button you increment the "pattern" variable and you reset the "step" (of the pattern) variable.

Do you have an example of this? My coding experience is mostly combining code

MatthewUser:
Do you have an example of this? My coding experience is mostly combining code

Blink Without Delay

I mean Blink without Delay used with a button? Im not sure how to add this

You have been asked to post some code, and you have been told that your question is too vague... You didn't post code or explained what you want to do, so no one can help you... :slight_smile:

MatthewUser:
I mean Blink without Delay used with a button? Im not sure how to add this

you were there with your Basic Concept:

check for button
run LED loop 1 (looped)
check for button
run LED loop 2 (looped)
check for button
run LED loop 3 (looped)

but we will do this:

constantly check for button press
if button was pressed
advance to the next led flashing pattern

you should try to learn the BlinkWithoutDelay and PushButton sketches that came with the arduino IDE.

try something like this which you can also change state with a press of a button or by typing a character into the serial monitor.

int buttonPin = 3;
int ledPin = 13;
int state = 0;
int oldState;
unsigned long startTime;
unsigned long interval = 1000UL;
//
void setup() 
{
  Serial.begin(9600);
}
//
void loop () 
{
  if (Serial.available())
  {
    int dump = Serial.read();
    state++;
    if (state > 4) state = 0;
  }
  int buttonState = digitalRead(buttonPin);
  {
    if (buttonState == HIGH)
    {
      if (oldState == LOW)
      {
        state++;
        Serial.println(state);
        if (state > 4) state = 0;
      }
    }
  }
  oldState = buttonState;
  if (state == 0)
  {
    noFlash();
    startTime = millis();
  }
  else if (state == 1)
  {
    flashA();
  }
  else if (state == 2)
  {
    flashB();
  }
  else if (state == 3)
  {
    flashC();
  }
  else if (state == 4)
  {
    flashD();
  }
}
//
void noFlash()
{
  digitalWrite(ledPin, LOW);
}
//
void flashA() //slow flash
{
  if (millis() - startTime >= interval)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}
//
void flashB() //med flash
{
  if (millis() - startTime >= interval/4UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}
//
void flashC() //fast flash
{
  if (millis() - startTime >= interval/20UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}

void flashD() //solid
{
  digitalWrite(ledPin, HIGH);
  startTime = millis();
}

I tried the code from post #10 and changed the LED pin to 52 on my mega but for some reason the LED is dim now. Anyone know why?