An interrupt in electronics is a signal that temporarily stops the normal flow of a program so that the system can respond immediately to an important event.
An interrupt is like someone tapping you on the shoulder while you're doing something. You pause what you're doing, respond to the tap, then return to your task.
Interrupts are used to react quickly to events such as:
A button press (like in your Arduino example)
A sensor reaching a threshold
Timers triggering at a set interval
Communication events (like receiving data from a serial port)
The delay() function relies on the millis() timer, which is updated using a timer interrupt (usually Timer0). Here's what that means:
Every millisecond, the Arduino fires an interrupt to increment the millis() count.
delay() just waits until the required number of milliseconds has passed, by checking millis() in a loop.
When you're inside an Interrupt Service Routine (ISR):
Global interrupts are disabled by default.
This means the timer interrupt that updates millis() won’t fire.
As a result, delay() will never return — it just waits forever because millis() isn’t increasing.
So using delay() inside an ISR causes the program to hang (or freeze).
If you must use delays inside an ISR (which you generally shouldn't), you can use:
delayMicroseconds(us); // Crude but works in ISRs
Or manually create a busy-wait:
for (volatile unsigned long i = 0; i < 100000UL; i++);
But again, ISRs should be short, fast, and non-blocking — typically you just set a flag and handle everything in loop().
Prompt: Write Arduino code that turns red, yellow, and blue LEDs on and off in succession, each one for 3 seconds. When a pushbuttlon is pushed, an interrupt is generated which turns off all the LEDs, then flashes the yellow LED on and off 5 times at half-second intervals, then returns to the main red-yelloe-blue loop
ChatGPT said:
Sure! Here's an Arduino sketch that does exactly what you described:
Red, Yellow, and Blue LEDs turn on in succession for 3 seconds each.
When a pushbutton is pressed (using an interrupt), all LEDs turn off.
Then the yellow LED blinks 5 times at 0.5-second intervals.
After that, it resumes the red-yellow-blue loop.
// LED pins
const int greenPin = 7;
const int yellowPin = 8;
const int redPin = 9;
// Button pin
const int buttonPin = 3;
// Walk request flag
volatile bool walkRequested = false;
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
// Attach interrupt to button pin (INT1 on pin 9 for Uno)
attachInterrupt(digitalPinToInterrupt(buttonPin), handleWalkRequest, FALLING);
}
void loop() {
// Normal cycle
digitalWrite(greenPin, HIGH);
delay(2000);
digitalWrite(greenPin, LOW);
checkWalkRequest();
digitalWrite(yellowPin, HIGH);
delay(2000);
digitalWrite(yellowPin, LOW);
checkWalkRequest();
digitalWrite(redPin, HIGH);
delay(2000);
digitalWrite(redPin, LOW);
checkWalkRequest();
}
void handleWalkRequest() {
walkRequested = true;
}
void checkWalkRequest() {
if (walkRequested) {
walkRequested = false; //reset flag
// Flash yellow 3 times (0.5s on, 0.5s off)
for (int i = 0; i < 5; i++) {
digitalWrite(yellowPin, HIGH);
delay(500);
digitalWrite(yellowPin, LOW);
delay(500);
}
}
}
Connect red, yellow, and blue LEDs with current-limiting resistors (e.g., 220Ω) to digital pins 2, 3, and 4.
Connect one leg of the pushbutton to pin 5, and the other to GND.
The sketch uses INPUT_PULLUP so you don’t need an external pull-up resistor.