/*example event-driven programming using ISR*/
#define led1 13
#define led2 12
boolean toggle1 = 0;
boolean toggle2 = 0;

int count=0;

void setup() {
  // set output pins
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  cli();//stop interrupts

//set timer1 interrupt at 1Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and CS10 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei();//allow interrupts
}

ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5Hz (takes two cycles for full wave- toggle high then toggle low)
  count++;
  toggle1 =1;    // flag for orange led event 
  if (count==3)  // flag for green led event
    toggle2=1;
}

void loop() {
  // put your main code here, to run repeatedly:
  if (toggle1){   // 
     digitalWrite(led1, digitalRead(led1)^1); // turn on/off orange led event
     toggle1=0;
  }

  if (toggle2){
     digitalWrite(led2, digitalRead(led2)^1); // turn on/off green led event
     toggle2=0;
     count=0;
  }

}