Need some help with this sketch.
The rgb fade stops with any input from serial, but doesnt respond to any serial commands after...
Any help would be appreciated.
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
boolean rgbfade = false;
void setup() {
setColourRgb(0,0,0);
// initialize serial communication:
Serial.begin(57600);
}
void fade() {
unsigned int rgbColour[3];
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
while (rgbfade = true){
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
if (Serial.available() > 0) {
rgbfade = false;
break;
}
}
}
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case '1': // start fading rgb
rgbfade = true;
fade();
break;
case '2': // turn off fading and turn on red
setColourRgb(0, 255, 0);
rgbfade = false;
break;
default: // turn off fading and shut all colors off
setColourRgb(0, 0, 0);
rgbfade = false;
break;
}
}
}