Advertisement
pleasedontcode

Scanner rev_113

Nov 6th, 2023
79
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 23:53:40
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20.  
  21. /****** FUNCTION PROTOTYPES *****/
  22. void updateInputs();
  23. float lookupPhyDataFromVoltage(float voltage, int segmentPoints, const float* voltagePhyDataLookup);
  24. float mapFloat(float x, float inMin, float inMax, float outMin, float outMax);
  25. void convertInputsFromRawToPhyData();
  26.  
  27. /***** DEFINITION OF ANALOG INPUT PINS *****/
  28. const uint8_t sensorPinA0 = A0;
  29.  
  30. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  31. const uint8_t segmentPointsVoltageTemperaturePinA0 = 3;
  32. const float voltageTemperaturePinA0Lookup[2][segmentPointsVoltageTemperaturePinA0] PROGMEM =
  33. {
  34.   {0.0, 2.0, 3.0},   // Voltage [V]
  35.   {20.0, 60.0, 100.0}   // Temperature [°C]
  36. };
  37.  
  38. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  39. unsigned int sensorPinA0RawData = 0; // Analog Input
  40.  
  41. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  42. float sensorPinA0PhyData = 0.0; // Temperature [°C]
  43.  
  44. void setup()
  45. {
  46.   // Put your setup code here, to run once:
  47.   pinMode(sensorPinA0, INPUT);
  48.   Serial.begin(9600); // Initialize serial communication
  49. }
  50.  
  51. void loop()
  52. {
  53.   // Put your main code here, to run repeatedly:
  54.   updateInputs(); // Refresh input data
  55.   convertInputsFromRawToPhyData(); // After that updateInput function is called, so raw data are transformed into physical data according to the characteristic curve
  56.  
  57.   // Check if temperature is above 100°C
  58.   if (sensorPinA0PhyData > 100.0)
  59.   {
  60.     Serial.println("Temperature is above 100°C");
  61.   }
  62.  
  63.   delay(1000); // Wait for 1 second before repeating the loop
  64. }
  65.  
  66. void updateInputs()
  67. {
  68.   sensorPinA0RawData = analogRead(sensorPinA0);
  69. }
  70.  
  71. float lookupPhyDataFromVoltage(float voltage, int segmentPoints, const float* voltagePhyDataLookup)
  72. {
  73.   // Search table for appropriate value.
  74.   uint8_t index = 0;
  75.  
  76.   const float *voltagePointer = &voltagePhyDataLookup[0];
  77.   const float *phyDataPointer = &voltagePhyDataLookup[segmentPoints];
  78.  
  79.   // Perform minimum and maximum voltage saturation based on characteristic curve
  80.   voltage = min(voltage, voltagePointer[segmentPoints - 1]);
  81.   voltage = max(voltage, voltagePointer[0]);
  82.  
  83.   while (pgm_read_float(voltagePointer[index]) <= voltage && index < segmentPoints)
  84.     index++;
  85.  
  86.   // If index is zero, physical value is smaller than our table range
  87.   if (index == 0)
  88.   {
  89.     return mapFloat(voltage,
  90.                     pgm_read_float(voltagePointer[0]),   // X1
  91.                     pgm_read_float(voltagePointer[1]),   // X2
  92.                     pgm_read_float(phyDataPointer[0]),   // Y1
  93.                     pgm_read_float(phyDataPointer[1])); // Y2
  94.   }
  95.   // If index is maxed out, physical value is larger than our range.
  96.   else if (index == segmentPoints)
  97.   {
  98.     return mapFloat(voltage,
  99.                     pgm_read_float(voltagePointer[segmentPoints - 2]),   // X1
  100.                     pgm_read_float(voltagePointer[segmentPoints - 1]),   // X2
  101.                     pgm_read_float(phyDataPointer[segmentPoints - 2]),   // Y1
  102.                     pgm_read_float(phyDataPointer[segmentPoints - 1])); // Y2
  103.   }
  104.   // index is between 0 and max, just right
  105.   else
  106.   {
  107.     return mapFloat(voltage,
  108.                     pgm_read_float(voltagePointer[index - 1]), // X1
  109.                     pgm_read_float(voltagePointer[index]),    // X2
  110.                     pgm_read_float(phyDataPointer[index - 1]), // Y1
  111.                     pgm_read_float(phyDataPointer[index]));  // Y2
  112.   }
  113. }
  114.  
  115. float mapFloat(float x, float inMin, float inMax, float outMin, float outMax)
  116. {
  117.   return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
  118. }
  119.  
  120. void convertInputsFromRawToPhyData()
  121. {
  122.   float voltage = 0.0;
  123.  
  124.   voltage = sensorPinA0RawData * (5.0 / 1023.0);
  125.   sensorPinA0PhyData = lookupPhyDataFromVoltage(voltage, segmentPointsVoltageTemperaturePinA0, &(voltageTemperaturePinA0Lookup[0][0]));
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement