Final Prince Sharma
Final Prince Sharma
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
int main(void) {
// INTERNAL INPUT PULLUP RESISTER FOR SWITCH
DDRD &= ~(1 << BUTTON_PIN);
PORTD |= (1 << BUTTON_PIN);
// 3 LEDs
DDRB |= (1 << LED_PIN_1) | (1 << LED_PIN_2) | (1 << LED_PIN_3);
while (1)
{
// Main logic of code
}
return 0;
}
ISR(INT0_vect) {
_delay_ms(50); // Code logic for Debouncing
if (!(PIND & (1 << BUTTON_PIN))) {
if (count == 3) {// if 3rd led is on
count = 0;
} else {
count++;
}
blink_led(count);
_delay_ms(500);
}
}
void blink_led(int n) {
switch (n) {
case 0:
PORTB &= ~(1 << LED_PIN_1);
PORTB &= ~(1 << LED_PIN_2);
PORTB &= ~(1 << LED_PIN_3);
break;
case 1:
PORTB |= (1 << LED_PIN_1);
break;
case 2:
PORTB &= ~(1 << LED_PIN_1);
PORTB |= (1 << LED_PIN_2);
break;
case 3:
PORTB &= ~(1 << LED_PIN_2);
PORTB |= (1 << LED_PIN_3);
break;
}
}
Pin Connection
Component Connection Arduino Pin ATmega328P Pin
LED 1 Anode (+) D8 PB0
LED 2 Anode (+) D9 PB1
LED 3 Anode (+) D10 PB2
Push Button One side D2 PD2
Other side GND GND