HELP!- Repeat events inside case structure

Hey Guys,
I’m trying to make a program to run my digital LED’s under my desk and I want them to change colors every time I press a button. I have that part down. Now I want to add animation. I have the color programming down, I just need to know how to repeat a line of code inside a case structure. For instance I want it to switch between red and blue indefinitely while inside case structure #4.
Thx for your help,
Josh

P.S. Here’s my code.

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2
#define PIXEL_PIN    6 

#define PIXEL_COUNT 27
#define BLACK 0,0,0
#define RED 255,0,0
#define GREEN 0,255,0
#define BLUE 0,0,255
#define YELLOW 255,255,0
#define PURPLE 255,0,255

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show();
}

void loop() {
  bool newState = digitalRead(BUTTON_PIN);

  if (newState == LOW && oldState == HIGH) {
    delay(20);
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 3)
        showType=0;
      startShow(showType);
    }
  }
  oldState = newState;
}

void startShow(int i) {
  switch(i){
    case 0: 
strip.setPixelColor(0,BLACK);
strip.setPixelColor(1,BLACK);
strip.setPixelColor(2,BLACK);
strip.setPixelColor(3,BLACK);
strip.setPixelColor(4,BLACK);
strip.setPixelColor(5,BLACK);
strip.setPixelColor(6,BLACK);
strip.setPixelColor(6,BLACK);
strip.setPixelColor(7,BLACK);
strip.setPixelColor(8,BLACK);
strip.setPixelColor(9,BLACK);
strip.setPixelColor(10,BLACK);
strip.setPixelColor(12,BLACK);
strip.setPixelColor(13,BLACK);
strip.setPixelColor(14,BLACK);
strip.setPixelColor(16,BLACK);
strip.setPixelColor(17,BLACK);
strip.setPixelColor(18,BLACK);
strip.setPixelColor(19,BLACK);
strip.setPixelColor(20,BLACK);
strip.setPixelColor(21,BLACK);
strip.setPixelColor(22,BLACK);
strip.setPixelColor(24,BLACK);
strip.setPixelColor(25,BLACK);
strip.setPixelColor(26,BLACK);
strip.show();// Black/off
            break;
    case 1: 
strip.setPixelColor(0,RED);
strip.setPixelColor(1,RED);
strip.setPixelColor(2,RED);
strip.setPixelColor(3,RED);
strip.setPixelColor(4,RED);
strip.setPixelColor(5,RED);
strip.setPixelColor(6,RED);
strip.setPixelColor(7,RED);
strip.setPixelColor(8,RED);
strip.setPixelColor(9,RED);
strip.setPixelColor(10,RED);
strip.setPixelColor(12,RED);
strip.setPixelColor(13,RED);
strip.setPixelColor(14,RED);
strip.setPixelColor(16,RED);
strip.setPixelColor(17,RED);
strip.setPixelColor(18,BLUE);
strip.setPixelColor(19,BLUE);
strip.setPixelColor(20,BLUE);
strip.setPixelColor(21,BLUE);
strip.setPixelColor(22,RED);
strip.setPixelColor(24,RED);
strip.setPixelColor(25,RED);
strip.setPixelColor(26,RED);
    strip.show();// Red/Blue
            break;
    case 2: 
strip.setPixelColor(0,GREEN);
strip.setPixelColor(1,GREEN);
strip.setPixelColor(2,GREEN);
strip.setPixelColor(3,GREEN);
strip.setPixelColor(4,GREEN);
strip.setPixelColor(5,GREEN);
strip.setPixelColor(6,GREEN);
strip.setPixelColor(6,GREEN);
strip.setPixelColor(7,GREEN);
strip.setPixelColor(8,GREEN);
strip.setPixelColor(9,GREEN);
strip.setPixelColor(10,GREEN);
strip.setPixelColor(12,GREEN);
strip.setPixelColor(13,GREEN);
strip.setPixelColor(14,GREEN);
strip.setPixelColor(16,GREEN);
strip.setPixelColor(17,GREEN);
strip.setPixelColor(18,YELLOW);
strip.setPixelColor(19,YELLOW);
strip.setPixelColor(20,YELLOW);
strip.setPixelColor(21,YELLOW);
strip.setPixelColor(22,GREEN);
strip.setPixelColor(24,GREEN);
strip.setPixelColor(25,GREEN);
strip.setPixelColor(26,GREEN);
    strip.show();// Green/Yellow
            break;
    case 3: 
strip.setPixelColor(0,BLUE);
strip.setPixelColor(1,BLUE);
strip.setPixelColor(2,BLUE);
strip.setPixelColor(3,BLUE);
strip.setPixelColor(4,BLUE);
strip.setPixelColor(5,BLUE);
strip.setPixelColor(6,BLUE);
strip.setPixelColor(6,BLUE);
strip.setPixelColor(7,BLUE);
strip.setPixelColor(8,BLUE);
strip.setPixelColor(9,BLUE);
strip.setPixelColor(10,BLUE);
strip.setPixelColor(12,BLUE);
strip.setPixelColor(13,BLUE);
strip.setPixelColor(14,BLUE);
strip.setPixelColor(16,BLUE);
strip.setPixelColor(17,BLUE);
strip.setPixelColor(18,PURPLE);
strip.setPixelColor(19,PURPLE);
strip.setPixelColor(20,PURPLE);
strip.setPixelColor(21,PURPLE);
strip.setPixelColor(22,BLUE);
strip.setPixelColor(24,BLUE);
strip.setPixelColor(25,BLUE);
strip.setPixelColor(26,BLUE);
    strip.show();// Blue/Purple
            break;

  }
}

