volatile bool convDone;
volatile unsigned int value;
volatile unsigned long lastTime, convTime;
void setup() {
Serial.begin (115200);
Serial.println ("ADC Interrupt Example");
// ADC Setup
ADCSRA = 0b10001111; // Enable ADC & Interrupt, pre-scaler = 128
ADMUX = 0b01000000; // Use AVCC and A0 as input to ADC
sei(); // Set global interrupt flag
convDone = true;
}
void loop() {
if (convDone) {
// start converting voltage on A0
ADCSRA |= bit(ADSC) | bit(ADIE);
lastTime = micros();
convDone = false;
}
static unsigned long printTime;
if(millis() - printTime > 500) {
printTime = millis();
Serial.print(convTime);
Serial.print("uS - Val: ");
Serial.println(value);
}
}
// ADC Interrupt Service Routine
ISR(ADC_vect) {
convTime = micros() - lastTime;
value = ADC;
convDone = true;
}