Advertisement
pleasedontcode

Scanner rev_122

Nov 7th, 2023
51
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-07 11:50:57
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <Wire.h>
  21. #include <LiquidCrystal_I2C.h>
  22.  
  23. /****** SYSTEM REQUIREMENT 1 *****/
  24. /* Display the data read from sensor and pot */
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void updateInputs(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. void convertInputsFromRawToPhyData(void);
  33.  
  34. /***** DEFINITION OF ANALOG INPUT PINS *****/
  35. const uint8_t sensore_PIN_A0 = A0;
  36. const uint8_t pot_Potentiometer_Vout_PIN_A1 = A1;
  37.  
  38. /***** DEFINITION OF I2C PINS *****/
  39. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_D20 = 20;
  40. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_D21 = 21;
  41. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  42.  
  43. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  44. const uint8_t SEGMENT_POINTS_voltage_Temperature_PIN_A0 = 3;
  45. const float voltage_Temperature_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Temperature_PIN_A0] PROGMEM =
  46. {
  47.   {0.0, 2.0, 3.0}, // Voltage [V]
  48.   {20.0, 60.0, 100.0} // Temperature [°C]
  49. };
  50.  
  51. const uint8_t SEGMENT_POINTS_voltage_rotazione_PIN_A1 = 2;
  52. const float voltage_rotazione_PIN_A1_lookup[2][SEGMENT_POINTS_voltage_rotazione_PIN_A1] PROGMEM =
  53. {
  54.   {0.0, 5.0}, // Voltage [V]
  55.   {0.0, 360.0} // rotazione [gradi]
  56. };
  57.  
  58.  
  59. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  60. /***** used to store raw data *****/
  61. unsigned int sensore_PIN_A0_rawData = 0; // Analog Input
  62. unsigned int pot_Potentiometer_Vout_PIN_A1_rawData = 0; // Analog Input
  63.  
  64. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  65. /***** used to store data after characteristic curve transformation *****/
  66. float sensore_PIN_A0_phyData = 0.0; // Temperature [°C]
  67. float pot_Potentiometer_Vout_PIN_A1_phyData = 0.0; // rotazione [gradi]
  68.  
  69. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  70. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);
  71.  
  72. void setup(void)
  73. {
  74.   // put your setup code here, to run once:
  75.  
  76.   pinMode(sensore_PIN_A0, INPUT);
  77.   pinMode(pot_Potentiometer_Vout_PIN_A1, INPUT);
  78.  
  79.   lcd.init();
  80.   lcd.backlight();
  81. }
  82.  
  83.  
  84. void loop(void)
  85. {
  86.   // put your main code here, to run repeatedly:
  87.  
  88.   updateInputs(); // Refresh input data
  89.  
  90.   convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  91.  
  92. }
  93.  
  94.  
  95. void updateInputs(void)
  96. {
  97.   sensore_PIN_A0_rawData = analogRead(sensore_PIN_A0);
  98.   pot_Potentiometer_Vout_PIN_A1_rawData = analogRead(pot_Potentiometer_Vout_PIN_A1);
  99. }
  100.  
  101. /* BLOCK lookup_phyData_from_voltage */
  102. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  103. {
  104.     // Search table for appropriate value.
  105.     uint8_t index = 0;
  106.  
  107.     const float *voltagePointer = &voltage_phyData_lookup[0];
  108.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  109.  
  110.     // Perform minimum and maximum voltage saturation based on characteristic curve
  111.     voltage = min(voltage, voltagePointer[segment_points-1]);
  112.     voltage = max(voltage, voltagePointer[0]);
  113.  
  114.     while( pgm_read_float(voltagePointer[index]) <= voltage && index < segment_points )
  115.         index++;
  116.  
  117.     // If index is zero, physical value is smaller than our table range
  118.     if( index==0 )
  119.     {
  120.         return map_f( voltage,
  121.             pgm_read_float(voltagePointer[0]),   // X1
  122.             pgm_read_float(voltagePointer[1]),   // X2
  123.             pgm_read_float(phyDataPointer[0]),   // Y1
  124.             pgm_read_float(phyDataPointer[1]) ); // Y2
  125.     }
  126.     // If index is maxed out, phyisical value is larger than our range.
  127.     else if( index==segment_points )
  128.     {
  129.         return map_f( voltage,
  130.             pgm_read_float(voltagePointer[segment_points-2]),   // X1
  131.             pgm_read_float(voltagePointer[segment_points-1]),   // X2
  132.             pgm_read_float(phyDataPointer[segment_points-2]),   // Y1
  133.             pgm_read_float(phyDataPointer[segment_points-1]) ); // Y2
  134.     }
  135.     // index is between 0 and max, just right
  136.     else
  137.     {
  138.         return map_f( voltage,
  139.             pgm_read_float(voltagePointer[index-1]), // X1
  140.             pgm_read_float(voltagePointer[index]),    // X2
  141.             pgm_read_float(phyDataPointer[index-1]), // Y1
  142.             pgm_read_float(phyDataPointer[index]) );  // Y2
  143.     }
  144. }
  145. /* END BLOCK lookup_phyData_from_voltage */
  146.  
  147. /* BLOCK map_f */
  148. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  149. {
  150.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  151. }
  152. /* END BLOCK map_f */
  153.  
  154. /* BLOCK convertInputsFromRawToPhyData */
  155. void convertInputsFromRawToPhyData()
  156. {
  157.     float voltage = 0.0;
  158.  
  159.     voltage = sensore_PIN_A0_rawData * (5.0 / 1023.0);
  160.     sensore_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_A0, &(voltage_Temperature_PIN_A0_lookup[0][0]));
  161.  
  162.     voltage = pot_Potentiometer_Vout_PIN_A1_rawData * (5.0 / 1023.0);
  163.     pot_Potentiometer_Vout_PIN_A1_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_rotazione_PIN_A1, &(voltage_rotazione_PIN_A1_lookup[0][0]));
  164.  
  165. }
  166. /* END BLOCK convertInputsFromRawToPhyData */
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement