Good evening all,
I'm having an issue with a quick "test" project that I am tinkering with (learning the "do's" and "don'ts") the NeoPixel LED strip. I've been piecing together excerpts from other folks and so far everything is great.
I'm doing it over serial sending switch/case with numbers from the keyboard for different options. The issue I am currently having is "case 1:", when I hit the "1" key and it fires off, I want to be able to interrupt it immediately by using key "0". So basically, I'd like it to loop forever after hitting key "1" but terminate after hitting "0".
However, when hitting key "1", then key "0" immediately, the while loop repeats twice, then breaks. I haven't done any C/C++ since I was 12 and now I'm 38 so now it is all trial and error based so please pardon my syntax. Any help would be appreciated.
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define PIN 6
#define LEDs 16
int LED_Fade_Interval = 5;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDs, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
strip.begin();
strip.show();
}
void loop() {
while (Serial.available() == 0);
int numKey = Serial.read() - '0';
uint32_t low = strip.Color(0, 0, 0);
switch(numKey)
{
case 1:
Serial.println("LED Strip is ON - BLUE FADE");
while (numKey != 0)
{
int numKey = Serial.read() - '0';
delay(50);
ws2812_FadeInOut(0, 0, 255, LED_Fade_Interval);
if (numKey == 0)
{
for( int i = 0; i<LEDs; i++){
strip.setPixelColor(i, low);
strip.show();
}
break;
}
}
case 0:
Serial.println("LED Strip is OFF");
for( int i = 0; i<LEDs; i++){
strip.setPixelColor(i, low);
strip.show();
}
break;
}
}
// *******************************************
// FADER Function
// *******************************************
void ws2812_FadeInOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
for(uint8_t b = 0; b <255; b++) {
for(uint8_t i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
}
strip.show();
delay(wait);
};
for(uint8_t b=255; b > 0; b--) {
for(uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
}
strip.show();
delay(wait);
};
};