2

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:

enter image description here

5

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 Apr 8 '17 at 13:11
1

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;
}
  • 1
    An 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 block – James Waldby - jwpat7 Apr 8 '17 at 13:35
  • @jwpat7 Sorry for the code I have edited this code now..check it. Up – Prayuktibid Apr 8 '17 at 14:44
  • And it will blink too fast to be visible. – Edgar Bonet Apr 8 '17 at 14:54
  • @EdgarBonet add some delay() then problem will be solved – Prayuktibid Apr 8 '17 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 Apr 8 '17 at 20:49
1

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:

enter image description here

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, :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.