// Define pin assignments and constants
const int spstPin = 2;
const int p1Pin = 3;
const int r1Pin = 4;
const int r2Pin = 5;
const int r3Pin = 6;
const int s1Pin = 7;
const int s2Pin = 8; // Assuming this was intended for s2Pin based on your description
const int potPin = A0;

const unsigned long minInterval = 1000;   // 1 second in milliseconds
const unsigned long maxInterval = 10000;  // 10 seconds in milliseconds

// Define states
enum State {
  WAITING,
  MOVING_PISTON,
  R2_ACTIVE,
  R3_ACTIVE
};
State currentState = WAITING;

// Variables for timing
unsigned long previousMillis = 0;
unsigned long interval = minInterval;

void setup() {
  pinMode(spstPin, INPUT);
  pinMode(p1Pin, INPUT_PULLUP); // Use internal pull-up resistor for push button
  pinMode(r1Pin, OUTPUT);
  pinMode(r2Pin, OUTPUT);
  pinMode(r3Pin, OUTPUT);
  pinMode(s1Pin, INPUT);
  pinMode(s2Pin, INPUT);

  digitalWrite(r1Pin, LOW);
  digitalWrite(r2Pin, LOW);
  digitalWrite(r3Pin, LOW);
}

void loop() {
  unsigned long currentMillis = millis();

  switch (currentState) {
    case WAITING:
      if (digitalRead(spstPin) == HIGH) {
        currentState = MOVING_PISTON;
        digitalWrite(r1Pin, HIGH);
        previousMillis = currentMillis;
      }
      break;

    case MOVING_PISTON:
      if (digitalRead(s1Pin) == HIGH) {
        currentState = R2_ACTIVE;
        digitalWrite(r1Pin, LOW);
        digitalWrite(r2Pin, HIGH);
      }
      break;

    case R2_ACTIVE:
      if (digitalRead(s2Pin) == HIGH) {
        currentState = R3_ACTIVE;
        digitalWrite(r2Pin, LOW);
        digitalWrite(r3Pin, HIGH);
        interval = map(analogRead(potPin), 0, 1023, minInterval, maxInterval);
        previousMillis = currentMillis;
      }
      break;

    case R3_ACTIVE:
      if (currentMillis - previousMillis >= interval) {
        currentState = WAITING;
        digitalWrite(r3Pin, LOW);
      }
      break;
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module