I'm trying to make a state machine for a project I'm working on. I eventually want 2 buttons: 1 to change color and 1 to change patterns.
The sketch so far compiles perfectly. The serial monitor is there for debugging purposes and it advances thru the colors just like it should.
I've written the sketch to open with the default color red. It does.
Then advance to color green. It does.
But that's where it stops. The strip continues to stay green even though the serial monitor is showing it should be blue, orange, purple, etc. After the last color, the strip should go back to default red. And it does. It changes back to red, then to green and stays stuck at green until red is called again.
#include <Adafruit_NeoPixel.h>
#define delay(TIME_DELAY) 30000;
int cycleButton = 2; // cycle colors button pin
int buttonToggleCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
const int ledPin = 6;
const int numLeds = 74;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(74, 6);
void setup() { //this sets the output pins
pinMode(cycleButton, INPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
strip.begin();
strip.show(); // turns leds off
}
void loop() {
buttonState = digitalRead(cycleButton);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) { // if the state has changed, increment the counter
buttonToggleCounter++; // if the current state is HIGH then the button went from off to on:
}
delay(50); // Delay a little bit to avoid bouncing
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
if (buttonToggleCounter == 1) {
buttonState = 1;
}
if (buttonToggleCounter == 2) {
buttonState = 2;
}
if (buttonToggleCounter == 3) {
buttonState = 3;
}
if (buttonToggleCounter == 4) {
buttonState = 4;
}
if (buttonToggleCounter == 5) {
buttonState = 5;
}
if (buttonToggleCounter == 6) {
buttonToggleCounter = 0; //resets counter
}
if (buttonToggleCounter == 0) {
buttonState = 0; //defaults to red
}
switch (buttonState)
{
case 1:
Serial.println("case 1 - should be green");
delay(TIME_DELAY);
for(uint32_t i=0; i<numLeds; i++)
{
strip.setPixelColor(i,0x008000);
strip.show();
}
break;
case 2:
Serial.println("case 2 - should be blue");
delay(TIME_DELAY);
for(uint32_t i = 0; i > numLeds; i++)
{
strip.setPixelColor(i, 0x0000ff);
strip.show();
}
break;
case 3:
Serial.println("case 3 - should be orange");
delay(TIME_DELAY);
for(uint32_t i = 0; i > numLeds; i++)
{
strip.setPixelColor(i,0xffa500);
strip.show();
}
break;
case 4:
Serial.println("case 4 - should be purple");
delay(TIME_DELAY);
for(uint32_t i=0; i > numLeds; i++)
{
strip.setPixelColor(i,0x800080);
strip.show();
}
break;
case 5:
Serial.println("case 5 - should be yellow");
delay(TIME_DELAY);
for(uint32_t i=0; i > numLeds; i++)
{
strip.setPixelColor(i,0xffff00);
strip.show();
}
break;
default:
Serial.println("Default color is red");
delay(TIME_DELAY);
for(uint32_t i=0; i<numLeds; i++)
{
strip.setPixelColor(i,0xff0000); // default color is red
strip.show();
}
break;
}
}
I don't get it. Any help will be greatly appreciated! And in case you need to know, the led strip is: WS2812B 5050 RGB LED Strip 144 LEDs ws2812 IC Individual Addressable 5V