Using external interrupts in Arduino.

Hello to the Arduino community,

I am trying to write code in arduino using interrupts for an atmega328 and since I am a newbie I need help with my code as I can't get it to work after several tries.

const int ledPin = 13; // LED connected to digital pin 13
const int buttonPin = 4 ; // button on pin 4

define long interval 400

int blinking;
int buttonState = 0;
int lastbuttonstate = 0;
int long thisMills = 0;
int long previousMillis = 0;

void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
attachInterrupt(0, int0handler, LOW);

}

void loop()
{
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH && lastbuttonstate == LOW){
thisMills = millis();
lastbuttonstate=buttonstate;
blinking = true;
}

else { blinking = false;
lastbuttonstate=buttonState;
}

if (lastbuttonstate = HIGH) {
if (buttonState = HIGH && ((thisMills - previousMills) <= interval)){
Serial.print("Event Occured");
blinking = true; // turn on blinking while timing
delay (5) ;
lastbuttonstate = buttonState;
previousMills = thisMills;
}
}

if (buttonState == LOW && lastbuttonstate == HIGH )
{
blinking = false;
lastbuttonstate = buttonState;
}

else if (buttonState == LOW && lastbuttonstate == LOW){
blinking =false;
lastbuttonstate = buttonState;
}

void int0handler(){

if (blinking == true){

digitalWrite(ledPin, HIGH);
}
else if (blinking == false){
digitalWrite(ledPin, LOW);

}

I am trying to print an "Event Occured" command every time a button is pressed twice in succession alongwith blinking an LED.For checking a button press twice I am checking if the interval between the two successive button presses is 400ms or less.The button press needs to be an interrupt and is attached to Interrupt 0 of an atmega328.

It would be great if someone could tell me where I am going wrong in this code or if the whole code is wrong.

Thanks so much!

The button press needs to be an interrupt

Why?

This is how the hardware has been set up.The interrupt from the button pin further has to generate an action on a GUI.

This is how the hardware has been set up

I'm afraid I don't understand. I'll try rewording the question...

It is difficult to get interrupt service routines (like int0handler) correctly coded and even more difficult to get them debugged. Often code like yours can be written without using interrupts. I think it is worth trying to eliminate the interrupt service routine.

My question is, why do you think your application needs to use an interrupt? Why can't you simply read the state of the digital input (the button)?

I do not see volatile modifier here:
int blinking;
Every variable that is used in the interrupt AND in the main code should be declared as volatile.

I already have a hardware built for the code I am writing.The button on my prototype board is attached to the INT0 (PD2) pin of the atmega328.Everytime this button is pressed it needs to mark an event.

I could do it without an interrupt as well but wouldn't be able to achieve the functionality I want on the software end.

But it would be good to know how to make this program work with and without interrupts.

You have the button connected on pin 4, and the interrupt as 0 (which is only for pins 2 and 3):

http://www.arduino.cc/en/Reference/AttachInterrupt

Between that, not declaring the variables volatile, and the strange code in your loop (why are you checking it there, and not in your ISR?)...

:slight_smile:

But it would be good to know how to make this program work with and without interrupts

You may want to start with getting it to compile. :wink:

This redefines long which is going to cause some problems. Remove it...

# define long interval  400

millis returns an unsigned long so these two need to be changed...

[glow]unsigned[/glow] long thisMills = 0;
[glow]unsigned[/glow] long previousMillis = 0;

The variable is buttonState not buttonstate...

lastbuttonstate=button[glow]s[/glow]tate;

These are assignments instead of conditions...

       if (lastbuttonstate [glow]=[/glow] HIGH) {
          if (buttonState [glow]=[/glow] HIGH && ((thisMills - previousMills) <= interval)){

The variable is previousMillis not previousMills...

          if (buttonState = HIGH && ((thisMills - [glow]previousMills[/glow]) <= interval)){

interval should be declared a constant at the top of the Sketch...

const unsigned long interval = 400;

The variable is previousMillis not previousMills...

     previousMills =  thisMills;

There is a closing brace missing from the end of loop.

........
There is a closing brace missing from the end of loop.

A human compiler. Keeewl :smiley: Where do I get me one?