Having some issue understanding how to use the WS2812FX library pre-programmed functions. Checked kitsurfer1404's GitHub site for WS2812FX and read the README & user guide file in the library but did not find the solution to my problem.
I want to cycle through different modes with a button. I can cycle through different cases with the code below, but the patterns do not display correctly.
case 0: displays static rainbow across pixel strip
case 1: static red LED every third LED
case 2: static pair of red and green LEDs (2x red, 2x green,...)
case 3: first 3 lights turn white
case 4: group of 5 red LEDs randomly appear/disappear on strip
case 5: all LEDs static red with a few random flickers of white/pink
case 6: first LED red, all others off
case 7: all LEDs are flickering white with hints of color
case 8: first LED red followed by 2 off LEDs and rest of strip has random static LED colors
case 9: first LED red, all others off
What changes do I need to make to the code?
Can I use switch/case with WS2812FX?
/*
Cycle through different patterns using a button on Arduino Pro Mini ATMega328P (5V, 16MHz). 1000uF capacitor connected across VCC & GND of NeoPixel strip, and a 330ohm resistor in series with Data In and pin 6. Pin 2 pulled HIGH with button connected that pulls LOW when pressed.
*/
#include <WS2812FX.h>
#define LED_COUNT 26 // Number of WS2812B LEDs
#define LED_PIN 6 // Pin connected to Data In (DIN)
const byte button = 2; // Button pin to GND (LOW) when pressed
int showType = 0;
bool lastReading = HIGH; // Variable to store last button state
bool reading = HIGH; // Variable to store current button state
unsigned long patternInterval = 20; // time between steps in the pattern
unsigned long lastUpdate = 0; // for millis() when last update occurred
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode(button, INPUT_PULLUP); // Pull pin HIGH
pinMode(LED_PIN, OUTPUT); // Set DIN pin as OUTPUT
ws2812fx.init(); // Initialize WS2812FX library
ws2812fx.setBrightness(64); // Brightness 0-255 [black to blinding]
ws2812fx.setSpeed(1000); // Speed 10-5000 [fast to slow]
ws2812fx.start(); // Start WS2812FX library
}
void loop()
{
ws2812fx.service();
reading = digitalRead(button);
// Check if button pressed
if (reading == LOW && lastReading == HIGH)
{
lastUpdate = millis();
showType++ ; // Switch to next case
if (showType >= 10) // If case exceeds 9
{
showType = 0; // reset to case 0
}
delay(20); // debounce delay
}
lastReading = reading; // Update current state
if (millis() - lastUpdate > patternInterval)
{
startShow(showType);
}
}
void startShow(int showPattern)
{
switch (showPattern)
{
case 0: ws2812fx.setMode(FX_MODE_RAINBOW_CYCLE);
break;
case 1: ws2812fx.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
break;
case 2: ws2812fx.setMode(FX_MODE_MERRY_CHRISTMAS);
break;
case 3: ws2812fx.setMode(FX_MODE_CHASE_RANDOM);
break;
case 4: ws2812fx.setMode(FX_MODE_FIREWORKS);
break;
case 5: ws2812fx.setMode(FX_MODE_FLASH_SPARKLE);
break;
case 6: ws2812fx.setMode(FX_MODE_SCAN);
break;
case 7: ws2812fx.setMode(FX_MODE_MULTI_DYNAMIC);
break;
case 8: ws2812fx.setMode(FX_MODE_CHASE_BLACKOUT);
break;
case 9: ws2812fx.setMode(FX_MODE_COMET);
break;
}
}
What I remember trying so far:
- Having switch/case in loop{} = no change
- Not using millis() "if" statement = no change
- Adding anything else to case statements = any addition causes "cannot compile" message
- Using showType variable in setup = no change
- Adding default mode in setup = went straight to case 0