Advertisement
pleasedontcode

Scanner rev_123

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