#define LED_ONE PORTD ^= (1<<DDD5)
int period; // declare period as global variable
ISR(INT0_vect){ // interrupt service routine for pin change interrupt request 2
LED_ONE;
if (!(PIND & (1<<PD2))){ // if pin 2 is LOW, not pressed
period = TCNT1*.0000625*1024; // convert clock ticks to milliseconds. 1/16M = 0.0000625 (time of a normal tick)
Serial.println(period);
}else{
TCNT1 = 0; // reset the timer to 0
Serial.println("0");
}
}
int main(void)
{
// setup code that only runs once
DDRD |= (1<<DDD5); //Set PD 5 as output
DDRD &= (~(1<<DDD2)); // set pin 2 as input
PORTD |= (1<<PD2) ; //Enables pull-up resistors
//EIMSK 00000001, External Interrupt Request 0 Enable
EIMSK |= (1<<INT0);
//EICRA 00000001, Any logical change on INT0 generates an interrupt request.
EICRA |= (1<<ISC00);
EICRA &= (~(1<<ISC01)); // Push to lit
sei(); // enable interrupts
PORTD &= (~(1<<PD5));
TCCR1A = 0b00000000; // set timer 1 to normal mode
TCCR1B = 0b00000101; // and set prescaler to 1024
Serial.begin(9600); // initialize serial
while(1){
// code that loops forever
}
}