CCCPaste

Temperature Conversion rev_05

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Temperature Conversion
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-05-15 11:27:09

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read temperatura and print on serial */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateInputs();
float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
float map_f(float x, float in_min, float in_max, float out_min, float out_max);
void convertInputsFromRawToPhyData();

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t temperatura_PIN_D13 = 13; // Changed D13 to 13

/****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_D13 = 19;
const float voltage_Temperature_PIN_D13_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_D13] =
{
    {0.84, 0.93, 1.01, 1.1, 1.15, 1.24, 1.33, 1.42, 1.51, 1.58, 1.66, 1.74, 1.83, 1.91, 1.99, 2.08, 2.16, 2.23, 2.26}, // Voltage [V]
    {90.0, 85.0, 80.0, 75.0, 70.0, 65.0, 60.0, 55.0, 50.0, 45.0, 40.0, 35.0, 30.0, 25.0, 20.0, 15.0, 10.0, 7.0, 5.0} // Temperature [°C]
};

/***** DEFINITION OF INPUT RAW VARIABLES *****/
/***** used to store raw data *****/
unsigned int temperatura_PIN_D13_rawData = 0; // Analog Input

/***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float temperatura_PIN_D13_phyData = 0.0; // Temperature [°C]

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(temperatura_PIN_D13, INPUT);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateInputs(); // Refresh input data
    convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
}

void updateInputs()
{
    temperatura_PIN_D13_rawData = analogRead(temperatura_PIN_D13);
}

float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
{
    // Search table for appropriate value.
    uint8_t index = 0;

    const float *voltagePointer = &voltage_phyData_lookup[0];
    const float *phyDataPointer = &voltage_phyData_lookup[segment_points];

    // Perform minimum and maximum voltage saturation based on characteristic curve
    voltage = min(voltage, voltagePointer[segment_points-1]);
    voltage = max(voltage, voltagePointer[0]);

    while( voltagePointer[index] <= voltage && index < segment_points )
        index++;

    // If index is zero, physical value is smaller than our table range
    if( index==0 )
    {
        return map_f( voltage,
            voltagePointer[0],   // X1
            voltagePointer[1],   // X2
            phyDataPointer[0],   // Y1
            phyDataPointer[1] ); // Y2
    }
    // If index is maxed out, phyisical value is larger than our range.
    else if( index==segment_points )
    {
        return map_f( voltage,
            voltagePointer[segment_points-2],   // X1
            voltagePointer[segment_points-1],   // X2
            phyDataPointer[segment_points-2],   // Y1
            phyDataPointer[segment_points-1] ); // Y2
    }
    // index is between 0 and max, just right
    else
    {
        return map_f( voltage,
            voltagePointer[index-1], // X1
            voltagePointer[index],    // X2
            phyDataPointer[index-1], // Y1
            phyDataPointer[index] );  // Y2
    }
}

float map_f(float x, float in_min, float in_max, float out_min, float out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void convertInputsFromRawToPhyData()
{
    float voltage = 0.0;

    voltage = temperatura_PIN_D13_rawData * (3.3 / 1023.0);
    temperatura_PIN_D13_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_D13, &(voltage_Temperature_PIN_D13_lookup[0][0]));
}

/* END CODE */