Button_Cycle_color2.0.ino (4.98 KB)

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

The naive way to do this would be something like:

   case 4:
      while (true) {
        strip.setPixelColor(0, RED);
        //set all other pixels to RED
        strip.show();
        delay(1000);
        strip.setPixelColor(0, BLUE);
        //set all other pixels to BLUE
        strip.show();
        delay(1000);
      }
      break;

But the problem is that will cause you to just permanently get stuck on that animation because you just get stuck inside of that while loop and the button is no longer being polled. The solution is to expand on the technique demonstrated at File > Examples > 02.Digital > BlinkWithoutDelay:

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2
#define PIXEL_PIN    6

#define PIXEL_COUNT 27
#define BLACK 0,0,0
#define RED 255,0,0
#define GREEN 0,255,0
#define BLUE 0,0,255
#define YELLOW 255,255,0
#define PURPLE 255,0,255

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;
unsigned long animationUpdateTimestamp;
unsigned int animationUpdateInterval;
byte animationStep;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show();
}

void loop() {
  bool newState = digitalRead(BUTTON_PIN);

  if (newState == LOW && oldState == HIGH) {
    delay(20);
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 3)
        showType = 0;
      startShow(showType);
    }
  }
  oldState = newState;

  //update animation
  if (millis() - animationUpdateTimestamp >= animationUpdateInterval) {  //it's time to update the animation
    animationUpdateTimestamp += animationUpdateInterval; //update timestamp
    switch (showType) {
      case 4:
        switch (animationStep) {
          case 0:
            strip.setPixelColor(0, RED);
            //set other pixels as desired
            strip.show();
            animationStep++;
            break;
          case 1:
            strip.setPixelColor(0, BLUE);
            //set other pixels as desired
            strip.show();
            animationStep = 0;
            break;
        }
        break;
    }
  }
}

