Hi all,
I'm not sure whether this is a problem in the sketch or if it is a hardware problem. I am using the DOIT ESP32 DEVKIT V1 board and I've got a "D-PAD" of sorts with 4 tactile buttons. The buttons share a common ground wire. It works as expected most of the time but occasionally I press 1 button and the interrupt for a different button gets triggered first. It's almost like the jittering behavior except instead of seeing a double press its, I'm getting a one-two punch.
I've got some output to demonstrate. There are some other parts to the sketch that I've omitted for simplification here (but i did double check the behavior still occurs in the sketch below) so if it seems overly complicated that's part of the reason.
#define UP 25
#define DOWN 33
#define LEFT 26
#define RIGHT 32
#define DEBOUNCE_MICROS 50000
volatile unsigned long last_micros;
bool isInterrupted = false;
int vAxis = 0;
int vMax = 10;
int vMin = 0;
int hAxis = 0;
int hMax = 10;
int hMin = 0;
int vValue = 0;
int hValue = 0;
void setup() {
Serial.begin(115200);
pinMode(UP, INPUT_PULLUP);
attachInterrupt(UP, debounceUp, RISING);
pinMode(DOWN, INPUT_PULLUP);
attachInterrupt(DOWN, debounceDown, RISING);
pinMode(LEFT, INPUT_PULLUP);
attachInterrupt(LEFT, debounceLeft, RISING);
pinMode(RIGHT, INPUT_PULLUP);
attachInterrupt(RIGHT, debounceRight, RISING);
}
void loop() {
if (isInterrupted) {
update();
}
}
void debounceUp() {
if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
if (!isInterrupted && vAxis < vMax) {
++vAxis;
isInterrupted = true;
}
last_micros = micros();
}
}
void debounceDown() {
if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
if (!isInterrupted && vAxis > vMin) {
--vAxis;
isInterrupted = true;
}
last_micros = micros();
}
}
void debounceLeft() {
if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
if (!isInterrupted && hAxis > hMin) {
--hAxis;
isInterrupted = true;
}
last_micros = micros();
}
}
void debounceRight() {
if((long)(micros() - last_micros) >= DEBOUNCE_MICROS) {
if (!isInterrupted && hAxis < hMax) {
++hAxis;
isInterrupted = true;
}
last_micros = micros();
}
}
void update() {
if (hAxis != hValue) {
hValue = hAxis;
Serial.print("updated h to ");
Serial.println(hValue);
}
else if (vAxis != vValue) {
vValue = vAxis;
Serial.print("updated v to ");
Serial.println(vValue);
}
Serial.print("vValue: ");
Serial.print(vValue);
Serial.print("; hValue: ");
Serial.println(hValue);
isInterrupted = false;
}
Playing with the buttons some here is what I'm seeing
Walking right then left along the horizontal axis
11:33:52.643 -> updated h to 5
11:33:52.643 -> vValue: 0; hValue: 5
11:33:53.568 -> updated h to 4
11:33:53.568 -> vValue: 0; hValue: 4
11:33:54.389 -> updated v to 1 <————— THIS (Up button wasn’t pressed)
11:33:54.389 -> vValue: 1; hValue: 4
11:33:54.554 -> updated h to 3
11:33:54.554 -> vValue: 1; hValue: 3
Only pressing the UP button
11:34:53.842 -> updated v to 1
11:34:53.842 -> vValue: 1; hValue: 0
11:34:55.028 -> updated v to 2
11:34:55.028 -> vValue: 2; hValue: 0
11:34:56.217 -> updated v to 1. <————— THIS (Down button wasn’t pressed)
11:34:56.217 -> vValue: 1; hValue: 0
11:34:56.350 -> updated v to 2
11:34:56.350 -> vValue: 2; hValue: 0
I've tried assigning each interrupt function its own "last_micros" value and that didn't do anything. I really appreciate any assistance with this. Thanks so much in advance. Let me know if I need to provide any additional details