The solution suggested by @nrobinson2000 works perfectly, when I don’t have events like, publish to the particle cloud / writing data to EEPROM in the loop function.
As I have to publish data to cloud, save data to EEPROM, this solution becomes unreliable.
I have started using the below code suggested by @BulldogLowell, it still doesn’t remove the bouncing of the interrupt fully, as it still registers 2 to 3 interrupts for each button press.
Could someone please guide me how I could resolve interrupt bouncing issue ?
Here is a the MicroSwitch that I am using along with my particle Electron.
int button = D2;
void press(void);
int buttonState=1;
volatile bool flag = false;
void setup()
{
Serial.begin(9600);
//while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Interrupt Test");
pinMode(button, INPUT_PULLUP);
attachInterrupt(button, press, FALLING);
}
void loop()
{
if (flag == true)
{
Serial.println("Key Pressed");
flag = false;
}
}
void press()
{
//Serial.println("switched now");
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 50) // debounce time = 50milliseconds
{
flag = true;
}
last_interrupt_time = interrupt_time;
}