const int pwmPin = 9; // PWM output pin to control MOSFET
const int potPin = A0; // Potentiometer input pin
const int sensePin = A1; // Voltage sense pin
void setup() {
pinMode(pwmPin, OUTPUT);
pinMode(potPin, INPUT);
pinMode(sensePin, INPUT);
Serial.begin(9600); // For debugging
}
void loop() {
int setVoltage = analogRead(potPin); // Read desired voltage from pot
setVoltage = map(setVoltage, 0, 1023, 0, 255); // Map to PWM range
int actualVoltage = analogRead(sensePin); // Read actual output voltage
actualVoltage = map(actualVoltage, 0, 1023, 0, 255); // Map to PWM range
int error = setVoltage - actualVoltage; // Calculate error
// Adjust PWM signal to correct output voltage
int pwmValue = constrain(setVoltage + error, 0, 255);
analogWrite(pwmPin, pwmValue);
// Debugging
Serial.print("Set Voltage: ");
Serial.print(setVoltage);
Serial.print(" Actual Voltage: ");
Serial.print(actualVoltage);
Serial.print(" PWM Value: ");
Serial.println(pwmValue);
delay(100); // Small delay for stability
}