Hello! Any help would be hugely appreciated. I was having issues with my interrupt triggering multiple times on one button press (not just twice from change, but at times 5-6). My original code is the first block. I need the interrupt because the LEDs will have a loop for the effect and will be tied up for 3/4 of the time. This means that without the interrupt you have to push it at the right time otherwise it won't register.
volatile int i = 0;
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(btnPin), changeEffect, FALLING);
}
void loop() {
switch (i) {
case 0: // Off
memset(colors, 0, sizeof(colors));
break;
case 1:
solid();
break;
case 2:
equilizer();
break;
}
ledStrip.write(colors, ledCount, checkBrightness());
}
void changeEffect() {
i++;
if(i > 2) {
i = 0;
}
Serial.println(i);
}
To fix this, I found an example that said to use millis() to make sure it's only triggered once every 100ms. This works great, but the variable I does not retain its value.
volatile int i = 0;
volatile int past = 0;
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(btnPin), changeEffect, CHANGE);
}
void loop() {
switch (i) {
case 0: // Off
memset(colors, 0, sizeof(colors));
break;
case 1:
solid();
break;
case 2:
equilizer();
break;
}
ledStrip.write(colors, ledCount, checkBrightness());
}
void changeEffect() {
if(millis() - past > 100) {
i++;
if(i > 2) {
i = 0;
}
Serial.println(i);
past = millis();
}
}