Advertisement
pleasedontcode

"Temperature Conversion" rev_01

Jun 4th, 2024
430
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: Arduino Uno
  14.     - Source Code created on: 2024-06-05 00:51:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* convert voltage in temperature */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24.  
  25. /****** FUNCTION PROTOTYPES *****/
  26. void setup(void);
  27. void loop(void);
  28. void updateInputs(void);
  29. void convertInputsFromRawToPhyData(void);
  30. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
  31. float map_f(float x, float in_min, float in_max, float out_min, float out_max);
  32.  
  33. /***** DEFINITION OF ANALOG INPUT PINS *****/
  34. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  35.  
  36. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  37. const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_A0 = 2;
  38. const float voltage_Temperature_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_A0] =
  39. {
  40.     {0.3, 0.5},  // Voltage [V]
  41.     {0.2, 0.4}   // Temperature [°C]
  42. };
  43.  
  44. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. unsigned int pot_Potentiometer_Vout_PIN_A0_rawData = 0; // Analog Input
  47.  
  48. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float pot_Potentiometer_Vout_PIN_A0_phyData = 0.0; // Temperature [°C]
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // put your main code here, to run repeatedly:
  61.     updateInputs(); // Refresh input data
  62.     convertInputsFromRawToPhyData(); // Transform raw data to physical data according to characteristic curve
  63. }
  64.  
  65. void updateInputs()
  66. {
  67.     pot_Potentiometer_Vout_PIN_A0_rawData = analogRead(pot_Potentiometer_Vout_PIN_A0);
  68. }
  69.  
  70. /* BLOCK lookup_phyData_from_voltage */
  71. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  72. {
  73.     // Search table for appropriate value.
  74.     uint8_t index = 0;
  75.  
  76.     const float *voltagePointer = &voltage_phyData_lookup[0];
  77.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  78.  
  79.     // Perform minimum and maximum voltage saturation based on characteristic curve
  80.     voltage = min(voltage, voltagePointer[segment_points-1]);
  81.     voltage = max(voltage, voltagePointer[0]);
  82.  
  83.     while( voltagePointer[index] <= voltage && index < segment_points )
  84.         index++;
  85.  
  86.     // If index is zero, physical value is smaller than our table range
  87.     if( index==0 )
  88.     {
  89.         return map_f( voltage,
  90.             voltagePointer[0],   // X1
  91.             voltagePointer[1],   // X2
  92.             phyDataPointer[0],   // Y1
  93.             phyDataPointer[1] ); // Y2
  94.     }
  95.     // If index is maxed out, phyisical value is larger than our range.
  96.     else if( index==segment_points )
  97.     {
  98.         return map_f( voltage,
  99.             voltagePointer[segment_points-2],   // X1
  100.             voltagePointer[segment_points-1],   // X2
  101.             phyDataPointer[segment_points-2],   // Y1
  102.             phyDataPointer[segment_points-1] ); // Y2
  103.     }
  104.     // index is between 0 and max, just right
  105.     else
  106.     {
  107.         return map_f( voltage,
  108.             voltagePointer[index-1], // X1
  109.             voltagePointer[index],    // X2
  110.             phyDataPointer[index-1], // Y1
  111.             phyDataPointer[index] );  // Y2
  112.     }
  113. }
  114. /* END BLOCK lookup_phyData_from_voltage */
  115.  
  116. /* BLOCK map_f */
  117. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  118. {
  119.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  120. }
  121. /* END BLOCK map_f */
  122.  
  123. /* BLOCK convertInputsFromRawToPhyData */
  124. void convertInputsFromRawToPhyData()
  125. {
  126.     float voltage = 0.0;
  127.  
  128.     voltage = pot_Potentiometer_Vout_PIN_A0_rawData * (5.0 / 1023.0);
  129.     pot_Potentiometer_Vout_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_A0, &(voltage_Temperature_PIN_A0_lookup[0][0]));
  130.  
  131. }
  132. /* END BLOCK convertInputsFromRawToPhyData */
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement