Hi. I have several effects on a Neopixel Strip. I want to trigger them with a push button.
First press -effect 1, Second press -effect 2 ..and so on. easy.
I have set them on a "switch case".
The push counter uses millis(). so do not stop the code. The simple code to control de pixels strip is this:
yeah , does not do nothing.... but I dont care about it. The code works if I delete the delay() on the switch cases. And that is the real problem. Without the delay there is no effect on the strip.
Yeah I can improve the first part of the code. I have test it with switch cases and serial.println
1-Fade between 4 colors. Not going to black.
2- Moving pixel through fixed background. like the code you sent me, but keeping the no moving
pixels with a color.
3- Moving Rainbow.
Many thanks in advance. and many other thanks if you accept this mission.
This might be what you want but you really need to explain things fully to get yourself understood.
// Version YY/MM/DD Description
// 1.00 21/09/08 Running sketch
// 1.01 21/09/09 Added state 4, magenta LED goes back and forth
// 1.02 21/09/09 Made name changes to different variables, added comments
#include <Adafruit_NeoPixel.h>
#define CLOSED LOW
#define numberOfPixels 14
Adafruit_NeoPixel tira = Adafruit_NeoPixel(numberOfPixels, 2, NEO_GRB + NEO_KHZ800);
bool flag = false; //'false' means the TIMER is disabled
//a 'char' has a range of +-128
char incDec = 1; //(incement/decrement) this will be: 1 for up, -1 for down
char pixelNumer = 0;
const byte heartbeatLED = 13; //toggles ever 500ms
const byte buttonPin = 4;
const byte maximumState = 4; //there are 4 states in the 'State Machine'
byte buttonState = 0;
byte lastButtonState = 0;
byte mState = 0; //the current state the machine is in
//timing stuff
unsigned long commonMillis;
unsigned long switchMillis;
unsigned long heartbeatMillis;
//*********************************************************************
void setup()
{
Serial.begin(9600);
tira.begin();
tira.show();
tira.setBrightness(50);
pinMode(heartbeatLED, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
//*********************************************************************
void loop()
{
//**********************************************
//used to check for blocking code
if (millis() - heartbeatMillis >= 500)
{
//restart the TIMER
heartbeatMillis = millis();
//toggle the LED state
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//**********************************************
//is it time to check the switches ?
if (millis() - switchMillis >= 50)
{
//restart the TIMER
switchMillis = millis();
checkSwitches();
}
//**********************************************
//State Machine
switch (mState)
{
//*******************
case 0:
//do nothing
break;
//*******************
case 1:
if (flag == true && millis() - commonMillis >= 500)
{
//restart the TIMER
commonMillis = millis();
//green
tira.setPixelColor(pixelNumer, 255, 0, 0);
tira.show();
//next LED
pixelNumer++;
//are we finished ?
if (pixelNumer > numberOfPixels - 1)
{
//we are finished now
flag = false;
}
}
break;
//*******************
case 2:
if (flag == true && millis() - commonMillis >= 500)
{
//restart the TIMER
commonMillis = millis();
//green
tira.setPixelColor(pixelNumer, 0, 255, 0);
tira.show();
//next LED
pixelNumer++;
//are we finished ?
if (pixelNumer > numberOfPixels - 1)
{
//we are finished now
flag = false;
}
}
break;
//*******************
case 3:
if (flag == true && millis() - commonMillis >= 500)
{
//restart the TIMER
commonMillis = millis();
//blue
tira.setPixelColor(pixelNumer, 0, 0, 255);
tira.show();
//next LED
pixelNumer++;
//are we finished ?
if (pixelNumer > numberOfPixels - 1)
{
//we are finished now
flag = false;
}
}
break;
//*******************
case 4:
if (flag == true && millis() - commonMillis >= 100)
{
//restart the TIMER
commonMillis = millis();
//*************************
//if we going up, we need to turn off the previous pixel
if (pixelNumer != 0 && incDec == 1)
{
//turn off the preceeding pixel
tira.setPixelColor(pixelNumer - 1, 0, 0, 0);
tira.show();
}
//*************************
//if we going down, we need to turn off the previous pixel
if (incDec == -1)
{
//turn off the preceeding pixel
tira.setPixelColor(pixelNumer + 1, 0, 0, 0);
tira.show();
}
//magenta
tira.setPixelColor(pixelNumer, 255, 0, 255);
tira.show();
//next pixel
pixelNumer = pixelNumer + incDec;
//*************************
//have we reached the top most pixel ?
if (pixelNumer > numberOfPixels - 1)
{
//readjust to the next pixel
pixelNumer = numberOfPixels - 2;
//we will now be going down
//toggle increment/decrement
incDec = -1;
}
//*************************
//have we reached the bottom most pixel ?
if (pixelNumer < 0)
{
//readjust to the next pixel
pixelNumer = 1;
//we will now be going up
//toggle increment/decrement
incDec = 1;
}
}
break;
} //END of switch/case
} //END of loop()
//*********************************************************************
void checkSwitches()
{
//**********************************************
//has there been a change in state on the switch ?
buttonState = digitalRead(buttonPin);
if (lastButtonState != buttonState)
{
//update to the new state
lastButtonState = buttonState;
//is the switch closed?
if (buttonState == CLOSED)
{
//clear all the LEDs
clearLEDs();
//the state in the machine
mState++;
//limit the state range
if (mState > maximumState)
{
//back to the begining
mState = 1;
}
//start with the first LED
pixelNumer = 0;
//enable timing
flag = true;
//restart the TIMER
commonMillis = millis();
}
} //END of if(buttonState != lastButtonState)
} //END of checkSwitches()
//*********************************************************************
void clearLEDs()
{
//all LEDs to black
for (byte x = 0; x < numberOfPixels; x++)
{
tira.setPixelColor(x, 0, 0, 0);
}
tira.show();
} //END of clearLEDs()