Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- volatile int count = 0; //variable counts 2sec delays to make 10
- volatile int state = 0; //keeps track of what state the program is in
- void setup()
- {
- cli(); //disable interrupts
- DDRB |= 0x01; //pin D8 is output for LED
- TCCR1A = 0x00; //normal timer1 operation
- TCCR1B = 0x00; // "
- TCCR1B |= (1 << CS10); //prescalar set to 1024
- TCCR1B |= (1 << CS12); // "
- sei(); //enable global interrupts
- }
- void loop()
- {
- if(!(PINB & 0x02)) //if pin D9 goes low
- {
- if(state == 0) //light on 10 secs
- {
- PORTB |= 0x01; //LED ON
- TCNT1 = 0; //initialize timer to 0
- count = 0; //initialize counter to 0
- TIMSK1 |= (1 << OCIE1A); //enable 10 sec interrupt
- }
- if(state == 1) //light flashes for 10 secs
- {
- PORTB &= ~0x01; //led off, then toggles in ISR
- TCNT1 = 0; //initialize timer to 0
- count = 0; //initialize counter to 0
- TIMSK1 |= (1 << OCIE1A); // start 10 sec
- TIMSK1 |= (1 << OCIE1B); // start toggle 200ms
- }
- state += 1; //increase button count
- delay(100); //debounce
- while(!(PINB & 0x02)); //stay here while button is pressed
- delay(100); //debounce
- }
- }
- ISR(TIMER1_COMPA_vect) //10 second
- {
- count += 1;
- OCR1A = TCNT1 + 31250; //up the Output compare another 2 secs from now
- if(count >= 5) //do 2 second delays 5 times
- {
- TIMSK1 = (0 << OCIE1A); //disable compares
- TIMSK1 = (0 << OCIE1B); // "
- count = 0;
- state = 0;
- PORTB &= ~0x01; // LED off
- }
- }
- ISR(TIMER1_COMPB_vect) //200 ms
- {
- OCR1B = TCNT1 + 3125; //up the OC 200 ms from now
- if(PINB & 0x01) //toggle LED
- {
- PORTB &= ~0x01;
- }else
- {
- PORTB |= 0x01;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement