I need to activate two relays only on the FALLING signal of two distinct physical pushbuttons (each with a 100 nF capacitor in parallel for hardware debouncing). The buttons are connected to the pins D3 and D4 at one end, and on GND at the other end (together). (might be a problem?)
What I chose to do was to attach two different interruptions (one for each physical button) that would lead to two different functions for toggling the states of the relays.
But what is happening is that I’m getting a very weir behavior. When I activate the pushbutton1, sometimes it triggers the correct interruption attachInterrupt(pushbutton1, toggle_relay1, FALLING);, but sometimes it activates the interruption that it isn’t supposed to activate in any way attachInterrupt(pushbutton2, toggle_relay2, FALLING);, and sometimes it activates both interruptions!
The same happens when activating (pressing) pushbutton2.
Also, when I comment out the second interruption attachInterrupt(pushbutton2, toggle_relay2, FALLING); and still pressing the pushbutton2 it toggles the relay1 ! Remembering that at one end they are connected to different pins on Photon, and at the other they are wired up together and connected to the board GND.
Could somebody help me solving this? Thank you in advance!
Here is my very simple code:
/* RELAYS */
int relay1 = D2;
int relay2 = D1;
/* PHYSICAL PUSHBUTTONS */
int pushbutton1 = D3;
int pushbutton2 = D4;
volatile int state_relay1 = LOW; // Initialize the relay 1 as LOW or ON
volatile int state_relay2 = LOW; // Initialize the relay 2 as LOW or ON
void setup(){
pinMode(relay1, OUTPUT); //setup of relay1 as output on Photon
pinMode(pushbutton1, INPUT_PULLUP);
attachInterrupt(pushbutton1, toggle_relay1, FALLING); //attach the interruption to toggle the state_relay1 on FALLING of pushbutton1
pinMode(relay2, OUTPUT); //setup of relay2 as output on Photon
pinMode(pushbutton2, INPUT_PULLUP);
attachInterrupt(pushbutton2, toggle_relay2, FALLING); //attach the interruption to toggle the state_relay2 on FALLING of pushbutton2
}
//Function to toggle_relay1 when the interruption from the falling edge signal from pushbutton1 happens
void toggle_relay1() {
state_relay1 = !state_relay1;
digitalWrite(relay1, state_relay1); //Writes the state_relay1 to relay 1
}
//Function to toggle_relay2 when the interruption from the falling edge signal from pushbutton2 happens
void toggle_relay2() {
state_relay2 = !state_relay2;
digitalWrite(relay2, state_relay2); //Writes the state_relay1 to relay 2
}