Advertisement
pleasedontcode

Temperature Conversion rev_05

May 15th, 2024
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Temperature Conversion
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-05-15 11:27:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read temperatura and print on serial */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24.  
  25. /****** FUNCTION PROTOTYPES *****/
  26. void setup(void);
  27. void loop(void);
  28. void updateInputs();
  29. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
  30. float map_f(float x, float in_min, float in_max, float out_min, float out_max);
  31. void convertInputsFromRawToPhyData();
  32.  
  33. /***** DEFINITION OF ANALOG INPUT PINS *****/
  34. const uint8_t temperatura_PIN_D13 = 13; // Changed D13 to 13
  35.  
  36. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  37. const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_D13 = 19;
  38. const float voltage_Temperature_PIN_D13_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_D13] =
  39. {
  40.     {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]
  41.     {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]
  42. };
  43.  
  44. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. unsigned int temperatura_PIN_D13_rawData = 0; // Analog Input
  47.  
  48. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float temperatura_PIN_D13_phyData = 0.0; // Temperature [°C]
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(temperatura_PIN_D13, INPUT);
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // put your main code here, to run repeatedly:
  61.     updateInputs(); // Refresh input data
  62.     convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  63. }
  64.  
  65. void updateInputs()
  66. {
  67.     temperatura_PIN_D13_rawData = analogRead(temperatura_PIN_D13);
  68. }
  69.  
  70. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  71. {
  72.     // Search table for appropriate value.
  73.     uint8_t index = 0;
  74.  
  75.     const float *voltagePointer = &voltage_phyData_lookup[0];
  76.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  77.  
  78.     // Perform minimum and maximum voltage saturation based on characteristic curve
  79.     voltage = min(voltage, voltagePointer[segment_points-1]);
  80.     voltage = max(voltage, voltagePointer[0]);
  81.  
  82.     while( voltagePointer[index] <= voltage && index < segment_points )
  83.         index++;
  84.  
  85.     // If index is zero, physical value is smaller than our table range
  86.     if( index==0 )
  87.     {
  88.         return map_f( voltage,
  89.             voltagePointer[0],   // X1
  90.             voltagePointer[1],   // X2
  91.             phyDataPointer[0],   // Y1
  92.             phyDataPointer[1] ); // Y2
  93.     }
  94.     // If index is maxed out, phyisical value is larger than our range.
  95.     else if( index==segment_points )
  96.     {
  97.         return map_f( voltage,
  98.             voltagePointer[segment_points-2],   // X1
  99.             voltagePointer[segment_points-1],   // X2
  100.             phyDataPointer[segment_points-2],   // Y1
  101.             phyDataPointer[segment_points-1] ); // Y2
  102.     }
  103.     // index is between 0 and max, just right
  104.     else
  105.     {
  106.         return map_f( voltage,
  107.             voltagePointer[index-1], // X1
  108.             voltagePointer[index],    // X2
  109.             phyDataPointer[index-1], // Y1
  110.             phyDataPointer[index] );  // Y2
  111.     }
  112. }
  113.  
  114. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  115. {
  116.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  117. }
  118.  
  119. void convertInputsFromRawToPhyData()
  120. {
  121.     float voltage = 0.0;
  122.  
  123.     voltage = temperatura_PIN_D13_rawData * (3.3 / 1023.0);
  124.     temperatura_PIN_D13_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_D13, &(voltage_Temperature_PIN_D13_lookup[0][0]));
  125. }
  126.  
  127. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement