Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define HR_LED _BV(2)
- #define HY_LED _BV(3)
- #define HG_LED _BV(4)
- #define VR_LED _BV(5)
- #define VY_LED _BV(6)
- #define VG_LED _BV(7)
- #define SW1 _BV(1)
- #define SW2 _BV(2)
- #define SW3 _BV(3)
- int timer=0;
- byte mode;
- void setup()
- {
- DDRD |= (HR_LED | HY_LED | HG_LED | VR_LED | VY_LED | VG_LED);
- DDRB &= ~(SW1 | SW2 | SW3);
- //ALL LED OFF
- PORTD = 0;
- }
- void run(const byte LEDR, const byte LEDY, const byte LEDG, const int t){
- int ct = timer-t;
- if(ct==1){
- PORTD &= ~LEDR;
- PORTD |= LEDG;
- }
- else if(ct==8250){
- PORTD |= LEDG;
- }
- else if(ct==8500){
- PORTD &= ~LEDG;
- }
- else if(ct==8750){
- PORTD |= LEDG;
- }
- else if(ct==9000){
- PORTD &= ~LEDG;
- }
- else if(ct>9250 && ct<12500){
- PORTD |= LEDY;
- }
- else if(ct==12500){
- PORTD &= ~LEDY;
- PORTD |= LEDR;
- }
- }
- void flash(const byte LEDA, const byte LEDB){
- if(timer==0){
- PORTD |= LEDA;
- PORTD |= LEDB;
- }
- else if(timer==500){
- PORTD &= ~LEDA;
- PORTD &= ~LEDB;
- }
- }
- void loop()
- {
- // 0 0 0
- if(!(PINB & SW1) && !(PINB & SW2) && !(PINB & SW3)){
- if(mode != 0){
- mode = 0;
- timer = 0;
- PORTD = 0;
- }
- mode = 0;
- if(timer==0){
- PORTD |= VR_LED;
- }
- else if(timer<=12500){
- run(HR_LED, HY_LED, HG_LED, 0);
- }
- else if(timer>12500 && timer<=25000){
- run(VR_LED, VY_LED, VG_LED, 12500);
- }
- else if(timer>25000){
- timer = 0;
- }
- }
- // 1 0 0
- else if((PINB & SW1) && !(PINB & SW2) && !(PINB & SW3)){
- if(mode != 1){
- mode = 1;
- timer = 0;
- PORTD = 0;
- }
- if(timer==1000){
- timer = 0;
- }
- flash(HR_LED, VY_LED);
- }
- // 1 1 0
- else if((PINB & SW1) && (PINB & SW2) && !(PINB & SW3)){
- if(mode != 2){
- mode = 2;
- timer = 0;
- }
- PORTD = HG_LED | VR_LED;
- }
- // 1 1 1
- else if((PINB & SW1) && (PINB & SW2) && (PINB & SW3)){
- if(mode != 3){
- mode = 3;
- timer = 0;
- }
- PORTD = VG_LED | HR_LED;
- }
- else{
- mode = -1;
- PORTD = 0;
- }
- delay(1);
- timer++;
- }
Add Comment
Please, Sign In to add comment