volatile bool LED0_STATE = false;
volatile bool LED1_STATE = false;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); //Set the pin to be OUTPUT
pinMode(7, OUTPUT); //Set the pin to be OUTPUT
// TIMER 0 should not be used - it is already used by millis and delay
// TIMER 1 in Normal (overflow) mode for interrupt every 4 sec-ish
noInterrupts(); // disable interrupts while we setup
// reset Timer 1 configurations, default to interrupt on overflow
TCCR1A = 0;
TCCR1B = 0;
// set bits 0-2 to select the scalar value
// 001 = 1, 010 = 8, 011 = 64, 100 = 256, 101 = 1024
TCCR1B = (TCCR1B & 0b11111000) | 0b101; // Pre-scalar = 1024
// enable timer 1 overflow interrupt by setting bit 0 of TIMSK1
TIMSK1 |= 1;
interrupts(); // enable all interrupts
noInterrupts(); // stop interrupts - same as cli()
// TIMER 2 in CTC mode with interrupts at 125Hz
TCCR2A = 0; TCCR2B = 0; // set entire registers to 0
TCNT2 = 0; //initialize counter value to 0
OCR2A = 124; // = (16*10^6) / (125*1024) - 1 (must be < 256)
TCCR2A |= (1 << WGM21); // turn on CTC mode
TCCR2B |= (1 << CS02) | (1 << CS01) | (1 << CS00); // 1024 prescaler
TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
interrupts(); // enable interrupts - same as sei()
}
void loop() {
// put your main code here, to run repeatedly:
}
ISR(TIMER1_OVF_vect) {
LED1_STATE = !LED1_STATE; //Invert LED state
digitalWrite(13, LED1_STATE); //Write new state to the LED on pin D5
}
ISR(TIMER2_COMPA_vect){
LED0_STATE = !LED0_STATE;
digitalWrite(7, LED0_STATE);
//Serial.println(millis());
}