Hello, I need help to fix the code. The logic is if current is below 3Amps, the relay should close to run dc motor and also the servo motor simultaneously
#include <Servo.h>
Servo wiperServo;
const int currentPin = A0; // ACS712 output
const int relayPin = 8; // Relay control
float threshold = 3.0; // 3A limit
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
wiperServo.attach(9);
wiperServo.write(0);
}
void loop() {
float current = 0;
// Read and average current
for (int i = 0; i < 1000; i++) {
current += (0.044 * analogRead(currentPin) - 3.78) / 1000.0;
delay(1);
}
Serial.print("Current: ");
Serial.println(current);
// When current is below threshold
if (current < threshold) {
digitalWrite(relayPin, HIGH); // close relay → motor ON
// wipe servo 3 times
for (int i = 0; i < 3; i++) {
wiperServo.write(90);
delay(500);
wiperServo.write(0);
delay(500);
}
digitalWrite(relayPin, LOW); // open relay → motor OFF
}
delay(1000);
}
So, what's not working? It looks like your code is doing exactly what you describe.
What does your Serial Monitor say? You have some Serial.print() statements that print out the current, is it below 3.0?
With running motors, there is always the possibility that you are not powering it correctly or supplying enough current to get the job done? How is this all wired up?
I'm lost there. Proteus simulation is showing zero. I'm using proteus to simulate and Arduino IDE to write the code. I still have to do the actual testing on a board.