Advertisement
pleasedontcode

Scanner rev_121

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