shomikc
Member level 4
Hi.
I am new to Arduino. I am not able to understand how to get 0.125 Hz with Timer1. I know it has something to do with Prescaling
but I could not find any documentation on the internet. The problem is to blink built-in LED at 1 Hz if a button is not pressed and
to blink the LED at 0.125 Hz if the button is pressed. Please help. If you can point me to any good document on Timer1 calculations
that would be very helpful. Thankyou.
I am new to Arduino. I am not able to understand how to get 0.125 Hz with Timer1. I know it has something to do with Prescaling
but I could not find any documentation on the internet. The problem is to blink built-in LED at 1 Hz if a button is not pressed and
to blink the LED at 0.125 Hz if the button is pressed. Please help. If you can point me to any good document on Timer1 calculations
that would be very helpful. Thankyou.
Code:
const int button_pin_number = 2;
const int led_pin_number = 13;
int button_unpressed_pressed = 0;
void setup() {
pinMode(led_pin_number, OUTPUT);
pinMode(button_pin_number, INPUT);
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << TOIE1);
interrupts();
}
ISR(TIMER1_COMPA_vect)
{
button_unpressed_pressed = digitalRead(button_pin_number);
if (button_unpressed_pressed == LOW)
{ OCR1A = 15624;
digitalWrite(led_pin_number, digitalRead(led_pin_number) ^ 1);
}
if (button_unpressed_pressed == HIGH)
{ OCR1A = 124992;
digitalWrite(led_pin_number, digitalRead(led_pin_number) ^ 1);
}
}
void loop()
{
}