const int limitSwitch1Pin = 2; // Pin for the first limit switch
const int limitSwitch2Pin = 3; // Pin for the second limit switch
const int pushButtonPin = 4; // Pin for the push button
const int relay1Pin = 5; // Pin for the first relay
const int relay2Pin = 6; // Pin for the second relay
bool pistonDirection = false; // false for "to" motion, true for "fro" motion
bool buttonPressed = false;
void setup() {
pinMode(limitSwitch1Pin, INPUT_PULLUP);
pinMode(limitSwitch2Pin, INPUT_PULLUP);
pinMode(pushButtonPin, INPUT_PULLUP);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
}
void loop() {
// Read limit switches
bool limit1Pressed = digitalRead(limitSwitch1Pin) == LOW;
bool limit2Pressed = digitalRead(limitSwitch2Pin) == LOW;
// Read push button
buttonPressed = digitalRead(pushButtonPin) == LOW;
// If the button is pressed, start the piston cycle
if (buttonPressed) {
// If neither limit switch is pressed, continue moving in the current direction
if (!limit1Pressed && !limit2Pressed) {
digitalWrite(relay1Pin, pistonDirection); // Control relay 1
digitalWrite(relay2Pin, !pistonDirection); // Control relay 2
}
// If the "to" limit switch is pressed, change direction to "fro"
else if (limit2Pressed) {
pistonDirection = true;
digitalWrite(relay1Pin, pistonDirection);
digitalWrite(relay2Pin, !pistonDirection);
}
// If the "fro" limit switch is pressed, change direction to "to"
else if (limit1Pressed) {
pistonDirection = false;
digitalWrite(relay1Pin, pistonDirection);
digitalWrite(relay2Pin, !pistonDirection);
}
}
// If the button is not pressed, stop the piston
else {
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
}
}