Looping in case statement while looking for I2C input to change case

All,

Please see the below code.

I would like to have the cases loop and also have I2C looking for new input to change the cases.

any ideas?

Code works just fine if I send a 0 (zero) case 0 does the routine once. If I send a 1 (one) then that routine runs just once.

#include <Wire.h>
#include <Adafruit_NeoPixel.h>

#define PIN 9
#define NUM_LEDS 14

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_RGB + NEO_KHZ800);

void setup()
{
Wire.begin(15); // join i2c bus with address #00OF
Wire.onReceive(receiveEvent); // register event
strip.begin();
strip.show();
}

void loop()
{
}

// Initialize the Serial interface:
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer

switch (x) {

case 0:
Serial.println("Zero");
{
// set pixel to red, delay(1000)
strip.setPixelColor(1, 128, 0, 0);
strip.setPixelColor(13, 128, 0, 0);
strip.setPixelColor(6, 255, 128, 0);
strip.setPixelColor(8, 255, 128, 0);
strip.show();
delay(1000);
// set pixel to off, delay(1000)
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(13, 0, 0, 0);
strip.setPixelColor(6, 0, 0, 0);
strip.setPixelColor(8, 0, 0, 0);
strip.show();
delay(1000);
}
break;

case 1:
Serial.println("One");
int rr = 1;
int rl = 13;
strip.setPixelColor(0, 0, 0, 255);
strip.show();
delay(100);
strip.setPixelColor(0, 0, 0, 0);
for (int count=0; count<6; count++) {
strip.setPixelColor(rr, 0, 0, 255);
strip.setPixelColor(rl, 0, 0, 255);
strip.show();
delay(100);
strip.setPixelColor(rr++, 0, 0, 0);
strip.setPixelColor(rl--, 0, 0, 0);
strip.show();
}
strip.setPixelColor(rr--, 0, 0, 255);
strip.show();
delay(200);
strip.setPixelColor(rl++, 0, 0, 0);
strip.show();
for (int count=0; count<6; count++) {
strip.setPixelColor(rr, 0, 0, 255);
strip.setPixelColor(rl, 0, 0, 255);
strip.show();
delay(100);
strip.setPixelColor(rr--, 0, 0, 0);
strip.setPixelColor(rl++, 0, 0, 0);
strip.show();
}
break;
}
}

Standard error for I2C slave:

void receiveEvent(int howMany) {
   while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer


switch (x) {

receiveEvent is called in interrupt context, so any call to a library/function that depends on interrupts to work is prohibited. Serial is such a library, delay() is such a function.

Move your code into the loop() function and just set a variable in the receiveEvent() function which you then check in loop().