volatile int resistorValue = 10;
volatile int timerValue = 0;
volatile byte STATE = HIGH;
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(9, OUTPUT);
// Enable internal pullup on INT0 / PIN2
pinMode(2, INPUT_PULLUP);
delay(1);
// Disable all interrupts
noInterrupts();
// Configure timer
TCCR1A = 0; // Turn off `Compare Output Mode`
TCCR1B = 0; // Turn off `Timer`, Reset Clock
TCNT1 = 0; // Reset `Clock Count`
// Set the prescaler to 1024
TCCR1B |= (1 << CS12) | (1 << CS10);
// Set compare value (16 000 000 / 1 024)
OCR1A = 15625 / 1000;
// Configure interrupt to happen on falling edge
EICRA |= (1 << ISC01);
// Enable external interrupt 0 (pin 2);
EIMSK |= (1 << INT0);
// CTC = Clear Timer On Compare
TCCR1B |= (1 << WGM12);
// Turn on `Output Compare Interrupt`
TIMSK1 |= (1 << OCIE1A);
// Enable interrupts
interrupts();
}
void loop()
{
delay(100);
// Here goes nothing
}
ISR(TIMER1_COMPA_vect) {
timerValue++;
if(timerValue >= resistorValue) {
STATE = !STATE;
digitalWrite(9, STATE);
timerValue = 0;
}
}
ISR(INT0_vect) {
resistorValue = analogRead(A0);
}
Comments