I can't find a solution

Hi everyone, this is my first post so i'll try to adhere to the rules best that i can.

I'm designing a lamp that has two functions, using a 24 led neopixel.

one button runs a plain white light to use for illumination, the other runs a coloured light display.

The problem that i'm having is switching between functions. You see, if i decide to run the colour display, I have to wait till the end till I can run the white light again.

I want to make interchangeable functions that are user friendly and I feel like i've tried everything to make it work.

any useful advice would be greatly appreciated.

please see my code below;

#include <Adafruit_NeoPixel.h>

    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.


#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 24

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 1;
const int PARTY_PIN = 3;
const int BUTTON_PIN = 2;


void setup() {
  
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(PARTY_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 

}

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);
  bool buttonState = digitalRead(PARTY_PIN);

  

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(30);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 1)
        showType=0;
      startShow(showType);
    }
  }

 if (buttonState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(30);
    // Check if button is still low after debounce.
    buttonState = digitalRead(PARTY_PIN);
    if (buttonState == LOW) {
      startShow(showType=2);
    }
  }
  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
    
    case 0: colorWipe(strip.Color(20,20,20), 75);  // white
            delay(1000);
            for(int j=20;j<185;j*=1.15){
              colorWipe(strip.Color(j,j,j),0);  // white
            }
            break;
    
    case 1: colorWipe(strip.Color(0, 0, 0), 10);    // Black/off
    break;

    case 2: do {
            colorWipe(strip.Color(255, 0, 0), 50); // Red
            colorWipe(strip.Color(0, 255, 0), 50); // Green  
            colorWipe(strip.Color(0, 0, 255), 50); // Blue
            theaterChase(strip.Color(127, 127, 127), 50); // White
            theaterChase(strip.Color(127, 0, 0), 50); // Red
            theaterChase(strip.Color(0, 0, 127), 50); // Blue
            rainbow(20);
            rainbowCycle(20);
            theaterChaseRainbow(50);            
    }
    while(1);                              
  }
  
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Arduino has a yield() function, which normally does nothing. It's called repeatedly inside the delay() function.

You can write your own yeild() function, which when compiled will replace the default do-nothing function. What you would do is put some code in there to read the button.

boolean early_exit;

void rainbow(uint8_t wait) {
  uint16_t i, j;

  early_exit = false;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
    if(early_exit) return;
  }
}


void yield() {
  // add sme code here to check button state
  if(digitalRead(BUTTON_PIN) != oldState) {
    early_exit = true;
  }
}

I'm ignoring debounce code inside yeild(). The effect will be that when the button is pressed and bounces, the rainbow() will get restarted multiple times. But meh - so what if it does?

The bigger solution is to recode all the things to use millis(). A stopgap would be to override the delay function itself so that it uses millis() and also respects the early_exit flag.

thank you kindly for your prompt and in-depth response.
you've given me a lot to think about and learn, i'll read this over and try to integrate it with my existing code.
i'm going to need a bit of luck with this.

It is very rare to use yield() in the Arduino like this. One of the major drawbacks is that you are still stuck in the delay. An hour-long process might use delay(1000ul * 60 * 60) and the yield() may detect the emergency stop button was pressed but it can't abort the delay.

Learn to convert your animations into a state machine process and run with no delays at all.

What you could do is something similar as in reply#1, where you create a function to monitor button presses which returns a conditional return so you fill your code with line like

if (buttonPress()) return;

and create a function which looks something like this

bool buttonPress() {
bool hasBeenPressed=false;
(-- whatever code you use for determining the presses, the stuff you now have in loop() essentially --)
return hasBeenPressed;

which will work as long as you do not have more levels of functions calls. Or you could create a State-machine or even better instead of using a loop (or state-machine) calculate what part of the pattern you are supposed to be in from 'elapsed-time' and then from loop() you will just constantly be monitoring the keypresses and calling any function that should be displaying the new ledstates .

MorganS:
Learn to convert your animations into a state machine process and run with no delays at all.

Ideally, yes. The color wipe is sample code provided by adafruit, and converting it over is probably beyond the abilities of someone who would be using it in the first place. If I was doing this, Id start from scratch rather than trying to use the adafruit code. Using yield isn't an ideal solution, sure, but it will get most of the way there.