Im currently using the arduino GIGA r1 wifi for a project and im trying to setup an interrupt pin however whenever I include the AttachInterupt() function as shown below whenever i upload my code to the board the boot0 light blinks read and my code doesnt run. when i run arduios example code of interrupts it works fine. so any ideas as to why this would break my code would be appreciated
const int pin_interrupt = 9;
attachInterrupt(digitalPinToInterrupt(pin_interrupt),interrupt_handler, CHANGE);
I moved your topic to an appropriate forum category @dswain1.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Hi @dswain1. I just gave it a try with this minimal sketch:
void setup() {
const int pin_interrupt = 9;
attachInterrupt(digitalPinToInterrupt(pin_interrupt), interrupt_handler, CHANGE);
}
void loop() {}
void interrupt_handler() {}
The problem did not occur. This tells me that it is related to something in the other code of your sketch. Please post the full contents of the sketch code in a reply here and I'll investigate further.
It is likely caused by some other code having attached an interrupt to another pin on the same "line". That causes the Mbed OS operating system of the GIGA R1 WiFi board to crash, as explained here:
https://github.com/arduino/ArduinoCore-mbed/issues/789#issuecomment-1840246454
I tried that sketch as well and whenever i changed the value of the interrupt pin it cause my arduino to crash
Crash, eh? I have never used the Giga R1 but I found this:
https://docs.arduino.cc/tutorials/giga-r1-wifi/cheat-sheet/
On that page it says this:
"it is possible for the operating system to crash while running a sketch.
On most Arduino boards, when a sketch fails due to e.g. memory shortage, the board resets.
On the GIGA R1, whenever the MbedOS fails, the board does not reset automatically. Instead, if it fails, the onboard red LED will start to blink in a looping pattern of 4 fast blinks and 4 slow blinks."
Just a shot in the dark, hopefully it helps to get to the bottom of the issue.
@ptillisch said
?
Does this work for you:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
What goes on in your interrupt_handler function?
No that code breaks as well even when i have the blink function simply do a serial print, ive also tried this on multiple arduinos
That is the reason for the crash.
You should not do anything with serial in the interrupt handler. You should only set a flag variable, as done in the code @hallowed31 shared, then run the serial code in the loop when that flag is set:
volatile bool someFlag = false;
void setup() {
Serial.begin(9600);
const byte pin_interrupt = 9;
pinMode(pin_interrupt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pin_interrupt), interrupt_handler, CHANGE);
}
void loop() {
if(someFlag) {
someFlag = false;
Serial.println("Hello, world!");
}
}
void interrupt_handler() {
someFlag = true;
}
I've made that same error. The code I posted wasn't mine - it was copied from the Giga wifi cheat sheet on docs.arduino.cc. I mention it only because I don't want to form any impression that I wrote it.
Also, I notice that this:
doesn't say this:
void loop() {
if(someFlag == true) {
someFlag = false;
Serial.println("Hello, world!");
}
}
which afaik, is the more common way to test conditionals, going by the basic examples in the Arduino IDE.
Am I correct to say that the way you wrote it is so the loop can't miss the very fast change of the interrupt pin whose routine exists outside of the main loop?
My understanding is that an interrupt is preferred over simply polling a state change of a pin for situations where we have "mission critical" events that must not rely on anything else going on in the program, and whose actions must occur "right now" as opposed to waiting in a typical state change detection. Is that correct?
I'm always trying to better understand exactly what the Arduino is doing, I'm a hobbyist and ongoing learner myself.
if(someFlag) and if(someFlag == true) are functionally identical (I'm sure they even compile to the same machine code).
In professional code, you would never see the second variant because it would be considered an excessively verbose way to write this when the function of the alternative code is 100% clear to any experienced programmer. However, if(someFlag) might not be clear to a less experienced programmer while if(someFlag == true) is quite clear so I completely support the use of the more verbose variant in code that will be exposed to Arduino users since we put a focus on making embedded systems accessible to everyone.
No, it was purely a stylistic choice. There is no functional reason for writing it the way I did vs. your variant.
Thanks for taking the time to explain that! ![]()