Advertisement
pleasedontcode

"Arduino Setup+" rev_01

Dec 22nd, 2023
79
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: "Arduino Setup+"
  13.     - Source Code compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2023-12-22 12:22:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Check if digital data is received from bluetooth */
  21.     /* and and use this data into MCP4725. */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* Read currentAcquisition and send it via bluetooth. */
  24.     /* The data to be send has to be integer. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <SoftwareSerial.h>
  30. #include <Wire.h>
  31. #include <Adafruit_MCP4725.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateInputs(void);
  37. void convertInputsFromRawToPhyData(void);
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t currentAcquisition_PIN_A0 = A0;
  41.  
  42. /***** DEFINITION OF Software Serial *****/
  43. const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1 = A1;
  44. const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2 = A2;
  45. SoftwareSerial DX_BT27_Slave_HC05_mySerial(DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2, DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1);
  46.  
  47. /***** DEFINITION OF I2C PINS *****/
  48. const uint8_t MCP4725_PIN_SDA_A4 = A4;
  49. const uint8_t MCP4725_PIN_SCL_A5 = A5;
  50.  
  51. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  52. const uint8_t SEGMENT_POINTS_voltage_Current_PIN_A0 = 2;
  53. const float voltage_Current_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Current_PIN_A0] = {
  54.   {0.0, 5.0},  //Voltage [V]
  55.   {0.0, 500.0} //Current [A]
  56. };
  57.  
  58. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  59. /***** used to store raw data *****/
  60. unsigned int currentAcquisition_PIN_A0_rawData = 0; // Analog Input
  61.  
  62. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  63. /***** used to store data after characteristic curve transformation *****/
  64. float currentAcquisition_PIN_A0_phyData = 0.0; // Current [A]
  65.  
  66. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  67. Adafruit_MCP4725 dac;
  68.  
  69. void setup(void) {
  70.   // put your setup code here, to run once:
  71.  
  72.   pinMode(currentAcquisition_PIN_A0, INPUT);
  73.  
  74.   DX_BT27_Slave_HC05_mySerial.begin(9600);
  75.  
  76.   dac.begin(MCP4725_I2CADDR_DEFAULT, &Wire); // Initialize DAC with default I2C address and Wire object
  77. }
  78.  
  79. void loop(void) {
  80.   // put your main code here, to run repeatedly:
  81.  
  82.   updateInputs(); // Refresh input data
  83.  
  84.   convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed into physical data according to the characteristic curve
  85.  
  86.   // Check if digital data is received from bluetooth and use this data into MCP4725
  87.   if (DX_BT27_Slave_HC05_mySerial.available()) {
  88.     int data = DX_BT27_Slave_HC05_mySerial.parseInt();
  89.     dac.setVoltage(data, true); // Set the voltage output of the DAC and write to EEPROM
  90.   }
  91.  
  92.   // Read currentAcquisition and send it via bluetooth
  93.   int currentAcquisitionInt = (int)currentAcquisition_PIN_A0_phyData;
  94.   DX_BT27_Slave_HC05_mySerial.println(currentAcquisitionInt);
  95. }
  96.  
  97. void updateInputs() {
  98.   currentAcquisition_PIN_A0_rawData = analogRead(currentAcquisition_PIN_A0);
  99. }
  100.  
  101. /* BLOCK lookup_phyData_from_voltage */
  102. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  103. {
  104.     // Search table for appropriate value.
  105.     uint8_t index = 0;
  106.  
  107.     const float *voltagePointer = &voltage_phyData_lookup[0];
  108.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  109.  
  110.     // Perform minimum and maximum voltage saturation based on characteristic curve
  111.     voltage = min(voltage, voltagePointer[segment_points-1]);
  112.     voltage = max(voltage, voltagePointer[0]);
  113.  
  114.     while( voltagePointer[index] <= voltage && index < segment_points )
  115.         index++;
  116.  
  117.     // If index is zero, physical value is smaller than our table range
  118.     if( index==0 )
  119.     {
  120.         return map_f( voltage,
  121.             voltagePointer[0],   // X1
  122.             voltagePointer[1],   // X2
  123.             phyDataPointer[0],   // Y1
  124.             phyDataPointer[1] ); // Y2
  125.     }
  126.     // If index is maxed out, phyisical value is larger than our range.
  127.     else if( index==segment_points )
  128.     {
  129.         return map_f( voltage,
  130.             voltagePointer[segment_points-2],   // X1
  131.             voltagePointer[segment_points-1],   // X2
  132.             phyDataPointer[segment_points-2],   // Y1
  133.             phyDataPointer[segment_points-1] ); // Y2
  134.     }
  135.     // index is between 0 and max, just right
  136.     else
  137.     {
  138.         return map_f( voltage,
  139.             voltagePointer[index-1], // X1
  140.             voltagePointer[index],    // X2
  141.             phyDataPointer[index-1], // Y1
  142.             phyDataPointer[index] );  // Y2
  143.     }
  144. }
  145. /* END BLOCK lookup_phyData_from_voltage */
  146.  
  147. /* BLOCK map_f */
  148. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  149. {
  150.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  151. }
  152. /* END BLOCK map_f */
  153.  
  154. /* BLOCK convertInputsFromRawToPhyData */
  155. void convertInputsFromRawToPhyData()
  156. {
  157.     float voltage = 0.0;
  158.  
  159.     voltage = currentAcquisition_PIN_A0_rawData * (5.0 / 1023.0);
  160.     currentAcquisition_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Current_PIN_A0, &(voltage_Current_PIN_A0_lookup[0][0]));
  161.  
  162. }
  163. /* END BLOCK convertInputsFromRawToPhyData */
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement