Advertisement
pleasedontcode

Scanner rev_118

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