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!