int button1Pin = 7; // Connect button 1 to digital pin 7
int button4Pin = 9;
int button2Pin = 5; // Connect button 2 to digital pin 5
int button3Pin = 6; // Connect button 3 to digital pin 6
int relay1Pin = 4; // Connect relay 1 to digital pin 4
int relay2Pin = 3; // Connect relay 2 to digital pin 3
int relay3Pin = 2;
int relay4Pin = 8; // Connect relay 3 to digital pin 2
int counter = 0;
bool relay1_active = false;
bool relay2_3_active = false;
unsigned long startTime;
bool safe = false;
const unsigned long RELAY1_2_DURATION = 2 * 60 * 1000; // 2 minutes in milliseconds
const unsigned long TOTAL_DURATION = 4 * 60 * 1000; // 4 minutes in milliseconds
void setup() {
// Initialize digital pins
pinMode(button1Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
// Start serial communication
Serial.begin(9600);
}
void loop() {
int moisture1 = analogRead(A2);
int moisture2 = analogRead(A3);
if (moisture1 > 50 && moisture2 > 50 ) {
digitalWrite(relay4Pin, LOW);
}
if (moisture2 < 50 && moisture1 < 50) {
digitalWrite(relay4Pin, HIGH);
}
while(digitalRead(button4Pin) == HIGH)
{
digitalWrite(relay4Pin, HIGH);
}
//Serial.println (digitalRead(button1Pin));
// Check button 1
if (digitalRead(button1Pin) == HIGH)// || الوقت المحدد حان
{
// Button 1 is pressed, turn on relay 1 and relay 2 for 2 minutes
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, LOW);
relay1_active = true;
safe = true;
startTime = millis();
}
// Check if relay 1 and 2 duration has passed
if (relay1_active && millis() - startTime >= RELAY1_2_DURATION) {
digitalWrite(relay1Pin, LOW);
relay1_active = false;
}
// Check button 2
if (digitalRead(button2Pin) == HIGH && safe == true) {
// Button 2 is pressed, turn on relay 3 and turn off relay 2
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, HIGH);
relay2_3_active = true;
}
// Check button 3
if (digitalRead(button3Pin) == HIGH && safe == true) {
// Button 3 is pressed, turn on relay 2 and turn off relay 3
digitalWrite(relay2Pin, HIGH);
digitalWrite(relay3Pin, LOW);
relay2_3_active = true;
}
// Check if relay 2&3 duration has passed
if ( relay2_3_active && millis() - startTime >= TOTAL_DURATION) {
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, LOW);
relay2_3_active = false;
safe == false ;
}
}