Sunday, December 7, 2014

Arduino Minute Timer

This might not be the best way... you should read this good one here as well..

//http://www.instructables.com/id/Arduino-Timer-Interrupts/

#define num_mins 2
#define LED 11
int COUNT = 1;

void setup(){

  cli();//stop interrupts

  //set timer1 interrupt at 0.25 Hz
  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 = 4*15624;// = (16*10^6) / (4*1*1024) - 1 (must be <65536 )
 // turn on CTC mode
  TCCR1B |= (1 << WGM12);  // from ATMEL datasheet
  // Set CS10 and CS12 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10); 
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts

pinMode( LED, OUTPUT );

}//end setup

ISR(TIMER1_COMPA_vect){//timer1 interrupt 0.25 Hz increment the counter
  COUNT = COUNT+1;
}

void loop(){
  int trigger = num_mins * 15;
  if( COUNT > trigger){
     digitalWrite( LED, HIGH);
  }
} // loop

No comments:

Post a Comment