src/cpp-sensor-module/MGMS-Sensor-Module/AnalogSensors.cpp
///////////////////////////////////////////////////////////////////////////////
// Reads Soil Temp values from ADC, reports the most likely temperature
Reads Soil Temp values from ADC, reports the most likely temperature
Reads Soil Temp values from ADC reports the most likely temperature
void UpdateSoilTemp(float* TempMode, char* ASCII)
{
    ADMUX = (0<<REFS1)|(1<<REFS0)|(0<<ADLAR)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|
            (0<<MUX0);   
    uint8_t i ;                         
    uint16_t DataH ;                    
    uint16_t DataL ;                    
    float DataFloat ;                   
    float RawValues[10] ;               
    uint8_t Counts[10] ;                
    uint8_t MaxCount ;                  
    for (i = 0 ; i<10 ; i++)
    {
        ADCSRA |= (1<<ADSC) ;           
        while( ADCSRA & (1<<ADSC)) ;    
        DataL = ADCL ;
        DataH = ADCH << 8 ;
        DataH = DataH + DataL ;
        DataFloat = DataH ;             
        RawValues[i] = ( ( (DataFloat / 1024)  *5 ) * 75.006 ) - 40 ;
    }
    qsort(RawValues, 10, sizeof(float), CompareFloat) ; 
    Counts[0] = 1 ;                                     
    for (i = 1 ; i<10 ; i++)                            
    {
        if ( RawValues[i] == RawValues[i-1] )
        {
            Counts[i] = Counts[i-1] + 1 ;
        }
        else
        {
            Counts[i] = 1 ;								
        }
    }
    MaxCount = 0 ;                                      
    for (i = 1 ; i<10 ; i++)                            
    {
        if ( Counts[i] > Counts[MaxCount] ) MaxCount = i ;
    }
    *TempMode = RawValues[MaxCount] ;
    dtostrf( RawValues[MaxCount], 6, 2, ASCII) ;
}
void UpdateSoilTemp ( float * TempMode , char * ASCII ) { ADMUX = ( 0 << REFS1 ) | ( 1 << REFS0 ) | ( 0 << ADLAR ) | ( 0 << MUX3 ) | ( 0 << MUX2 ) | ( 0 << MUX1 ) | ( 0 << MUX0 ) ; uint8_t i ; uint16_t DataH ; uint16_t DataL ; float DataFloat ; float RawValues [ 10 ] ; uint8_t Counts [ 10 ] ; uint8_t MaxCount ; for ( i = 0 ; i < 10 ; i ++ ) { ADCSRA |= ( 1 << ADSC ) ; while ( ADCSRA & ( 1 << ADSC ) ) ; DataL = ADCL ; DataH = ADCH << 8 ; DataH = DataH + DataL ; DataFloat = DataH ; RawValues [ i ] = ( ( ( DataFloat / 1024 ) * 5 ) * 75.006 ) - 40 ; } qsort ( RawValues , 10 , sizeof ( float ) , CompareFloat ) ; Counts [ 0 ] = 1 ; for ( i = 1 ; i < 10 ; i ++ ) { if ( RawValues [ i ] == RawValues [ i - 1 ] ) { Counts [ i ] = Counts [ i - 1 ] + 1 ; } else { Counts [ i ] = 1 ; } } MaxCount = 0 ; for ( i = 1 ; i < 10 ; i ++ ) { if ( Counts [ i ] > Counts [ MaxCount ] ) MaxCount = i ; } * TempMode = RawValues [ MaxCount ] ; dtostrf ( RawValues [ MaxCount ] , 6 , 2 , ASCII ) ; }
Reads Soil Temp values from ADC, reports the most likely temperature
Reads Soil Temp values from ADC reports the most likely temperature
// Select Analog Port 0 and Internal Reference Voltage // for loop counter // ADC High Bits // ADC Low Bits // Needed for int -> float cast // Samples for mode function // Occurrence of each value in RawData // Array location of the highest count // Take 10 measurements // Begin ADC conversion on Channel 0 // Wait for ADC conversion to complete // Cast to Float // Equation from THERM200 Datasheet // Begin finding mode of the measured data // Ascending sort // Count occurrence of each value // Count the first value // Count the rest // restart the count // Finds the largest count // Count the first value // Count the rest // Update the MaxCount as necessary