CCCPaste

**Analog Communication** rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Analog Communication**
	- Source Code NOT compiled for: Arduino Pro Mini 5V
	- Source Code created on: 2025-02-10 21:59:45

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Check if digital data is received from bluetooth */
	/* and and use this data into MCP4725. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Read currentAcquisition and send it via bluetooth. */
	/* The data to be send has to be integer. */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 	//https://github.com/adafruit/Adafruit_MCP4725

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateInputs();
void convertInputsFromRawToPhyData();
void sendDataViaBluetooth(int data);
void receiveDataFromBluetooth();

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t currentAcquisition_PIN_A0		= A0;

/***** DEFINITION OF Software Serial *****/
const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1		= A1;
const uint8_t DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2		= A2;
SoftwareSerial DX_BT27_Slave_HC05_mySerial(DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_RX_A2, DX_BT27_Slave_HC05_mySerial_PIN_SERIAL_TX_A1);

/***** DEFINITION OF I2C PINS *****/
const uint8_t MCP4725_PIN_SDA_A4		= A4;
const uint8_t MCP4725_PIN_SCL_A5		= A5;

/****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
const uint8_t SEGMENT_POINTS_voltage_Current_PIN_A0	= 2;
const float voltage_Current_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_Current_PIN_A0] =
{
	{0.0	,	5.0},	//Voltage [V]
	{0.0	,	500.0}	//Current [A]
};

/***** DEFINITION OF INPUT RAW VARIABLES *****/
unsigned int	currentAcquisition_PIN_A0_rawData		= 0; // Analog Input

/***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
float	currentAcquisition_PIN_A0_phyData		= 0.0; // Current [A]

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instantiate the MCP4725 object
Adafruit_MCP4725 mcp;

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(currentAcquisition_PIN_A0, INPUT);
	DX_BT27_Slave_HC05_mySerial.begin(9600);
	mcp.begin(MCP4725_I2CADDR_DEFAULT); // Initialize the MCP4725
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	updateInputs(); // Refresh input data
	convertInputsFromRawToPhyData(); // Transform raw data to physical data

	// Send current acquisition data via Bluetooth
	sendDataViaBluetooth((int)currentAcquisition_PIN_A0_phyData);

	// Check for incoming data from Bluetooth
	receiveDataFromBluetooth();
}

void updateInputs()
{
	currentAcquisition_PIN_A0_rawData = analogRead(currentAcquisition_PIN_A0);
}

void convertInputsFromRawToPhyData()
{
	float voltage = 0.0;
	voltage = currentAcquisition_PIN_A0_rawData * (5.0 / 1023.0);
	currentAcquisition_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_Current_PIN_A0, &(voltage_Current_PIN_A0_lookup[0][0]));

	// Set the voltage on the MCP4725
	mcp.setVoltage((uint16_t)(currentAcquisition_PIN_A0_phyData * 4095.0 / 500.0), false); // Scale to 12-bit value
}

void sendDataViaBluetooth(int data)
{
	DX_BT27_Slave_HC05_mySerial.println(data); // Send data as integer via Bluetooth
}

void receiveDataFromBluetooth()
{
	if (DX_BT27_Slave_HC05_mySerial.available()) {
		String command = DX_BT27_Slave_HC05_mySerial.readStringUntil('\n'); // Read incoming command
		// Process the command as needed
		// For example, you could set the MCP4725 voltage based on a received command
	}
}

/* BLOCK lookup_phyData_from_voltage */
float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
{
	// Search table for appropriate value.
	uint8_t index = 0;

	const float *voltagePointer = &voltage_phyData_lookup[0];
	const float *phyDataPointer = &voltage_phyData_lookup[segment_points];

	// Perform minimum and maximum voltage saturation based on characteristic curve
	voltage = min(voltage, voltagePointer[segment_points-1]);
	voltage = max(voltage, voltagePointer[0]);

	while( voltagePointer[index] <= voltage && index < segment_points )
		index++;

	// If index is zero, physical value is smaller than our table range
	if( index==0 )
	{
		return map_f( voltage,
			voltagePointer[0],   // X1
			voltagePointer[1],   // X2
			phyDataPointer[0],   // Y1
			phyDataPointer[1] ); // Y2
	}
	// If index is maxed out, phyisical value is larger than our range.
	else if( index==segment_points )
	{
		return map_f( voltage,
			voltagePointer[segment_points-2],   // X1
			voltagePointer[segment_points-1],   // X2
			phyDataPointer[segment_points-2],   // Y1
			phyDataPointer[segment_points-1] ); // Y2
	}
	// index is between 0 and max, just right
	else
	{
		return map_f( voltage,
			voltagePointer[index-1], // X1
			voltagePointer[index],    // X2
			phyDataPointer[index-1], // Y1
			phyDataPointer[index] );  // Y2
	}
}
/* END BLOCK lookup_phyData_from_voltage */

/* BLOCK map_f */
float map_f(float x, float in_min, float in_max, float out_min, float out_max)
{
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/* END BLOCK map_f */

/* END CODE */