Hello All!
I am having some weird behavior with the following code that is simply debouncing buttons. Running this code on the Arduino Nano RP2040 Connect causes pin 2 (or any other pin) to be read as a logical 0 even though their is a pull-up resistor configured.
#include <Arduino.h>
#define BTN0 2
#define LEDR 4
volatile bool intr0_triggered = false;
void isr0()
{
intr0_triggered = true;
}
void debounce_call_func(uint8_t pin, uint32_t debounce_time, volatile bool &interrupt_flag, voidFuncPtr func, uint32_t &last_debounce_time, PinStatus desired_state = LOW)
{
uint32_t current_time;
if (interrupt_flag)
{
current_time = millis();
if ((current_time - last_debounce_time) >= debounce_time)
{
if (digitalRead(pin) == desired_state)
{
func();
}
interrupt_flag = false;
last_debounce_time = current_time;
}
}
}
void ledr(){
digitalWrite(LEDR, !(digitalRead(LEDR)));
}
void setup()
{
Serial.begin(PICO_DEFAULT_UART_BAUD_RATE);
pinMode(BTN0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BTN0), isr0, CHANGE);
pinMode(LEDR, OUTPUT);
}
uint32_t ldt0 = 0;
void loop()
{
debounce_call_func(BTN0, 50, intr0_triggered, ledr, ldt0, LOW);
}
I though my board had just malfunctioned, so I rand the following to test if my pulls were just not working.
void setup() {
pinMode(2, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(digitalRead(2));
delay(100);
}
But this works, I am able to read a logical 1 from pin 2.
Anyone know what is causing pin 2 to be read as a logical 0 in my debouncing code? Is it a Arduino quirk?