I am trying to measure AC power using an Arduino with an Atmega328p. In summary, I want to use timer1 to make an interrupt every 400us where I measure two ADC channels (A0 and A1) and put them into an array. The problem is that instead of measuring five mains periods (mains frequency = 50Hz so 20ms * 5), I get instead 13 of these periods (260ms). What am I doing wrong?
volatile int sample_incremental = 0;
void setup() {
Serial.begin(115200);
pinMode(voltagePin, INPUT);
pinMode(currentPin, INPUT);
set_sample_interrupt();
sei();
}
void set_sample_interrupt() {
//Count to 400us
OCR1A = 99;
//Mode 4, CTC
TCCR1B |= (1 << WGM12);
//Enable interrupt
TIMSK1 |= (1 << OCIE1A);
//Prescaler 64
TCCR1B |= (1 << CS11) | (1 << CS10);
}
ISR (TIMER1_COMPA_vect) {
// action to be done every 400 usec
current_sample[sample_incremental] = analogRead(currentPin) - 504;
voltage_sample[sample_incremental] = analogRead(voltagePin) - 512;
if (sample_incremental < (SAMPLES * PERIODREADS))
sample_incremental++;
else {
//Disable interrupt
TIMSK1 &= ~(1 << OCIE1A);
sample_incremental = 0;
finishedReading = true;
}
}
Where after I do one reading, I stop the interrupt, and SAMPLES * PERIODREADS = 250.