I have a project that uses a Lora radio RX and wanted to use the call back function but found it would crash the processor.
So have gone back to the most simple example on using Interrupts using the Arduino example code under the attachInterrupt() documentation
If I run this on a Standard Nano it works as expected. If I replace the nano with a nano rp2040 when I try the interrupt, by taking a pin to 0V the RP2040 crashes and I have to reset the firmware (by loading the blink program into its Flash memory) before I can reprogram it. I have tried it with INPUT_PULLUP and a 4.7k resistor to 3.3V same effect.
If I remove the attachInterrupt() statement and monitor the input state in code I can detect the input level. So it appears to be a problem with the attachInterrupt() function.
I am stuck, I have looked thru similar discussions but cant see what I have to to get the simple interrupt example working. Any ideas very welcome.
Apologies for not using the correct format, hope this is acceptable. Here is the basic code from the Arduino documentation on attachInterrupt() function.
const byte ledPin = 13;
const byte interruptPin = 2; // input pin that the interruption will be attached to
volatile byte state = LOW; // variable that will be updated in the ISR
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
if I run this on a standard Nano it works, if I try it on a Nano RP2040 it does not work. Nothing happens and I have to reset the firmware in the Nano RP2040 before I can do anything else. I have tried different pins and used the pullup and used 4.7k to 3.3V as an external pullup. I have tried code that proves that I can read the state of the input pin so the input hardware is working.
Do you have the right board selected?
Here is my additional URL's. Notice I likely have less and different from you. That is because I am told many are now hard coded so to speak by Arduino. Here are the clues, try both boards and see what happens..
With a pullup resistor it works for me. But how do you test it? Keep in mind that there is no debouncing in this simple example, and an interrupt catches every bounce of a switch.
Regardless I prefer the Raspberry Pi Pico core of Earle F. Philhower, which @sonofcy already suggested.
You don't need another pull-up. I don't have your board, so I can't test, but using an ISR on a button is nuts. Just use any of the button libraries, and I am very sure it will work great.
Here is the example code for the most basic button library. The only thing I would change is to use pins 4, 5, 6.
#include <Button.h>
Button button1(2); // Connect your button between pin 2 and GND
Button button2(3); // Connect your button between pin 3 and GND
Button button3(4); // Connect your button between pin 4 and GND
void setup() {
button1.begin();
button2.begin();
button3.begin();
while (!Serial) { }; // for Leos
Serial.begin(9600);
}
void loop() {
if (button1.pressed())
Serial.println("Button 1 pressed");
if (button2.released())
Serial.println("Button 2 released");
if (button3.toggled()) {
if (button3.read() == Button::PRESSED)
Serial.println("Button 3 has been pressed");
else
Serial.println("Button 3 has been released");
}
}