I'm attempting to move my code from a button to a time interval to change the LED strip.
Currently, upon uploading the code, it waits 5 secs (which I expected) and displays the 1st case. However, it never moves on.
I'm wanting it to move thru each case every 5 secs. I'm sure I have the switch case statement placed incorrectly but I've been hitting my head against the wall now for over an hour. Any suggestions would be greatly appreciated!
#include <WS2812FX.h>
#define LED_PIN 6 // digital pin used to drive the LED strip
#define LED_COUNT 184 // number of LEDs on the strip
const int Interval = 5000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int showType = 0;
byte oldShowType = 0;
long time = 0;
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(115200);
ws2812fx.init();
ws2812fx.setBrightness(100);
//from working sketch
ws2812fx.setIdleSegment(0, 0, 161, FX_MODE_BLINK, COLORS(WHITE, RED), 200, false); // segment 0 is leds 0 - 162 flash (homerun?)
ws2812fx.setIdleSegment(1, 0, 161, FX_MODE_TRICOLOR_CHASE, COLORS(WHITE, RED, BLUE), 15000, false); // segment 1 is leds 0 - 162 (patriotic)
ws2812fx.setIdleSegment(2, 0, 161, FX_MODE_FADE, 0xFF0000, 4000, false); // segment 2 is leds 0 - 162 0xFF0000-RED (breathing)
ws2812fx.setIdleSegment(3, 162,184, FX_MODE_COLOR_WIPE, RED, 100, false); // segment 3 is leds (red-circle)
ws2812fx.strip_off();
}
//------------------------------------------------
void loop()
{
currentMillis = millis(); // capture the latest value of millis()
sequence(); // call the function that do the work
}
//-----------------------------------------------
void sequence(){
if (millis() - previousMillis >= Interval)
{
showType++;
if (showType > 4) //compares number of times the button is pushed. after # is reached, start over.
showType = 0; //which sequence do we start back at after we hit the limit on # of times button pushed.
}
if (showType != oldShowType)
{
startShow(showType); //jumps to 'void startShow(int i)'
}
oldShowType = showType;
ws2812fx.service();
}
void startShow(int i)
{
Serial.println(i);
switch (i)
{
case 0:
ws2812fx.stop();
ws2812fx.removeActiveSegment(3);
ws2812fx.removeActiveSegment(2);
ws2812fx.removeActiveSegment(1);
ws2812fx.removeActiveSegment(0);
ws2812fx.start();
break;
case 1:
ws2812fx.stop();
ws2812fx.addActiveSegment(0);
//ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 2:
ws2812fx.stop();
ws2812fx.removeActiveSegment(0);
ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 3:
ws2812fx.stop();
ws2812fx.removeActiveSegment(1);
ws2812fx.addActiveSegment(2);
ws2812fx.start();
break;
case 4:
ws2812fx.stop();
ws2812fx.removeActiveSegment(2);
ws2812fx.addActiveSegment(0);
ws2812fx.addActiveSegment(3);
ws2812fx.start();
break;
}
ws2812fx.service();
}
@jhtoolman2000, I can't see anywhere that previousMillis is updated.
@Idahowalker, I thought since there's a single statement following the if
if (showType > 4) //compares number of times the button is pushed. after # is reached,
{
showType = 0;
}
and
if (showType > 4) //compares number of times the button is pushed. after # is reached, start over.
showType = 0; //which sequence do we start back at after we hit the limit on # of times button pushed.
}
dougp: great catch! I did add in a statement of: previousMillis = millis;
I'm getting traction as now it changes (but not how it should). It appears to change quickly from 5 secs of dark (which is case 0), then to case 1 briefly. Then to case 2 (for about 5 secs) but skips case 3. Then jumps back to case 1 (bypassing case 0 all together) and stays there without moving anymore.
Did I put the wrong statement in? Sorry, still learning....
#include <WS2812FX.h>
#define LED_PIN 6 // digital pin used to drive the LED strip
#define LED_COUNT 184 // number of LEDs on the strip
const int Interval = 5000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int showType = 0;
byte oldShowType = 0;
long time = 0;
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(115200);
ws2812fx.init();
ws2812fx.setBrightness(100);
//from working sketch
ws2812fx.setIdleSegment(0, 0, 161, FX_MODE_BLINK, COLORS(WHITE, RED), 200, false); // segment 0 is leds 0 - 162 flash (homerun?)
ws2812fx.setIdleSegment(1, 0, 161, FX_MODE_TRICOLOR_CHASE, COLORS(WHITE, RED, BLUE), 15000, false); // segment 1 is leds 0 - 162 (patriotic)
ws2812fx.setIdleSegment(2, 0, 161, FX_MODE_FADE, 0xFF0000, 4000, false); // segment 2 is leds 0 - 162 0xFF0000-RED (breathing)
ws2812fx.setIdleSegment(3, 162,184, FX_MODE_COLOR_WIPE, RED, 100, false); // segment 3 is leds (red-circle)
ws2812fx.strip_off();
}
//------------------------------------------------
void loop()
{
currentMillis = millis(); // capture the latest value of millis()
sequence(); // call the function that do the work
}
//-----------------------------------------------
void sequence(){
if (millis() - previousMillis >= Interval)
{
showType++;
if (showType > 4) //compares number of times the button is pushed. after # is reached, start over.
{ showType = 0; //which sequence do we start back at after we hit the limit on # of times button pushed.
}
}
if (showType != oldShowType)
{
startShow(showType); //jumps to 'void startShow(int i)'
}
previousMillis = millis;
oldShowType = showType;
ws2812fx.service();
}
void startShow(int i)
{
Serial.println(i);
switch (i)
{
case 0:
ws2812fx.stop();
ws2812fx.removeActiveSegment(3);
ws2812fx.removeActiveSegment(2);
ws2812fx.removeActiveSegment(1);
ws2812fx.removeActiveSegment(0);
ws2812fx.start();
break;
case 1:
ws2812fx.stop();
ws2812fx.addActiveSegment(0);
//ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 2:
ws2812fx.stop();
ws2812fx.removeActiveSegment(0);
ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 3:
ws2812fx.stop();
ws2812fx.removeActiveSegment(1);
ws2812fx.addActiveSegment(2);
ws2812fx.start();
break;
case 4:
ws2812fx.stop();
ws2812fx.removeActiveSegment(2);
ws2812fx.addActiveSegment(0);
ws2812fx.addActiveSegment(3);
ws2812fx.start();
break;
}
ws2812fx.service();
}
Good question dougp...
Here was the previous sketch that was used based on a button push to move thru each case switch (which worked just fine). I hacked it to try to make it move thru each switch case after every 5 secs have passed...
#include <WS2812FX.h>
#define LED_PIN 6 // digital pin used to drive the LED strip
#define LED_COUNT 184 // number of LEDs on the strip
const int button = 2;
bool oldState = HIGH;
int showType = 0;
byte oldShowType = 0;
long time = 0;
long debounce = 50;
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode(button, INPUT_PULLUP);
Serial.begin(115200);
ws2812fx.init();
ws2812fx.setBrightness(100);
//from working sketch
ws2812fx.setIdleSegment(0, 0, 161, FX_MODE_BLINK, COLORS(WHITE, RED), 200, false); // segment 0 is leds 0 - 162 flash (homerun?)
ws2812fx.setIdleSegment(1, 0, 161, FX_MODE_TRICOLOR_CHASE, COLORS(WHITE, RED, BLUE), 15000, false); // segment 1 is leds 0 - 162 (patriotic)
ws2812fx.setIdleSegment(2, 0, 161, FX_MODE_FADE, 0xFF0000, 4000, false); // segment 2 is leds 0 - 162 0xFF0000-RED (breathing)
ws2812fx.setIdleSegment(3, 162,184, FX_MODE_COLOR_WIPE, RED, 100, false); // segment 3 is leds (red-circle)
ws2812fx.strip_off();
}
void loop()
{
// Get current button state.
bool newState = digitalRead(button);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH && millis() - time > debounce)
{
// Short delay to debounce button.
//delay(10);
// Check if button is still low after debounce.
time = millis();
newState = digitalRead(button);
if (newState == LOW)
{
showType++;
if (showType > 4) //compares number of times the button is pushed. after # is reached, start over.
showType = 0; //which sequence do we start back at after we hit the limit on # of times button pushed.
}
}
if (showType != oldShowType)
{
startShow(showType); //jumps to 'void startShow(int i)'
}
oldState = newState; // Set the last button state to the old state.
oldShowType = showType;
ws2812fx.service();
}
void startShow(int i)
{
Serial.println(i);
switch (i)
{
case 0:
ws2812fx.stop();
ws2812fx.removeActiveSegment(3);
ws2812fx.removeActiveSegment(2);
ws2812fx.removeActiveSegment(1);
ws2812fx.removeActiveSegment(0);
ws2812fx.start();
break;
case 1:
ws2812fx.stop();
ws2812fx.addActiveSegment(0);
//ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 2:
ws2812fx.stop();
ws2812fx.removeActiveSegment(0);
ws2812fx.addActiveSegment(1);
ws2812fx.start();
break;
case 3:
ws2812fx.stop();
ws2812fx.removeActiveSegment(1);
ws2812fx.addActiveSegment(2);
ws2812fx.start();
break;
case 4:
ws2812fx.stop();
ws2812fx.removeActiveSegment(2);
ws2812fx.addActiveSegment(0);
ws2812fx.addActiveSegment(3);
ws2812fx.start();
break;
}
ws2812fx.service();
}