const int S1_PIN = 2;
const int S2_PIN = 3;
const int R1_PIN = 4;
const int R2_PIN = 5;
const int R3_PIN = 6;
const int P1_PIN = 7;
const int P2_PIN = 8; // Manual control for R1
const int P3_PIN = 9; // Manual control for R2
const int P4_PIN = 10; // Manual control for R3;
const int T1_PIN = A0; // Potentiometer T1
const int T2_PIN = A1; // Potentiometer T2
const int MODE_SWITCH_PIN = 11; // SPST switch for mode control
const int R4_PIN = 12; // Relay R4 for Independent Mode
const int P5_PIN = A5; // Push button P5 connected to pin A5 (pull-up internally)
bool isAutoMode = true;
bool r4State = LOW; // Keep track of Relay R4 state
bool lastP5State = HIGH; // Keep track of the previous state of P5
void setup() {
pinMode(S1_PIN, INPUT_PULLUP);
pinMode(S2_PIN, INPUT_PULLUP);
pinMode(R1_PIN, OUTPUT);
pinMode(R2_PIN, OUTPUT);
pinMode(R3_PIN, OUTPUT);
pinMode(P1_PIN, INPUT_PULLUP);
pinMode(P2_PIN, INPUT_PULLUP);
pinMode(P3_PIN, INPUT_PULLUP);
pinMode(P4_PIN, INPUT_PULLUP);
pinMode(MODE_SWITCH_PIN, INPUT_PULLUP);
pinMode(R4_PIN, OUTPUT);
pinMode(P5_PIN, INPUT_PULLUP); // Using internal pull-up for P5 on pin A5
Serial.begin(9600); // Initialize Serial communication for debugging
// Check the initial state of the SPST switch at startup
if (digitalRead(MODE_SWITCH_PIN) == LOW) {
isAutoMode = false; // Switch is low, set to manual mode
} else {
isAutoMode = true; // Switch is high, set to auto mode
}
}
void loop() {
int currentP5State = digitalRead(P5_PIN);
int s1State = digitalRead(S1_PIN);
int s2State = digitalRead(S2_PIN);
// Check if both S1 and S2 are LOW, shut down process
if (s1State == LOW && s2State == LOW) {
digitalWrite(R1_PIN, LOW); // Turn off relay R1
digitalWrite(R2_PIN, LOW); // Turn off relay R2
digitalWrite(R3_PIN, LOW); // Turn off relay R3
}
else {
// Toggle Relay R4 with Push Button P5 to enable auto mode
if (currentP5State == LOW && lastP5State == HIGH) {
r4State = !r4State;
digitalWrite(R4_PIN, r4State);
}
lastP5State = currentP5State;
// Check the state of the SPST switch
if (digitalRead(MODE_SWITCH_PIN) == LOW) {
isAutoMode = false; // Manual mode is active when the switch is on
} else {
isAutoMode = true; // Auto mode is active when the switch is off
}
if (!isAutoMode) { // If manual mode is active
// Manual mode logic
if (digitalRead(P2_PIN) == LOW) {
digitalWrite(R1_PIN, HIGH);
while (digitalRead(S1_PIN) == HIGH && digitalRead(P2_PIN) == LOW) {
// Wait for S1 to be reached or P2 button release (momentary action)
}
digitalWrite(R1_PIN, LOW);
}
if (digitalRead(P3_PIN) == LOW) {
digitalWrite(R2_PIN, HIGH);
while (digitalRead(P3_PIN) == LOW && digitalRead(S2_PIN) == HIGH) {
// Wait for P3 button release or S2 to be reached (momentary action)
}
digitalWrite(R2_PIN, LOW);
}
if (digitalRead(P4_PIN) == LOW) {
digitalWrite(R3_PIN, HIGH);
while (digitalRead(P4_PIN) == LOW) {
// Wait for P4 button release (momentary action)
}
digitalWrite(R3_PIN, LOW);
}
} else { // If auto mode is active
// Auto mode logic
if (r4State) {
if (digitalRead(P1_PIN) == LOW) {
// Start auto cycle only if R4 is on and P1 is pushed
digitalWrite(R2_PIN, HIGH);
while (digitalRead(S2_PIN) == HIGH) {
// Wait for S2 to close
}
digitalWrite(R2_PIN, LOW);
// Read desired off time from Potentiometer T2
int offTimeR2 = map(analogRead(T2_PIN), 0, 1023, 0, 10000);
delay(offTimeR2);
digitalWrite(R1_PIN, HIGH);
while (digitalRead(S1_PIN) == HIGH) {
// Wait for S1 to be reached
}
digitalWrite(R1_PIN, LOW);
int onTimeR3 = map(analogRead(T1_PIN), 0, 1023, 1000, 10000);
digitalWrite(R3_PIN, HIGH);
delay(onTimeR3);
digitalWrite(R3_PIN, LOW);
}
}
}
}
delay(100);
}