You rang ?
I can't help with the strip.show() function as I have no experience of the hardware.
As to the need to wait until the sequence is finished you need to get rid of the for loop and use a technique other than delay(). Here is a program that I wrote to control LEDs on a quad.
#define ON HIGH
#define OFF LOW
#define DOUBLE_FLASH 0
#define ALL_OFF 1
#define LEFT_RIGHT 2
#define IN_OUT 3
#define ALL_ON 4
#define RANDOM 5
#define LEDS_ON_1 0
#define LEDS_OFF_1 1
#define LEDS_ON_2 2
#define LEDS_OFF_2 3
#define WAIT_1 1
byte receiverPin = 11;
unsigned long duration;
byte inputState = LOW;
byte prevInputState = LOW;
int pattern = 1;
unsigned long rearLedStateInterval;
unsigned long frontLedStateInterval;
int frontLedState = ON;
byte rearLedPins[] = {
A0, A1, A2, A3};
byte frontLedPins[] = {
8, 9};
void setup()
{
Serial.begin(115200);
for (int pin = 0; pin < 4; pin++)
{
pinMode(rearLedPins[pin], OUTPUT);
}
for (int pin = 0; pin < 2; pin++)
{
pinMode(frontLedPins[pin], OUTPUT);
}
delay(1000);
}
void loop()
{
duration = pulseIn(receiverPin, HIGH);
duration = map(duration, 1080, 1870, 0, 100);
if (duration > 50)
{
inputState = LOW;
}
else
{
inputState = HIGH;
}
if (inputState != prevInputState)
{
rearAll(OFF);
pattern++;
if (pattern > RANDOM)
{
pattern = DOUBLE_FLASH;
}
}
prevInputState = inputState;
switch (pattern)
{
case DOUBLE_FLASH:
{
static byte state = LEDS_ON_1;
switch(state)
{
case LEDS_ON_1: //all ON wait
rearAll(ON);
static unsigned long stateStart = millis();
rearLedStateInterval = 100;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_OFF_1;
stateStart = millis();
}
break; //end of LEDS_ON_1 state
case LEDS_OFF_1:
rearAll(OFF);
rearLedStateInterval = 200;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_2;
stateStart = millis();
}
break; //end of LEDS_OFF_1 state
case LEDS_ON_2:
rearAll(ON);
rearLedStateInterval = 100;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_OFF_2;
stateStart = millis();
}
break; //end of LEDS_ON_2 state
case LEDS_OFF_2: //all OFF wait longer after second flash
rearAll(OFF);
rearLedStateInterval = 500;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_1;
stateStart = millis();
}
break; //end of LEDS_OFF_2 state
}
break; //end of DOUBLE_FLASH
}
case ALL_OFF:
{
rearAll(OFF);
break;
}
case LEFT_RIGHT:
{
static byte state = LEDS_ON_1;
switch(state)
{
case LEDS_ON_1:
left(ON);
right(OFF);
static unsigned long stateStart = millis();
rearLedStateInterval = 300;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_2;
stateStart = millis();
}
break; //end of LEDS_ON_1 state
case LEDS_ON_2:
left(OFF);
right(ON);
rearLedStateInterval = 300;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_1;
stateStart = millis();
}
break; //end of LEDS_ON_2 state
}
break; //end of LEFT/RIGHT states
}
case IN_OUT:
{
static byte rearLedState = LEDS_ON_1;
switch(rearLedState)
{
case LEDS_ON_1:
outers(ON);
inners(OFF);
static unsigned long rearLedStateStart = millis();
rearLedStateInterval = 300;
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_2;
rearLedStateStart = millis();
}
break; //end of LEDS_ON_1 rearLedState
case LEDS_ON_2:
outers(OFF);
inners(ON);
rearLedStateInterval = 300;
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_1;
rearLedStateStart = millis();
}
break; //end of LEDS_ON_2 rearLedState
} //end of IN/OUT rearLedStates
break; //end of IN/OUT case
}
case ALL_ON:
{
rearAll(ON);
break;
}
case RANDOM:
{
static byte rearLedState = LEDS_ON_1;
switch(rearLedState)
{
case LEDS_ON_1:
digitalWrite(rearLedPins[random(0, 4)], random(0, 2));
static unsigned long rearLedStateStart = millis();
rearLedState = WAIT_1;
rearLedStateInterval = 50;
break;
case WAIT_1:
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_1;
rearLedStateStart = millis();
}
break;
} //end of RANDOM rearLedStates
break; //end of RANDOM case
}
} //end of rear leds patterns switch
switch (frontLedState)
{
case ON:
front(ON);
frontLedStateInterval = 80;
static unsigned long frontLedStateStart = millis();
if (millis() - frontLedStateStart >= frontLedStateInterval)
{
frontLedState = OFF;
frontLedStateStart = millis();
}
break; //end of front leds on
case OFF:
front(OFF);
frontLedStateInterval = 500;
if (millis() - frontLedStateStart >= frontLedStateInterval)
{
frontLedState = ON;
frontLedStateStart = millis();
}
break; //end of front leds off
} //end of front leds switch
} //end of loop()
void rearAll(byte ledState)
{
outers(ledState);
inners(ledState);
}
void left(byte ledState)
{
digitalWrite(rearLedPins[2], ledState);
digitalWrite(rearLedPins[3], ledState);
}
void right(byte ledState)
{
digitalWrite(rearLedPins[0], ledState);
digitalWrite(rearLedPins[1], ledState);
}
void inners(byte ledState)
{
digitalWrite(rearLedPins[1], ledState);
digitalWrite(rearLedPins[2], ledState);
}
void outers(byte ledState)
{
digitalWrite(rearLedPins[0], ledState);
digitalWrite(rearLedPins[3], ledState);
}
void front(byte ledState)
{
digitalWrite(frontLedPins[0], ledState);
digitalWrite(frontLedPins[1], ledState);
}
At first sight it will look complicated but take this section and study it. It is actually more complicated than it need be because of the nested switch/cases but that was how I did it at the time to allow the mark/space ratio of the flashes to be uneven.
case DOUBLE_FLASH:
{
static byte state = LEDS_ON_1;
switch(state)
{
case LEDS_ON_1: //all ON wait
rearAll(ON);
static unsigned long stateStart = millis();
rearLedStateInterval = 100;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_OFF_1;
stateStart = millis();
}
break; //end of LEDS_ON_1 state
case LEDS_OFF_1:
rearAll(OFF);
rearLedStateInterval = 200;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_2;
stateStart = millis();
}
break; //end of LEDS_OFF_1 state
It uses millis() for timing and switch/case so that only the code for the current state is executed each time through loop(). Because of that this section of code is executes each time through loop()
duration = pulseIn(receiverPin, HIGH);
duration = map(duration, 1080, 1870, 0, 100);
if (duration > 50)
{
inputState = LOW;
}
else
{
inputState = HIGH;
}
if (inputState != prevInputState)
{
rearAll(OFF);
pattern++;
if (pattern > RANDOM)
{
pattern = DOUBLE_FLASH;
}
}
prevInputState = inputState;
so it can respond to an input without waiting for the current sequence to finish. Replace the for loops in your rainbowCycle() function with counters that are incremented when the timing period ends (millis() - startTime >= requiredPeriod)
One thing I noted is that you are relying on exact values from pulseIn() to activate sequences. This will not be very reliable. I tested the value by using < and > and even mapped the raw small variation on change of switch position to a wider one to make it easier to spot the change.