/*
========================
Global Variables
========================
*/
//To keep track of the last time ISR was executed (not just entered)
unsigned long lastExecuted = 0;
/*
The amount of time to wait before considering another edge
as a valid Interrupt, so as to remove the effect of bouncing.
*/
unsigned long debounceDelay = 250;
/*
To keep track of the LED State and which state to get into next.
1000 - No LEDs ON
0gyr - Turn ON the LED that has a 1 (gyr - green, yellow, red)
*/
uint8_t ledState = 0b1000;
/*
Arduino Pin and Interrupt Setup.
*/
void setup()
{
/*
========================
Pin Mode Setup
========================
*/
// Set D3, D4, D5 as output (All other pins of Port D are input)
DDRD = 0b00111000;
/*
========================
Hardware Interrupt Setup for Button Press on D2(INT0)
========================
*/
// Global Interrupt Enable
SREG |= 1 << 7;
// Enable External Interrupt INT0
EIMSK = 0b00000001;
// Set the INT0(Pin 2) interrupt as Rising edge.
EICRA = 0b00000011;
}
/*
There is no need for loop as an action is only
required when an interrupt occurs, which is handled
by the ISR
*/
void loop()
{}
// ISR for Hardware Interrupt INT0(D2)
ISR(INT0_vect)
{
/*
If the ISR was entered with a period of Debounce Delay from
the last execution, do not execute further as the current
ISR call is most likely due to Bouncing.
*/
if ((millis() - lastExecuted) > debounceDelay)
{
/*
Reset the last execution time to the current
time when executing the ISR
*/
lastExecuted = millis();
// Change the State of LEDs to their next state.
if (ledState == 0b1000)
{
ledState = 0b0001;
}
else
{
ledState = ledState << 1;
}
/*
Set the LED outputs according to the LED State by
modyfing PORTD, taking care to not disturb any
other pins that aren't LEDs.
*/
PORTD = ((ledState & 0x7) << 3) | (PORTD & 0xC7);
}
}