I need some help with a project. It is using some LED NeoPixles and a push button to cycle through the different animations.
Right now when the program starts nothing happens. The LED's don't light up, and that is what I want. When I push the button once it moves to Case 1. In this case I want it to keep repeating the "swordPowerUp" animation, but it only runs once. When I put it in a while loop, I can't get it to exit on a button push, to move to Case 2.
Here is the main loop.
case 1:
{
int var = 0;
while(var < 1){
colorWipe(strip.Color(11, 222, 222), 15); //Default Blue for Power On.
strip.show();
var++;
}
SwordColorUp(strip.Color(249,185,255), strip.Color(249,185,255), strip.Color(249,185,255), wait_T, PixelCount, Pixel_Start_End);
Serial.print("Sword Color up");
delay(wait_T);
SwordColorBase(wait_T, PixelCount, Pixel_Start_End);
}
break;
Another issue is once I get it to "Case 2", i then have to press the button twice to get it back to "Case 1" I am assuming that this is because the default case is to have no lights running. Is there a way I can move from "case 1" to "case 2" and back to "case 1" without pressing the button twice from "case 2"
Here is the full project code if it helps.
#include <Adafruit_NeoPixel.h>
//Settings:
#define PIN 6 //The Pin out your Neopixel DIN strip/stick is connected to (Default is 6)
#define TPIXEL 35 //The total amount of pixel's/led's in your connected strip/stick
//To change the timing of between pulses change the number in int 'refresh', to change the speed of it scrolling change the number in int 'wait_T' and 1000 is equal to 1second.
int wait_T=40; //This is the delay between moving back and forth and per pixel
int refresh=400;
int PixelCount=35; //Set this to the AMOUNT of Led's/Pixels you have or want to use on your strip And It can be used to tell where to Stop then return the eye at in the strip
int Pixel_Start_End=0; //Set this to where you want it to Start/End at
boolean UsingBar = false; //Set this to true If you are using the 8x1 Neopixel Bar Or you want to only use 3 leds for the scanner.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(35, PIN);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(35, PIN, NEO_GRB + NEO_KHZ800); //Standered Strip function
//
// RF Pins
//
const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonState = LOW; // current state of the button
int lastButtonState = LOW; // previous state of the button
int pixelState = 0;
int debounce_count = 2; // number of millis/samples to consider before declaring a debounced input
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// read the pushbutton input pin
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// read the state of the switch into a local variable:
buttonState = debounceRead(buttonPin);
Serial.print("ButtonState: ");
Serial.println(buttonState);
Serial.print("LastButtonState: ");
Serial.println(lastButtonState);
Serial.print("showPixels: ");
Serial.println(pixelState);
// compare the buttonState to its previous state
// compare the buttonState to its previous state
if ((LOW == buttonState) && (buttonState != lastButtonState)) {
pixelState = (pixelState + 1) % 3; //can have values of 0, 1, or 2
switch(pixelState)
{
case 1:
{
int var = 0;
while(var < 1){
colorWipe(strip.Color(11, 222, 222), 15); //Default Blue for Power On.
strip.show();
var++;
}
SwordColorUp(strip.Color(249,185,255), strip.Color(249,185,255), strip.Color(249,185,255), wait_T, PixelCount, Pixel_Start_End);
Serial.print("Sword Color up");
delay(wait_T);
SwordColorBase(wait_T, PixelCount, Pixel_Start_End);
}
break;
case 2:
{
ReverseColorWipe(strip.Color(0, 0, 0), 15); //Set lights to off.
strip.show(); // Initialize all pixels to 'off'
}
break;
default:
break;
}
}
lastButtonState = buttonState;
}
uint8_t debounceRead(int pin)
{
uint8_t pinState = digitalRead(pin);
uint32_t timeout = millis();
while (millis() < timeout+10)
{
if (digitalRead(pin) != pinState)
{
pinState = digitalRead(pin);
timeout = millis();
}
}
return pinState;
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void ReverseColorWipe(uint32_t c, uint8_t wait) {
for(int i=(strip.numPixels()-1); i>=0; i--) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void SwordColorBase(uint8_t Delay, int TotalPixels, int pStart) {
for(int i=pStart; i<TotalPixels+2; i++) {
strip.setPixelColor(i, strip.Color(11,222,222)); //Sets the dots to blue
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
//delay(Delay);
}
}
void SwordColorUp(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pStart) {
for(int i=pStart; i<TotalPixels; i++) {
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i-3, strip.Color(11,222,222)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i-2, strip.Color(11,222,222)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(40);
}
}
void SwordColorDown(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pEnd) {
for(int i=TotalPixels-1; i>pEnd; i--) {
if(!UsingBar) { strip.setPixelColor(i-2, Ctt); } //Third Dot Color
strip.setPixelColor(i-1, Ct); //Second Dot Color
strip.setPixelColor(i, Co); //Center Dot Color
strip.setPixelColor(i+1, Ct); //Second Dot Color
if(!UsingBar) { strip.setPixelColor(i+2, Ctt); } //Third Dot Color
if(!UsingBar) {
strip.setPixelColor(i+3, strip.Color(0,0,0)); //Clears the dots after the 3rd color
} else {
strip.setPixelColor(i+2, strip.Color(0,0,0)); //Clears the dots after the 2rd color
}
strip.show();
//Serial.println(i); //Used For pixel Count Debugging
delay(Delay);
}
}
void startSparks (){ // Random sparks - Should loop 30 times
int i;
int p;
for(i=0; i<31; i++) {
p = random(TPIXEL);
pixels.setPixelColor(p, 0xFFFFFF);
pixels.show();
delay(50);
pixels.setPixelColor(p, 0);
}
}
void Sparks (){ // Random sparks - just one LED on at a time!
int i;
int p;
p = random(TPIXEL);
pixels.setPixelColor(p, 0xFFFFFF);
pixels.show();
delay(5);
pixels.setPixelColor(p, 0);
}