Advertisement
pleasedontcode

Scanner rev_110

Nov 6th, 2023
82
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: Scanner
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2023-11-06 22:20:36
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20.  
  21. /****** SYSTEM REQUIREMENT 1 *****/
  22. /* if temperature is above 100°C so provide */
  23. /* information via serial monitor. */
  24.  
  25. /****** FUNCTION PROTOTYPES *****/
  26. void setup(void);
  27. void loop(void);
  28. void updateInputs(void);
  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(void);
  32.  
  33. /***** DEFINITION OF ANALOG INPUT PINS *****/
  34. const uint8_t sensor_PIN_A0 = A0;
  35.  
  36. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  37. const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_A0 = 3;
  38. const float voltage_Temperature_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_A0] PROGMEM =
  39. {
  40.     {0.0, 2.0, 3.0}, //Voltage [V]
  41.     {20.0, 60.0, 100.0} //Temperature [°C]
  42. };
  43.  
  44. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. unsigned int sensor_PIN_A0_rawData = 0; // Analog Input
  47.  
  48. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float sensor_PIN_A0_phyData = 0.0; // Temperature [°C]
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(sensor_PIN_A0, INPUT);
  56.     Serial.begin(9600); // Initialize serial communication
  57. }
  58.  
  59. void loop(void)
  60. {
  61.     // put your main code here, to run repeatedly:
  62.     updateInputs(); // Refresh input data
  63.     convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  64.  
  65.     // Check if temperature is above 100°C
  66.     if (sensor_PIN_A0_phyData > 100.0)
  67.     {
  68.         Serial.print("Temperature above 100°C: ");
  69.         Serial.print(sensor_PIN_A0_phyData);
  70.         Serial.println("°C");
  71.     }
  72.  
  73.     delay(1000); // Wait for 1 second
  74. }
  75.  
  76. void updateInputs()
  77. {
  78.     sensor_PIN_A0_rawData = analogRead(sensor_PIN_A0);
  79. }
  80.  
  81. /* BLOCK lookup_phyData_from_voltage */
  82. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  83. {
  84.     // Search table for appropriate value.
  85.     uint8_t index = 0;
  86.  
  87.     const float *voltagePointer = &voltage_phyData_lookup[0];
  88.     const float *phyDataPointer = &voltage_phyData_lookup[1];
  89.  
  90.     // Perform minimum and maximum voltage saturation based on characteristic curve
  91.     voltage = min(voltage, pgm_read_float(&voltagePointer[segment_points - 1]));
  92.     voltage = max(voltage, pgm_read_float(&voltagePointer[0]));
  93.  
  94.     while (pgm_read_float(&voltagePointer[index]) <= voltage && index < segment_points)
  95.         index++;
  96.  
  97.     // If index is zero, physical value is smaller than our table range
  98.     if (index == 0)
  99.     {
  100.         return map_f(voltage,
  101.                      pgm_read_float(&voltagePointer[0]), // X1
  102.                      pgm_read_float(&voltagePointer[1]), // X2
  103.                      pgm_read_float(&phyDataPointer[0]), // Y1
  104.                      pgm_read_float(&phyDataPointer[1])); // Y2
  105.     }
  106.     // If index is maxed out, phyisical value is larger than our range.
  107.     else if (index == segment_points)
  108.     {
  109.         return map_f(voltage,
  110.                      pgm_read_float(&voltagePointer[segment_points - 2]), // X1
  111.                      pgm_read_float(&voltagePointer[segment_points - 1]), // X2
  112.                      pgm_read_float(&phyDataPointer[segment_points - 2]), // Y1
  113.                      pgm_read_float(&phyDataPointer[segment_points - 1])); // Y2
  114.     }
  115.     // index is between 0 and max, just right
  116.     else
  117.     {
  118.         return map_f(voltage,
  119.                      pgm_read_float(&voltagePointer[index - 1]), // X1
  120.                      pgm_read_float(&voltagePointer[index]), // X2
  121.                      pgm_read_float(&phyDataPointer[index - 1]), // Y1
  122.                      pgm_read_float(&phyDataPointer[index])); // Y2
  123.     }
  124. }
  125. /* END BLOCK lookup_phyData_from_voltage */
  126.  
  127. /* BLOCK map_f */
  128. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  129. {
  130.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  131. }
  132. /* END BLOCK map_f */
  133.  
  134. /* BLOCK convertInputsFromRawToPhyData */
  135. void convertInputsFromRawToPhyData()
  136. {
  137.     float voltage = 0.0;
  138.  
  139.     voltage = sensor_PIN_A0_rawData * (5.0 / 1023.0);
  140.     sensor_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_A0, &(voltage_Temperature_PIN_A0_lookup[0][0]));
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement