What does this line do
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
<<<< UPDATE TO THIS POST: >>>>
After some discussion, Robin2 looked up the correct answer to the above question in the manual, and reported below in post #10:
ADCSRA |= (1 << ADIE);
will cause an interrupt to happen when an analog reading completes
ADCSRA &= (0 << ADIE);
should stop that happening
So my answer below is a little wrong, but I must leave it to keep the logic of this thread intact.
And it now seems I need a small correction to the above correction: GoForSmoke in comment #14 below states that
ADCSRA &= (0 << ADIE);
won’t work and should have been
ADCSRA &= !(1 << ADIE);
“Because 0 left shifted … is still 0.”
But then starting in post #15, people start pointing out that the line above is also wrong and won’t work, because it uses the “!” (logical not) instead of “~” (ones complement) to reverse the bit. Meaning the line should have been:
ADCSRA &= ~(1 << ADIE);
ADCSRA &= ~ bit (ADIE); // for clearity
bitClear (ADCSRA, ADIE); // which makes the most sense to me.
A related fact I discovered experimentally after writing these posts, is that you cannot use
analogRead() when the analog pins have been setup to work directly with interrupts as in the above. If you do, execution of your loop() will hang on the analogRead() line, even though the interrupts will continue working just fine.
<<<< END OF UPDATE TO THIS POST >>>>
I didn’t show it’s use here; but, in another mode of the full sketch, the analog value of pin A0 is read repeatedly within the interrupt by the following line:
The line you asked about tells the Analog-to-Digital converter to discard the current value and take a new mesurement, to be ready for the next interrupt. And, letting the interrupt read the analog pin directly is much faster than would be repeatedly calling the line:
buffA[buffCnt] = analogRead(A0);
a line which doesn’t even begin to do an A/D conversion until called, making the interrupt take longer to complete.
Posted by DrAzzy:
You could always just disable the interrupt with the ADIE (Analog Digital converter Interrupt Enable) bit of ADCSRA.
I think that’s the answer DrAzzy. That should prevent time wasted by A/D conversion as well. Thanks.