Advertisement
pleasedontcode

Scanner rev_120

Nov 7th, 2023
54
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 09:40:42
  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. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  59. /***** used to store raw data *****/
  60. unsigned int sensore_PIN_A0_rawData = 0; // Analog Input
  61. unsigned int pot_Potentiometer_Vout_PIN_A1_rawData = 0; // Analog Input
  62.  
  63. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  64. /***** used to store data after characteristic curve transformation *****/
  65. float sensore_PIN_A0_phyData = 0.0; // Temperature [°C]
  66. float pot_Potentiometer_Vout_PIN_A1_phyData = 0.0; // rotazione [gradi]
  67.  
  68. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  69. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);
  70.  
  71. void setup(void)
  72. {
  73.     // put your setup code here, to run once:
  74.  
  75.     pinMode(sensore_PIN_A0, INPUT);
  76.     pinMode(pot_Potentiometer_Vout_PIN_A1, INPUT);
  77.  
  78.     lcd.init();
  79.     lcd.backlight();
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     // put your main code here, to run repeatedly:
  85.  
  86.     updateInputs(); // Refresh input data
  87.  
  88.     convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  89.  
  90. }
  91.  
  92. void updateInputs(void)
  93. {
  94.     sensore_PIN_A0_rawData = analogRead(sensore_PIN_A0);
  95.     pot_Potentiometer_Vout_PIN_A1_rawData = analogRead(pot_Potentiometer_Vout_PIN_A1);
  96. }
  97.  
  98. /* BLOCK lookup_phyData_from_voltage */
  99. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  100. {
  101.     // Search table for appropriate value.
  102.     uint8_t index = 0;
  103.  
  104.     const float *voltagePointer = &voltage_phyData_lookup[0];
  105.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  106.  
  107.     // Perform minimum and maximum voltage saturation based on characteristic curve
  108.     voltage = min(voltage, voltagePointer[segment_points-1]);
  109.     voltage = max(voltage, voltagePointer[0]);
  110.  
  111.     while( pgm_read_float(voltagePointer[index]) <= voltage && index < segment_points )
  112.         index++;
  113.  
  114.     // If index is zero, physical value is smaller than our table range
  115.     if( index==0 )
  116.     {
  117.         return map_f( voltage,
  118.             pgm_read_float(voltagePointer[0]),   // X1
  119.             pgm_read_float(voltagePointer[1]),   // X2
  120.             pgm_read_float(phyDataPointer[0]),   // Y1
  121.             pgm_read_float(phyDataPointer[1]) ); // Y2
  122.     }
  123.     // If index is maxed out, phyisical value is larger than our range.
  124.     else if( index==segment_points )
  125.     {
  126.         return map_f( voltage,
  127.             pgm_read_float(voltagePointer[segment_points-2]),   // X1
  128.             pgm_read_float(voltagePointer[segment_points-1]),   // X2
  129.             pgm_read_float(phyDataPointer[segment_points-2]),   // Y1
  130.             pgm_read_float(phyDataPointer[segment_points-1]) ); // Y2
  131.     }
  132.     // index is between 0 and max, just right
  133.     else
  134.     {
  135.         return map_f( voltage,
  136.             pgm_read_float(voltagePointer[index-1]), // X1
  137.             pgm_read_float(voltagePointer[index]),    // X2
  138.             pgm_read_float(phyDataPointer[index-1]), // Y1
  139.             pgm_read_float(phyDataPointer[index]) );  // Y2
  140.     }
  141. }
  142. /* END BLOCK lookup_phyData_from_voltage */
  143.  
  144. /* BLOCK map_f */
  145. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  146. {
  147.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  148. }
  149. /* END BLOCK map_f */
  150.  
  151. /* BLOCK convertInputsFromRawToPhyData */
  152. void convertInputsFromRawToPhyData()
  153. {
  154.     float voltage = 0.0;
  155.  
  156.     voltage = sensore_PIN_A0_rawData * (5.0 / 1023.0);
  157.     sensore_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Temperature_PIN_A0, &(voltage_Temperature_PIN_A0_lookup[0][0]));
  158.  
  159.     voltage = pot_Potentiometer_Vout_PIN_A1_rawData * (5.0 / 1023.0);
  160.     pot_Potentiometer_Vout_PIN_A1_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_rotazione_PIN_A1, &(voltage_rotazione_PIN_A1_lookup[0][0]));
  161.  
  162. }
  163. /* END BLOCK convertInputsFromRawToPhyData */
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement