1

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().

Kane
  • 13
  • 3
  • 1
    What if no key is pressed? – user4581301 Dec 09 '19 at 21:52
  • 1
    Unrelated: Read up on `switch`/`case`. It should help you simplify your code (unless multiple simultaneous button presses are legal). – user4581301 Dec 09 '19 at 21:54
  • It sounds like the same problem this person faced: https://arduino.stackexchange.com/questions/48736/input-4-digit-number-using-44-keypad-and-show-in-serial-monitor – Bobby Tables Dec 09 '19 at 21:57
  • When no key is pressed the lights stay off. I tried something similar in my own code to his solution. ``` while((key = keypad.getKey()) == NO_KEY) { delay(1); } while(keypad.getKey() != NO_KEY) { delay(1); } ``` Nothing still occurs. @BobbyTables @user4581301 – Kane Dec 09 '19 at 22:06
  • Could you Serial.Print(key) and see what the actual key value is when you press various keys? I think that would be of great help to track down the source of the problem. – Phillip Dec 09 '19 at 22:08
  • I'm assuming there's still no lights if you hold down the key as well? – Bobby Tables Dec 09 '19 at 22:10
  • @PhillipS Sure, I just tried it out and all the keys are printing out the correct number associated with the key being pressed. – Kane Dec 09 '19 at 22:12
  • @BobbyTables That's correct. – Kane Dec 09 '19 at 22:14
  • If you haven't got it to work correctly yet, I suggest adding in more logging code to see if it's reaching the FastLED.show() call and other pieces of the display code. Try creating a bare function which just lights up one led in simplest terms and see if you can get that to work. I'm happy to help in figuring out any other problems you may have. Unfortunately I don't have a direct & full solution. – Phillip Dec 10 '19 at 22:19

1 Answers1

0

I now believe that your if statements are working fine, but as a result of incorrect formation of your loop conditionals, christmas() does nothing. I suspect you have similar problems in your other lighting patterns.

This is using i for both loops. Also the second loop will never run since i > 299 is immediately false:

for(int i = 0; i < 6; i++){
      for(int i = 0; i > 299; i++) {

This also will never run since i is immediately greater than 0:

for (int i = 299; i <= 0; i--) {
Bobby Tables
  • 191
  • 2
  • 15
  • I'm going to edit my original post with an update, because I'm going to include new code. I'm actually an idiot for not noticing this initially, but it still does not fully function. – Kane Dec 09 '19 at 22:35