src/cpp-sensor-module/MGMS-Sensor-Module/AnalogSensors.cpp
void UpdateSoilTemp(float* TempMode, char* ASCII)
{
    ADMUX = (0<<REFS1)|(1<<REFS0)|(0<<ADLAR)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|
            (0<<MUX0);   // Select Analog Port 0 and Internal Reference Voltage

    uint8_t i ;                         // for loop counter
    uint16_t DataH ;                    // ADC High Bits
    uint16_t DataL ;                    // ADC Low Bits
    float DataFloat ;                   // Needed for int -> float cast
    float RawValues[10] ;               // Samples for mode function

    uint8_t Counts[10] ;                // Occurrence of each value in RawData
    uint8_t MaxCount ;                  // Array location of the highest count

    // Take 10 measurements
    for (i = 0 ; i<10 ; i++)
    {
        ADCSRA |= (1<<ADSC) ;           // Begin ADC conversion on Channel 0
        while( ADCSRA & (1<<ADSC)) ;    // Wait for ADC conversion to complete

        DataL = ADCL ;
        DataH = ADCH << 8 ;
        DataH = DataH + DataL ;

        DataFloat = DataH ;             // Cast to Float
        RawValues[i] = ( ( (DataFloat / 1024)  *5 ) * 75.006 ) - 40 ;
                                        // Equation from THERM200 Datasheet
    }

    // Begin finding mode of the measured data

    qsort(RawValues, 10, sizeof(float), CompareFloat) ; // Ascending sort
    // Count occurrence of each value
    Counts[0] = 1 ;                                     // Count the first value
    for (i = 1 ; i<10 ; i++)                            // Count the rest
    {
        if ( RawValues[i] == RawValues[i-1] )
        {
            Counts[i] = Counts[i-1] + 1 ;
        }
        else
        {
            Counts[i] = 1 ;								// restart the count
        }
    }

    // Finds the largest count
    MaxCount = 0 ;                                      // Count the first value
    for (i = 1 ; i<10 ; i++)                            // Count the rest
    {
        if ( Counts[i] > Counts[MaxCount] ) MaxCount = i ;
        // Update the MaxCount as necessary
    }

    *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
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