I am facing a very troubling issue here.
I use a very simple code that has an interrupt attached to a button, and toggles the state of the relay when an interrupt is fired on the button.
To debounce the button, I have tried using both external and also internal pulldowns/pullups
The interesting/troubling issue I am having is, when there is a pull resistor in place, (internal or external) , whenever I plug something into a nearby AC source or switch on/off a plugpoint, an interrupt is being triggered.
If I do not add a pull resistor, the AC does not interfere, but obviously there are loads and loads of random interrupts being fired . I do have a software debounce in place that does not process false interrupts, but there are too many interrupts being fired and it is not a good behaviour.
So tl;dr with pull resistor, AC load triggers interrupt , and I wanted to know if anyone else had faced or know any solutions for this.
I am posting the code I have used here below.
Code: Select all
#define BUTTON 16
#define RELAY 17
volatile unsigned long int interrupt,last_interrupt = 0;
volatile bool state = 0, change = 0;
void IRAM_ATTR buttonPressHandler(){
interrupt = millis();
if(interrupt - last_interrupt > 200){
state = !state;
digitalWrite(RELAY, state);
change = 1;
}
last_interrupt = interrupt;
}
void setup(){
Serial.begin(115200);
pinMode(BUTTON,INPUT_PULLUP);
attachInterrupt(BUTTON, buttonPressHandler, RISING);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, state);
}
void loop(){
if(change){
Serial.println("Set relay to : " + String(state));
Serial.println("Last ISR at : " + String(last_interrupt));
change = 0;
}
}