// Pin assignments
const int buttonPin = 2;
const int limitSwitch1 = 3;
const int limitSwitch2 = 4;
const int relayPin1 = 5; // Relay for Valve 1
const int relayPin2 = 6; // Relay for Valve 2
// Piston motion states
enum MotionState {
STOPPED,
FORWARD,
REVERSE
};
MotionState currentState = STOPPED;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(limitSwitch1, INPUT_PULLUP);
pinMode(limitSwitch2, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
// Turn off both relays at startup
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
void loop() {
int buttonState = digitalRead(buttonPin);
int switch1State = digitalRead(limitSwitch1);
int switch2State = digitalRead(limitSwitch2);
switch (currentState) {
case STOPPED:
if (buttonState == LOW) {
currentState = FORWARD;
digitalWrite(relayPin1, HIGH); // Activate hydraulic valve 1 in one direction
}
break;
case FORWARD:
if (switch1State == LOW) {
currentState = REVERSE;
digitalWrite(relayPin1, LOW); // Turn off hydraulic valve 1
digitalWrite(relayPin2, HIGH); // Activate hydraulic valve 2 in one direction
}
break;
case REVERSE:
if (switch2State == LOW) {
currentState = STOPPED;
digitalWrite(relayPin2, LOW); // Turn off hydraulic valve 2
}
break;
}
}