Hi Tapo!
Yes I remember you haha 😊
One thing to consider, and I hope to find time for some experiments tomorrow, is testing the approach Azroliak used in his comment: see this comment.
He basically added a capacitor in parallel with the button (see attachment), which could help prevent “odd effects”.
Additionally he used longjm (longjump’s) instead of even touching the EEPROM, I’ll post his comment here as well for reference:
—- Azroliak’s comment —-
We can’t use the goto function since we’re not in the same function but, we can use the setjmp/longjmp couple. That way, I can comment the eeprom functions and that work pretty well.
#include "FastLED.h"
//#include <EEPROM.h>
#include <setjmp.h>
#define NUM_LEDS 142
CRGB leds[NUM_LEDS];
#define PIN 5
#define BUTTON 2
byte selectedEffect=0;
jmp_buf envBuffer;
void setup()
{
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
pinMode(2,INPUT_PULLUP); // internal pull-up resistor
attachInterrupt (digitalPinToInterrupt (BUTTON), changeEffect, RISING); // pressed
}
void loop() {
int val;
val = setjmp(envBuffer);
//EEPROM.get(0,selectedEffect);
if(selectedEffect>18) {
selectedEffect=0;
//EEPROM.put(0,0);
}
...
}
void changeEffect() {
if (digitalRead (BUTTON) == HIGH) {
selectedEffect++;
longjmp(envBuffer, selectedEffect);
//EEPROM.put(0, selectedEffect);
//asm volatile (" jmp 0");
}
}
...
I’ve also added a capacitor (100nF) in parallel with the button to avoid boucing effect which allow me to put the interruption on rising edge.
—- —-
Note: Using Goto jumps or longjmp can cause issues with the stack of your Arduino -as far as I understand. But I’ll try to do some experiments this upcoming week.