Well this is not what you said about how the switch is wired up.
Assuming you have a push button switch on pin 4 between input and ground. Then this code will turn on or off your rainbow effect when you push it.
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
uint8_t changePin = 4;
bool commuted = false;
uint8_t patternCount = 0; // initial pattern value
uint8_t patternCountLimit = 2; // how many patterns are they
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// Serial.begin(250000); // for debug printing
pinMode(changePin, INPUT_PULLUP); // for change pin wired between input and ground
strip.begin();
strip.show();
strip.setBrightness(50);
}
void loop() {
if (patternCount == 0) rainbow(10);
if (patternCount == 1) commutableDelayPin(200, changePin, LOW);
commutableCatchPin(changePin, LOW);
}
void rainbow(int wait) {
int pixelHue;
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) {
pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
if(commuted) firstPixelHue = 5*65536; // commutate outer loop
//delay(wait);
commutableDelayPin(wait, changePin, LOW);
}
}
void commutableDelayPin(uint32_t delayTime, uint8_t changePin, bool commuteState){ // this will return when the delay time has expired or the change button is pushed.
uint32_t startTime = millis(); // the millis count when we start;
// now hold until either time is up or commute pin is pressed
while((millis() - startTime < delayTime) && (commuted != true)) { // keep the loop going until time is up or delay is commutated
if ( digitalRead(changePin) == commuteState){ // if you see the button being held down abandon the delay
commuted = true;
}
}
}
void commutableCatchPin(uint8_t changePin, bool commuteState) { // check to see if the delay has been commuted and take action if it has been
if(commuted){ // we have had a commute state deal with it
commuted = false; // reset the global flag
//Serial.println("button pressed");
while(digitalRead(changePin) == commuteState) {
delay(50); // do nothing until button is released
}
//Serial.println("button released");
delay(200); // allow any button bouncing to stop
// now take action for when a delay has been commutated
// this could be to wipe an addressable LED string or to turn off the LED
if(patternCount == 0) {
for(int i = 0; i<strip.numPixels() ; i++) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
// move on the pattern to produce
patternCount += 1 ; // move onto next pattern
if(patternCount >= patternCountLimit) {
patternCount = 0; // wrap round patternCount
}
//Serial.print("count on button ");Serial.println(count);
} // end of dealing with a commute
}
This will explain what is going on.
commutable delay