#include <avr/io.h>
#include <util/delay.h>
#define BTN PD2
#define RED PD3
#define ORANGE PD4
#define GREEN PD5
#define BLUE_BLINK PD6
#define RED_WALK PD7
#define GREEN_WALK PB0
#define DELAY 2500
#define BLINK_DELAY 250
#define WALK_DELAY 10000
void init();
void runLoop();
void setCarRed();
void setCarOrange();
void setCarGreen();
void setBlinkBlue();
void setWalkGreen();
int main() {
init();
while(1) {
runLoop();
}
return 0;
}
void init() {
DDRD &= ~(0 << BTN);
DDRD |= (1 << RED);
DDRD |= (1 << ORANGE);
DDRD |= (1 << GREEN);
DDRD |= (1 << BLUE_BLINK);
DDRD |= (1 << RED_WALK);
DDRB |= (1 << GREEN_WALK);
setCarGreen();
}
void runLoop() {
// if the BTN pin (PD2) gets a high signal, start the lighting sequence
if (PIND & (1 << BTN)) {
setBlinkBlue();
setCarOrange();
setCarRed();
setWalkGreen();
setCarRed();
setCarGreen();
}
}
void setBlinkBlue() {
PORTD &= ~(1 << RED); // RED LED off
PORTD &= ~(1 << ORANGE); // ORANGE LED off
PORTD |= (1 << GREEN); // GREEN LED on
PORTD |= (1 << RED_WALK); // RED_WALK LED on
PORTB &= ~(1 << GREEN_WALK); // GREEN_WALK LED off
// blink the blue light 5 times
for (int i = 0; i < 10; i++) {
PORTD ^= (1 << BLUE_BLINK); // BLUE_BLINK LED blink
_delay_ms(BLINK_DELAY);
}
}
void setCarOrange() {
PORTD &= ~(1 << RED); // RED LED off
PORTD |= (1 << ORANGE); // ORANGE LED on
PORTD &= ~(1 << GREEN); // GREEN LED off
PORTD |= (1 << RED_WALK); // RED_WALK LED on
PORTB &= ~(1 << GREEN_WALK); // GREEN_WALK LED off
PORTD &= ~(1 << BLUE_BLINK); // BLUE_BLINK LED off
_delay_ms(DELAY);
}
void setCarRed() {
PORTD |= (1 << RED); // RED LED on
PORTD &= ~(1 << ORANGE); // ORANGE LED off
PORTD &= ~(1 << GREEN); // GREEN LED off
PORTD |= (1 << RED_WALK); // RED_WALK LED on
PORTB &= ~(1 << GREEN_WALK); // GREEN_WALK LED off
PORTD &= ~(1 << BLUE_BLINK); // BLUE_BLINK LED off
_delay_ms(DELAY);
}
void setWalkGreen() {
PORTD |= (1 << RED); // RED LED on
PORTD &= ~(1 << ORANGE); // ORANGE LED off
PORTD &= ~(1 << GREEN); // GREEN LED off
PORTD &= ~(1 << RED_WALK); // RED_WALK LED on
PORTB |= (1 << GREEN_WALK); // GREEN_WALK LED off
PORTD &= ~(1 << BLUE_BLINK); // BLUE_BLINK LED off
_delay_ms(WALK_DELAY);
}
void setCarGreen() {
PORTD &= ~(1 << RED); // RED LED off
PORTD &= ~(1 << ORANGE); // ORANGE LED off
PORTD |= (1 << GREEN); // GREEN LED on
PORTD |= (1 << RED_WALK); // RED_WALK LED on
PORTB &= ~(1 << GREEN_WALK); // GREEN_WALK LED off
PORTD |= (1 << BLUE_BLINK); // BLUE_BLINK LED on
// no delay because default state
}Voyant
clignotant
Passage
piétons
Feux de
circulation