void startShow(int i) {
  switch (i) {
    case 0:
      strip.setPixelColor(0, BLACK);
      strip.setPixelColor(1, BLACK);
      strip.setPixelColor(2, BLACK);
      strip.setPixelColor(3, BLACK);
      strip.setPixelColor(4, BLACK);
      strip.setPixelColor(5, BLACK);
      strip.setPixelColor(6, BLACK);
      strip.setPixelColor(6, BLACK);
      strip.setPixelColor(7, BLACK);
      strip.setPixelColor(8, BLACK);
      strip.setPixelColor(9, BLACK);
      strip.setPixelColor(10, BLACK);
      strip.setPixelColor(12, BLACK);
      strip.setPixelColor(13, BLACK);
      strip.setPixelColor(14, BLACK);
      strip.setPixelColor(16, BLACK);
      strip.setPixelColor(17, BLACK);
      strip.setPixelColor(18, BLACK);
      strip.setPixelColor(19, BLACK);
      strip.setPixelColor(20, BLACK);
      strip.setPixelColor(21, BLACK);
      strip.setPixelColor(22, BLACK);
      strip.setPixelColor(24, BLACK);
      strip.setPixelColor(25, BLACK);
      strip.setPixelColor(26, BLACK);
      strip.show();// Black/off
      break;
    case 1:
      strip.setPixelColor(0, RED);
      strip.setPixelColor(1, RED);
      strip.setPixelColor(2, RED);
      strip.setPixelColor(3, RED);
      strip.setPixelColor(4, RED);
      strip.setPixelColor(5, RED);
      strip.setPixelColor(6, RED);
      strip.setPixelColor(7, RED);
      strip.setPixelColor(8, RED);
      strip.setPixelColor(9, RED);
      strip.setPixelColor(10, RED);
      strip.setPixelColor(12, RED);
      strip.setPixelColor(13, RED);
      strip.setPixelColor(14, RED);
      strip.setPixelColor(16, RED);
      strip.setPixelColor(17, RED);
      strip.setPixelColor(18, BLUE);
      strip.setPixelColor(19, BLUE);
      strip.setPixelColor(20, BLUE);
      strip.setPixelColor(21, BLUE);
      strip.setPixelColor(22, RED);
      strip.setPixelColor(24, RED);
      strip.setPixelColor(25, RED);
      strip.setPixelColor(26, RED);
      strip.show();// Red/Blue
      break;
    case 2:
      strip.setPixelColor(0, GREEN);
      strip.setPixelColor(1, GREEN);
      strip.setPixelColor(2, GREEN);
      strip.setPixelColor(3, GREEN);
      strip.setPixelColor(4, GREEN);
      strip.setPixelColor(5, GREEN);
      strip.setPixelColor(6, GREEN);
      strip.setPixelColor(6, GREEN);
      strip.setPixelColor(7, GREEN);
      strip.setPixelColor(8, GREEN);
      strip.setPixelColor(9, GREEN);
      strip.setPixelColor(10, GREEN);
      strip.setPixelColor(12, GREEN);
      strip.setPixelColor(13, GREEN);
      strip.setPixelColor(14, GREEN);
      strip.setPixelColor(16, GREEN);
      strip.setPixelColor(17, GREEN);
      strip.setPixelColor(18, YELLOW);
      strip.setPixelColor(19, YELLOW);
      strip.setPixelColor(20, YELLOW);
      strip.setPixelColor(21, YELLOW);
      strip.setPixelColor(22, GREEN);
      strip.setPixelColor(24, GREEN);
      strip.setPixelColor(25, GREEN);
      strip.setPixelColor(26, GREEN);
      strip.show();// Green/Yellow
      break;
    case 3:
      strip.setPixelColor(0, BLUE);
      strip.setPixelColor(1, BLUE);
      strip.setPixelColor(2, BLUE);
      strip.setPixelColor(3, BLUE);
      strip.setPixelColor(4, BLUE);
      strip.setPixelColor(5, BLUE);
      strip.setPixelColor(6, BLUE);
      strip.setPixelColor(6, BLUE);
      strip.setPixelColor(7, BLUE);
      strip.setPixelColor(8, BLUE);
      strip.setPixelColor(9, BLUE);
      strip.setPixelColor(10, BLUE);
      strip.setPixelColor(12, BLUE);
      strip.setPixelColor(13, BLUE);
      strip.setPixelColor(14, BLUE);
      strip.setPixelColor(16, BLUE);
      strip.setPixelColor(17, BLUE);
      strip.setPixelColor(18, PURPLE);
      strip.setPixelColor(19, PURPLE);
      strip.setPixelColor(20, PURPLE);
      strip.setPixelColor(21, PURPLE);
      strip.setPixelColor(22, BLUE);
      strip.setPixelColor(24, BLUE);
      strip.setPixelColor(25, BLUE);
      strip.setPixelColor(26, BLUE);
      strip.show();// Blue/Purple
      break;
    case 4:
      animationUpdateInterval = 1000;
      animationStep = 0;
      break;
  }
}

Of course there are various ways you could implement this, some might be more efficient depending on how many other animations you add but this should give you a general idea of how to do this in a non-blocking manner so that the button can be continuously polled. You may want to move the animation update code to a separate function to keep your loop from getting too crowded.

WOW!!! YOU WERE SOOOO HELPFUL!!!!! I THANK YOU SO MUCH!