The example program is from here: https://www.arduino.cc/en/Reference/attachInterrupt
When I paste in the code and run it, the LED is on all the time (It's supposed to blink):
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;
}
This is my setup:
3 Answers 3
I think you misunderstood this example: it is not expected to automatically blink!
The LED is suposed to change state at every change of level on pin 2. This supposes that you wired something (e.g. a push button) to pin 2 and that this will make pin 2 change level when something happens (e.g. you press the button or you release it).
On your diagram, there is nothing wired on pin 2, so what you observe is just normal.
If you want to see attachInterrupt
in practice, then connect pin 2 to a button with the other button lead connected to GND
.
Then you can observe what happens when you play with that button.
-
I didn't have a button but I connected an ultrasonic distance sensor and the LED starts to blink whenever the distance changes. Thank you :)andrew– andrew04/08/2017 13:11:59Commented Apr 8, 2017 at 13:11
if you want to blink your LED by giving interrupt by push button then you can try this..
int pushButton = 2;
int LED = 13;
volatile bool flag = false;
volatile byte state = LOW;
void setup()
{
pinMode(pushButton, INPUT_PULLUP);
pinMode(LED,OUTPUT);
attachInterrupt(digitalPinToInterrupt(pushButton), &setFlag, FALLING);
}
void loop()
{
if (flag)
{
digitalWrite(LED, state);
delay(100);
state = !state;
}
}
void setFlag()
{
flag = true;
}
-
1An error with the code you show:
setFlag
is not defined. Also, some of the closing braces aren't indented in the code as shown, and the last one isn't shown as part of the code blockJames Waldby - jwpat7– James Waldby - jwpat704/08/2017 13:35:44Commented Apr 8, 2017 at 13:35 -
@jwpat7 Sorry for the code I have edited this code now..check it. UpPrayuktibid– Prayuktibid04/08/2017 14:44:54Commented Apr 8, 2017 at 14:44
-
And it will blink too fast to be visible.Edgar Bonet– Edgar Bonet04/08/2017 14:54:05Commented Apr 8, 2017 at 14:54
-
@EdgarBonet add some
delay()
then problem will be solvedPrayuktibid– Prayuktibid04/08/2017 15:08:24Commented Apr 8, 2017 at 15:08 -
your code is a little bit too complicated. in the "if (flag)" portion of the code, flip the LED via "digitalWrite(LED, !digitalRead(LED));" and then clear the flag. No delay needed.dannyf– dannyf04/08/2017 20:49:37Commented Apr 8, 2017 at 20:49
to expand on my comments to Prayuktibid's code, here is what I would propose in the loop:
void loop()
{
if (flag)
{
//blink the led
digitalWrite(LED, !digitalRead(LED)); //digitalWrite(LED, state);
//delay(100);
//reset the flag
flag = false; //state = !state;
}
}
the rest remains unchanged.
here is how it works:
it works as you would expect, without resorting to delays.
edit: btw, the approach shown above (of setting a flag in the isr, processing that flag in the main loop, and resetting the flag after the processing) is a fairly standard way of utilizing interrupts - pretty much the #1 choice unless you have some special reasons to deviate from it.
edit2: to just follow up on that state above (about trying to process the flag in the main loop).
for something this simple, you can also do it in the isr, making the code much closer to your original code:
void loop()
{
digitalWrite(LED, flag); //digitalWrite(LED, state);
}
void setFlag()
{
//flip the flag
flag = !flag; //flag = true;
}
the code above produced the same waveform as the code posted earlier.
the lessons learned: there is more than one way to solve a problem, as long as you do it right, :)