#include <avr/io.h>
#include <avr/interrupt.h>
const int redPin = 12; // Red LED connected to digital pin 13
const int yellowPin = 11; // Yellow LED connected to digital pin 12
const int greenPin = 10; // Green LED connected to digital pin 11
const int buttonRedPin = 2; // Button for controlling Red LED connected to digital pin 2 (INT0)
const int buttonGreenPin = 3; // Button for controlling Green LED connected to digital pin 3 (INT1)
enum TrafficLightState {
RED,
GREEN,
};
TrafficLightState currentState = RED;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonRedPin, INPUT_PULLUP); // Enable internal pull-up resistor for buttonRedPin
pinMode(buttonGreenPin, INPUT_PULLUP); // Enable internal pull-up resistor for buttonGreenPin
// Configure external interrupts INT0 (pin 2) and INT1 (pin 3)
EICRA |= (1 << ISC00) | (1 << ISC10); // Set interrupt trigger type to any logical change
EIMSK |= (1 << INT0) | (1 << INT1); // Enable external interrupts INT0 and INT1
// Enable global interrupts
sei();
}
void loop() {
// Main loop code goes here
}
// External interrupt INT0 service routine (Button for Red LED)
ISR(INT0_vect) {
if (currentState != RED) {
changeState(RED);
}
}
// External interrupt INT1 service routine (Button for Green LED)
ISR(INT1_vect) {
if (currentState != GREEN) {
changeState(GREEN);
}
}
void changeState(TrafficLightState newState) {
currentState = newState;
switch (currentState) {
case RED:
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
break;
case GREEN:
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
break;
}
}