Here is a code for automatic impedance matching with 2 sets of four relay. The relays are toggled and the power transferred is calculated when the distance sensor reads a change in the current distance. The comparison is made with the initial power at the initial distance. How do I get the maximum power at the combination of the relays with resonance capacitors connected to them? I appreciate your comments
long duration;
long distance;
const int distanceSensorTriggerPin = 13; // Example trigger pin
const int distanceSensorEchoPin = 12; // Example echo pin
// Define analog input pins for voltage and current measurements
const int voltagePin = A0; // Example voltage measurement pin
const int currentPin = A1; // Example current measurement pin
// Variables for tracking maximum power and corresponding relay array configuration
float maxPower = 0.0;
int maxPowerRelayArray = 0;
#define RELAY1_PIN_1 2 // Relay 1, Bit 1 pin
#define RELAY1_PIN_2 3 // Relay 1, Bit 2 pin
#define RELAY1_PIN_3 4 // Relay 1, Bit 3 pin
#define RELAY1_PIN_4 5 // Relay 1, Bit 4 pin
#define RELAY2_PIN_1 6 // Relay 2, Bit 1 pin
#define RELAY2_PIN_2 7 // Relay 2, Bit 2 pin
#define RELAY2_PIN_3 8 // Relay 2, Bit 3 pin
#define RELAY2_PIN_4 9 // Relay 2, Bit 4 pin
#define NUM_BITS 4 // Number of bits in each relay array
// Initialize relay array pins as outputs
void initializeRelayArrays() {
pinMode(RELAY1_PIN_1, OUTPUT);
pinMode(RELAY1_PIN_2, OUTPUT);
pinMode(RELAY1_PIN_3, OUTPUT);
pinMode(RELAY1_PIN_4, OUTPUT);
pinMode(RELAY2_PIN_1, OUTPUT);
pinMode(RELAY2_PIN_2, OUTPUT);
pinMode(RELAY2_PIN_3, OUTPUT);
pinMode(RELAY2_PIN_4, OUTPUT);
}
// Calculate the received power for a given relay combination
float calculateReceivedPower(int relay1Value, int relay2Value) {
return voltagePin*currentPin;
}
// Set the impedance using the relay arrays based on the distance
void setImpedance(float distance) {
float maxPower = 0;
int maxRelay1 = 0;
int maxRelay2 = 0;
// Loop through all possible relay combinations
for (int i = 0; i < pow(2, NUM_BITS); i++) {
int relay1Value = i % 16; // Convert the loop variable to a 4-bit value
int relay2Value = i / 16; // Shift the loop variable to get the next 4-bit value
// Set relay array bits based on the relay values
digitalWrite(RELAY1_PIN_1, (relay1Value & 0b0001) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_2, (relay1Value & 0b0010) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_3, (relay1Value & 0b0100) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_4, (relay1Value & 0b1000) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_1, (relay2Value & 0b0001) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_2, (relay2Value & 0b0010) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_3, (relay2Value & 0b0100) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_4, (relay2Value & 0b1000) ? HIGH : LOW);
// Calculate the received power for the current relay combination
float receivedPower = calculateReceivedPower(relay1Value, relay2Value);
// Update the maximum power and corresponding relay values
if (receivedPower > maxPower) {
maxPower = receivedPower;
maxRelay1 = relay1Value;
maxRelay2 = relay2Value;
}
}
// Set the relay array bits to achieve the impedance for the given distance
digitalWrite(RELAY1_PIN_1, (maxRelay1 & 0b0001) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_2, (maxRelay1 & 0b0010) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_3, (maxRelay1 & 0b0100) ? HIGH : LOW);
digitalWrite(RELAY1_PIN_4, (maxRelay1 & 0b1000) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_1, (maxRelay2 & 0b0001) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_2, (maxRelay2 & 0b0010) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_3, (maxRelay2 & 0b0100) ? HIGH : LOW);
digitalWrite(RELAY2_PIN_4, (maxRelay2 & 0b1000) ? HIGH : LOW);
}
void setup() {
Serial.begin(9600);
initializeRelayArrays(); // Initialize relay arrays
//distanceSensor.begin(); // Initialize the distance sensor
pinMode(distanceSensorTriggerPin, OUTPUT);
pinMode(distanceSensorEchoPin, INPUT);
// Start serial communication
}
void loop() {
digitalWrite(distanceSensorTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(distanceSensorTriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(distanceSensorTriggerPin, LOW);
duration = pulseIn(distanceSensorEchoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
setImpedance(distance); // Set the impedance based on the distance
delay(1000); // Delay between impedance adjustments
}








