WS2811 Button counter

My end goal is to create a countdown timer using a WS2811 led strip. For now I am trying to program for setting the desired time. What I have works, kind of. With a press of a button I am incrementing the “H” counter using H++, then sending the command to the LED strip to also increment. This works fine until I press the button 8 times. Using the LCD screen to see what the value of H is it counts …6, 7, 8, after 8 it then jumps to 255. If I remove the two lines to the LED strip the LCD screen counts up to 15 (which is my set limit) I can’t figure out why it suddenly jumps to 255 after 8 button presses. It does this with and without the WS2811 connected. Any ideas?

Arduino Uno

DFRobot LCD Keypad Shield

1 meter WS2811 LED strip


#include "FastLED.h"

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

int keypad_pin = A0;
int keypad_value = 0;
char btn_push;
 int H = 0; // count hours

#define DATA_PIN    11

#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    10
CRGB leds[NUM_LEDS];

#define BRIGHTNESS          10
 
void setup()
{
    lcd.begin(16,2);  //Initialize a 2x16 type LCD
    lcd.clear();

    FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
    //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

    // set master brightness control
     FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{ 
  btn_push = ReadKeypad();                //reads button status
  WaitBtnRelease();                       //waits for the button to be released  
    if(btn_push == 'U' && H<15)           
      {
      H++;                              //increment counter by one. Eighth button press causes "H" to go to -256
      //H+=2;                              //increment counter by two. Fifth button press causes "H" to go to 255
         leds[H].setRGB(255,0,0);
         leds[H+1].setRGB(255,0,0);
         FastLED.show();
      }
  delay(100);
  lcd.setCursor(5,0);
  lcd.print(H);                         //for debuging, prints value of "H" on LCD screen
  leds[0].setRGB(255,0,0);              //sets first LED to red. Clears other leds when system is reset
  FastLED.show();
}
char ReadKeypad() //scans for button presses
{    
  /* Keypad button analog Value
  no button pressed 1023
  select  741
  left    503
  down    326
  up      142
  right   0 
  */
  keypad_value = analogRead(keypad_pin);
  
  if(keypad_value < 50)
    return 'R';
  else if(keypad_value < 150)
    return 'U';
  else if(keypad_value < 300)
    return 'D';
  else if(keypad_value < 500)
    return 'L';
  else if(keypad_value < 800)
    return 'S';
  else 
    return 'N';
}
void WaitBtnRelease()
{
    while(analogRead(keypad_pin) < 800) { }
}

you're going out of bounds on your leds array..
10 leds highest array element is 9..
memory is being corrupted that is what you're noticing..
sorry.. ~q

You already have the constant NUMLEDS to set array sizes and bounds, and make it automatically the same throughout the program.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.