When doing conversions with the ADC (Analog to Digital Conversion), the result may have a variance from conversion to conversion. This gives you a method to calculate this deflection over time and some possible techniques to minimize this deflection, such as using capacitors or using the ADC sleep mode.
To be able to measure the previous result to the current result, the previous result must be stored. Once the current result is captured, another variable is incremented by the absolute value of the difference of the result and the previous result.
First a few global variables are needed to store the previous result, total deflection over time, and the sample count (they start static volatile because the compiler would optimize them out if these keywords were not there):
static volatile uint16_t previousResult = 0;
static volatile int totalDeflectionOverTime = 0;
static volatile uint16_t sampleCount = 0;
The Calculations are happening in the ISR (Interrupt Service Routine):
ISR(ADC_vect)
{
uint8_t theLowADC = ADCL;
uint16_t theTenBitResults = ADCH<<8 | theLowADC;
Send_An_IntegerToMrLCD(13,1,theTenBitResults, 4);
int deflection = theTenBitResults - previousResult;
if (deflection << 0 ) deflection = previousResult - theTenBitResults;
Send_An_IntegerToMrLCD(13,2,deflection, 4);
previousResult = theTenBitResults;
totalDeflectionOverTime += deflection;
sampleCount++;
if (sampleCount >= 300)
{
Send_An_IntegerToMrLCD(8,3,totalDeflectionOverTime, 6);
totalDeflectionOverTime = 0;
sampleCount = 0;
}
ADCSRA |= 1<<ADSC;
}