This code does not work on the UNO R4
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
// initialize serial communication
Serial.begin(57600);
// while (!Serial); //
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
Serial.print("State = ");
Serial.println(state);
}
void blink() {
state = !state;
}
The state never is set to 1.
I have two UNO R4's and both fail to fire interrupt.
I had been trying to use the GY-86 with interrupt, however, interrupt never happens.
Can someone help me with this?
Worked for me on my R4 minima once I took out all the duplicates of the code. Ground pin 2, state switches to HIGH, disconnect, back to LOW.
Try this slight modification which only prints when the state changes:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile bool state = LOW;
void setup() {
// initialize serial communication
Serial.begin(57600);
// while (!Serial); //
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
static bool oldState = LOW;
if( state != oldState ) {
oldState = state;
digitalWrite(ledPin, oldState);
Serial.print("State = ");
Serial.println(oldState);
delay(20);
}
}
void blink() {
state = !state;
}
Please look at the code that you posted. Did you make a mistake when posting it as it seems to be repeated multiple times
Thank you for your response.
I tried your code, however, seems the interrupt does not fire as there is no output to the serial console.
Not sure what else to check.
Thank you, I did not notice the double copy of the code.
Hi, once again thank you.
I should have read your reply more carefully.
If I ground pin 2 , I see the text in the console.
Many thanks, now I can concentrate on the GY-86 to determine why it is not firing its interrupt.
Now that your question has been answered, please take the time to mark the topic as solved by clicking the Solution button at the bottom of the reply that answered your question , as shown in How to get the best out of this forum .
docdoc
November 16, 2023, 9:23am
9
Good, so now please edit your first post and get rid of the duplicates...
docdoc
November 16, 2023, 5:53pm
11
Not yet... You still have twice the same code (two setup, two loop, and two blink): remove everything after the first blink function and you're done!
Thank you. I have cleaned up the code to remove the duplicates.