Advertisement
pleasedontcode

**Analog Communication** rev_02

Feb 10th, 2025
64
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: **Analog Communication**
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2025-02-10 21:59:45
  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. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SoftwareSerial.h>
  31. #include <Wire.h>
  32. #include <Adafruit_MCP4725.h>   //https://github.com/adafruit/Adafruit_MCP4725
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateInputs();
  38. void convertInputsFromRawToPhyData();
  39. void sendDataViaBluetooth(int data);
  40. void receiveDataFromBluetooth();
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t currentAcquisition_PIN_A0     = A0;
  44.  
  45. /***** DEFINITION OF Software Serial *****/
  46. const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1      = A1;
  47. const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2      = A2;
  48. SoftwareSerial DX_BT27_Slave_HC05_mySerial(DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2, DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1);
  49.  
  50. /***** DEFINITION OF I2C PINS *****/
  51. const uint8_t MCP4725_PIN_SDA_A4        = A4;
  52. const uint8_t MCP4725_PIN_SCL_A5        = A5;
  53.  
  54. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  55. const uint8_t SEGMENT_POINTS_voltage_Current_PIN_A0 = 2;
  56. const float voltage_Current_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Current_PIN_A0] =
  57. {
  58.     {0.0    ,   5.0},   //Voltage [V]
  59.     {0.0    ,   500.0}  //Current [A]
  60. };
  61.  
  62. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  63. unsigned int    currentAcquisition_PIN_A0_rawData       = 0; // Analog Input
  64.  
  65. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  66. float   currentAcquisition_PIN_A0_phyData       = 0.0; // Current [A]
  67.  
  68. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  69. // Instantiate the MCP4725 object
  70. Adafruit_MCP4725 mcp;
  71.  
  72. void setup(void)
  73. {
  74.     // put your setup code here, to run once:
  75.     pinMode(currentAcquisition_PIN_A0, INPUT);
  76.     DX_BT27_Slave_HC05_mySerial.begin(9600);
  77.     mcp.begin(MCP4725_I2CADDR_DEFAULT); // Initialize the MCP4725
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.     updateInputs(); // Refresh input data
  84.     convertInputsFromRawToPhyData(); // Transform raw data to physical data
  85.  
  86.     // Send current acquisition data via Bluetooth
  87.     sendDataViaBluetooth((int)currentAcquisition_PIN_A0_phyData);
  88.  
  89.     // Check for incoming data from Bluetooth
  90.     receiveDataFromBluetooth();
  91. }
  92.  
  93. void updateInputs()
  94. {
  95.     currentAcquisition_PIN_A0_rawData = analogRead(currentAcquisition_PIN_A0);
  96. }
  97.  
  98. void convertInputsFromRawToPhyData()
  99. {
  100.     float voltage = 0.0;
  101.     voltage = currentAcquisition_PIN_A0_rawData * (5.0 / 1023.0);
  102.     currentAcquisition_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Current_PIN_A0, &(voltage_Current_PIN_A0_lookup[0][0]));
  103.  
  104.     // Set the voltage on the MCP4725
  105.     mcp.setVoltage((uint16_t)(currentAcquisition_PIN_A0_phyData * 4095.0 / 500.0), false); // Scale to 12-bit value
  106. }
  107.  
  108. void sendDataViaBluetooth(int data)
  109. {
  110.     DX_BT27_Slave_HC05_mySerial.println(data); // Send data as integer via Bluetooth
  111. }
  112.  
  113. void receiveDataFromBluetooth()
  114. {
  115.     if (DX_BT27_Slave_HC05_mySerial.available()) {
  116.         String command = DX_BT27_Slave_HC05_mySerial.readStringUntil('\n'); // Read incoming command
  117.         // Process the command as needed
  118.         // For example, you could set the MCP4725 voltage based on a received command
  119.     }
  120. }
  121.  
  122. /* BLOCK lookup_phyData_from_voltage */
  123. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  124. {
  125.     // Search table for appropriate value.
  126.     uint8_t index = 0;
  127.  
  128.     const float *voltagePointer = &voltage_phyData_lookup[0];
  129.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  130.  
  131.     // Perform minimum and maximum voltage saturation based on characteristic curve
  132.     voltage = min(voltage, voltagePointer[segment_points-1]);
  133.     voltage = max(voltage, voltagePointer[0]);
  134.  
  135.     while( voltagePointer[index] <= voltage && index < segment_points )
  136.         index++;
  137.  
  138.     // If index is zero, physical value is smaller than our table range
  139.     if( index==0 )
  140.     {
  141.         return map_f( voltage,
  142.             voltagePointer[0],   // X1
  143.             voltagePointer[1],   // X2
  144.             phyDataPointer[0],   // Y1
  145.             phyDataPointer[1] ); // Y2
  146.     }
  147.     // If index is maxed out, phyisical value is larger than our range.
  148.     else if( index==segment_points )
  149.     {
  150.         return map_f( voltage,
  151.             voltagePointer[segment_points-2],   // X1
  152.             voltagePointer[segment_points-1],   // X2
  153.             phyDataPointer[segment_points-2],   // Y1
  154.             phyDataPointer[segment_points-1] ); // Y2
  155.     }
  156.     // index is between 0 and max, just right
  157.     else
  158.     {
  159.         return map_f( voltage,
  160.             voltagePointer[index-1], // X1
  161.             voltagePointer[index],    // X2
  162.             phyDataPointer[index-1], // Y1
  163.             phyDataPointer[index] );  // Y2
  164.     }
  165. }
  166. /* END BLOCK lookup_phyData_from_voltage */
  167.  
  168. /* BLOCK map_f */
  169. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  170. {
  171.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  172. }
  173. /* END BLOCK map_f */
  174.  
  175. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement