i plan to run LED animations from my arduino.
basically i have 2 buttons, one should switch the main mode i am in, the other should change sub-modes like color or speed.
when using buttons like i had in my (last post) this caused problems since it only worked when i pressed them at the right moment. i figure this is because some of the animation are quite long. so now i switched to interrupts
(please tell me if this is wong!)
the problem now is, that this code runs for a few cycle where i can change modes just fine. but at some point it will just get stuck. there is no serial output anymore and i need to restart the whole thing.
i saw this happening after just ~20 cycles, other times it runs fine for minutes
another thing i see sometimes is that when pressing the 2nd button instead of going from mode 1-1 to 1-2 it seems to rest and go back to 1-0.
i tried to make sure that i am not pressing the button too long - this does not seem to be the case
am i doing this completely wrong, or am i at least on the right track?
#include "FastLED.h"
#define NUM_LEDS 25
CRGB leds[NUM_LEDS];
#define LedPin 5
#define BUTTON 2
#define BUTTON2 3
// Variables will change:
int buttonPushCounter = 0; // counters for the number of button presses
int buttonPushCounter2 = 0;
void setup() {
// initialize serial communication:
Serial.begin(9600);
//setup LEDs
FastLED.addLeds<WS2812, LedPin, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
//attach ínterrupts
attachInterrupt (digitalPinToInterrupt (BUTTON), changeEffect, CHANGE); // pressed
attachInterrupt (digitalPinToInterrupt (BUTTON2), changeEffect2, CHANGE); // pressed
}
void loop() {
switch (buttonPushCounter)
{
case 0:
Serial.println(F("Case 0"));
//flickeringRainbow()
//flickeringRainbowSolid()
case0();
break;
case 1:
Serial.println(F("Case 1"));
case1();
break;
case 2:
Serial.println(F("Case 2"));
break;
case 3:
Serial.println(F("Case 3"));
break;
}
if (buttonPushCounter == 4)
buttonPushCounter = 0;
}
// ########## Case 0 ##############
void case0()
{
//Serial.println(F("buttonPushCounter2= "));Serial.println(buttonPushCounter2); // debug pushCounter2
switch (buttonPushCounter2)
{
case 0:
Serial.println(F("Case 0-0"));
//flickeringRainbow();
break;
case 1:
Serial.println(F("Case 0-1"));
//flickeringRainbowSolid();
break;
}
if (buttonPushCounter2 == 2)
{
buttonPushCounter2 = 0;
}
}
// ########## Case 1 ##############
void case1()
{
//Serial.println(F("buttonPushCounter2= "));Serial.println(buttonPushCounter2); // debug pushCounter2
switch (buttonPushCounter2)
{
case 0:
Serial.println(F("Case 1-0"));
//rainbowCycle(10);
break;
case 1:
Serial.println(F("Case 1-1"));
//rainbowCycle(20);
break;
case 2:
Serial.println(F("Case 1-2"));
//rainbowCycle(50);
break;
}
if (buttonPushCounter2 == 2)
{
buttonPushCounter2 = 0;
}
}
void changeEffect() {
if (digitalRead (BUTTON) == HIGH) {
buttonPushCounter++;
buttonPushCounter2 = 0;
}
}
void changeEffect2() {
if (digitalRead (BUTTON2) == HIGH) {
buttonPushCounter2++;
}
}