Hello everyone,
I have a question. I have a project where I want to power an actuator with 3 reed switches. I want when the arduino powers on the actuator will retract. In the loop I want the actuator to extend all the way when all three read switches are closed. I have this code I wrote with one reed switch but it seems to not retract every time I restart the arduino. Sometimes it will and sometimes it wont.
i am using a dual relay module with a 12v power supply. I have the neg of actuator to one common pin on relay and negative to the other common pin. I then have the power supply negative go to both NC and the positive go to both NO on the relay outputs. because it wasn't working I added another reed switch to try and make it retract but I would prefer it just retracts on startup.
// Define the pins for reed switch and relay module
const int reedSwitchPin = 2;
const int resetswitch = 3;
const int relayExtendPin = 8; // Control pin for extending the actuator
const int relayRetractPin = 9; // Control pin for retracting the actuator
bool actuatorExtended = false;
void setup() {
pinMode(reedSwitchPin, INPUT_PULLUP);
pinMode(resetswitch, INPUT_PULLUP);
pinMode(relayExtendPin, OUTPUT);
pinMode(relayRetractPin, OUTPUT);
delay(1000);
// Retract actuator on startup
// digitalWrite(relayExtendPin, LOW); // Ensure extend relay is off
// digitalWrite(relayRetractPin, HIGH); // Turn on retract relay
digitalWrite(relayExtendPin, LOW); // Ensure extend relay is off
digitalWrite(relayRetractPin, HIGH); // Turn on retract relay
}
void loop() {
// Check if reed switch is triggered
if (digitalRead(reedSwitchPin) == LOW) {
// If actuator is not already extended, extend it
if (!actuatorExtended) {
digitalWrite(relayRetractPin, LOW); // Ensure retract relay is off
digitalWrite(relayExtendPin, HIGH); // Turn on extend relay
actuatorExtended = true;
// extendActuator();
}
} else {
// If actuator is extended and reed switch is not triggered, retract it
// if (actuatorExtended) {
// retractActuator();
// }
}
}
//void extendActuator() { deleted to simplify
//digitalWrite(relayRetractPin, LOW); // Ensure retract relay is off
// digitalWrite(relayExtendPin, HIGH); // Turn on extend relay
//actuatorExtended = true;
//}
