/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
/* Example code with timer intyerrutp that will create an interruption each
500ms using timer1 and prescalar of 256.
Calculations (for 500ms):
System clock 16 Mhz and Prescalar 256;
Timer 1 speed = 16Mhz/256 = 62.5 Khz
Pulse time = 1/62.5 Khz = 16us
Count up to = 500ms / 16us = 31250 (so this is the value the OCR register should have)
*/
void setupTimer()
{
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000100; //Set CS12 to 1 so we get prescalar 256
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 31250*/
OCR1A = 31250; //Finally we set compare register A to this value
sei();
}
bool isrState;
bool ledState;
void setup()
{
setupTimer();
isrState = false;
ledState = false;
digitalWrite(13, LOW);
}
void loop()
{
if(isrState)
{
if(ledState)
{
digitalWrite(13, LOW);
ledState = false;
}
else
{
digitalWrite(13, HIGH);
ledState = true;
}
isrState = false;
}
}
ISR(TIMER1_COMPA_vect)
{
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
isrState = true;
}