I am using the FastLED library and the Keypad library to run code on my WS2812 lights. When I used just a basic keypad layout to print out what key was being pressed, this code worked. But now that I added methods to run code from the keypad if statements, it seems as if it is not working. Nothing happens to the lights when I press a number on the keypad. Currently the LED code is in methods. I had previously just had it in the if statements for the keypad itself. I'm going to keep the code included here as concise as possible as to not flood the screen with what I think isn't relevant. If you have any questions I can provide relevant snippets of the rest of the code.
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(32);
Serial.begin(9600);
}
void christmas() {
for(int i = 0; i < 6; i++){
for(int i = 0; i > 299; i++) {
leds[i] = CRGB (0, 128, 0); //green
FastLED.show();
leds[i+1] = CRGB (255, 0, 0); //red
FastLED.show();
delay(20);
}
//the second loop turns the first light red and the second light green starting at the end
for (int i = 299; i <= 0; i--) {
leds[i] = CRGB (255, 0, 0); //green
FastLED.show();
leds[i + 1] = CRGB (0, 128, 0); //red
FastLED.show();
delay(20);
}
}
}
void loop() {
char key = keypad.getKey(); //determines what key is pressed
if (key == '1'){
christmas();
}
if (key == '2'){
dot();
}
if (key == '3'){
mavericks();
}
if (key == '4'){
rainbow();
}
if (key == '5'){
blueBar();
}
if (key == '6'){
purple();
}
To be clear there is no error when running the code. It runs fine. But, it does nothing when the keys are pressed on the keypad, where as it should, I believe, run the methods called and effect the lights accordingly.
Edit:
After implementing Bobby Tables suggestions, it works to an extent. Except now When I press 1, which is:
void christmas() {
delay(10);
for(int j = 0; j < 6; j++){
delay(10);
for(int i = 0; i > 299; i++) {
delay(10);
leds[i] = CRGB::Red; //green
FastLED.show();
leds[i+1] = CRGB::Green; //red
FastLED.show();
delay(20);
}
//the second loop turns the first light red and the second light green starting at the end
for (int i = 299; i >= 0; i--) {
leds[i] = CRGB::Green; //green
FastLED.show();
leds[i + 1] = CRGB::Red;
FastLED.show();
delay(20);
}
}
}
it runs instantly but if I press any other key it does not fully function. Such as when I press 5 which is:
void blueBar() {
delay(10);
fill_solid(leds, NUM_LEDS, CRGB::White);
delay(10);
for(int j = 0; j < 6; j++) {
delay(10);
for(int i = 0; i < 299; i++) {
leds[i] = CRGB::Blue;
leds[i + 1] = CRGB::Blue;
leds[i + 2] = CRGB::Blue;
leds[i + 3] = CRGB::Blue;
leds[i + 4] = CRGB::Blue;
leds[i + 5] = CRGB::Blue;
FastLED.show();
delay(200);
leds[i] = CRGB::White;
FastLED.show();
}
}
}
It does nothing. Except when I press 5 and then press 1 afterwards. It will fill the initial solid white but then start running christmas().