Advertisement
pleasedontcode

Scanner rev_119

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