Handling External interrupt won't work

hi there
i just create a program like this

#include <avr/io.h>
#include <avr/interrupt.h>
const int buttonPin = 2;
const int ledPin = 13;
const int buzzer = 12;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(buttonPin, INPUT);
sei();
EIMSK |= (1 << INT0); //ENABLE GLOBAL INT0 INTERRUPT
EICRA |= ( 1 << ISC00 | 1 << ISC01); // MAKE IT RISING
//EICRA |= ( 1 << ISC01); // —"----*/
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay(500);
}
ISR(EXT_INT0_vect){
digitalWrite(buzzer,HIGH);
delay(2000);
digitalWrite(buzzer,LOW);
}
I’m using mega 2560,no error when compile and upload
i just don’t understand why it’s won’t work.
please somebody help because i can’t figure it out for 5 hours googling and test

delay relies upon timer interrupts that are turned off in your interrupt handler. So you cannot use delay in an ISR and expect anything other than a hung up processor. Interrupts are for things that happen very very very fast, not things that involve 2 second delays.

yes i know exactly why it’s have to be short
i already test to delete the delay like this

ISR(EXT_INT0_vect){
digitalWrite(buzzer,HIGH);
}

the buzzer should be on when i pressed the button. but it won’t.

samuelbles:
the buzzer should be on when i pressed the button. but it won’t.

You don’t need an interrupt for a button that a human presses. Just use digitalRead() every time loop() repeats.

…R

actually i use interrupt not for that sketch. i make a robot that have a button in front of it. so if the robot hit something, the robot will turn around i can't call read button all the time

samuelbles: actually i use interrupt not for that sketch. i make a robot that have a button in front of it. so if the robot hit something, the robot will turn around i can't call read button all the time

There is nothing in that description that precludes polling a button pin if the program is properly designed so that loop() can repeat hundreds or thousands of times per second.

An interrupt is generally only essential to capture a very short-lived event or to detect frequent events.

Of course it can be used for detecting any sort of button press. The usual way is to use the ISR to set a flag to record that the event has happened and use code in loop() to do something when it sees that the flag has been set.

...R

other than my purpose
i just can’t understand why the isr wasn’t called
i just try this code

#include <avr/io.h>
#include <avr/interrupt.h>
const int buttonPin = 2;
const int ledPin = 13;
const int buzzer = 12;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(buttonPin, INPUT);
cli(); //
EICRA |= (1<<ISC00); // Enable trigger on rising edge
EIFR = bit (INTF0);
EIMSK |= (1<<INT0);
sei();
}
void loop() {
digitalWrite(ledPin, state);
}
ISR(EXT_INT0_vect){
state = !state;
}

still won’t work. please help

Why not use attachInterrupt instead of diddling with the registers like that?

because attachInterrupt() using timer 0 i just don't want the timer 0 used

samuelbles: because attachInterrupt() using timer 0

What makes you think that would be true?

because delay and milis not work in that function delay and milis are in the timer 0 am i right? sorry i'm kinda newbie here

Delay and millis use the timer, so what?

In which way is this connected to interrupts, or - as you claimed - attachInterrupt?

external interrupts and attachInterrupt have absolutely nothing to do with timers. Where did you get the idea that they did?

I suspect the OP is under the mistaken belief that if he avoids Timer0 he can use delay() and millis() inside his ISR.

The millis() function depends on interrupts happening. In an ISR (no matter what causes the ISR to trigger) interrupts are switched off. This can affect several aspects of the Arduino system. Which is why each interrupt should be as short as possible - 100 microseconds would be long.

...R