#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns, 4 rows
const int deployButtonPin = 3;
const int stopButtonPin = 2;
const int continueButtonPin = 1;
const int relayPins[] = {14, 15, 16, 17};
const int lightSensorPin = A0;
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]);
int currentRelay = 0;
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.print("Press Deploy");
pinMode(deployButtonPin, INPUT);
pinMode(stopButtonPin, INPUT);
pinMode(continueButtonPin, INPUT);
pinMode(lightSensorPin, INPUT); // Light sensor pin
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Initial state: all relays are off
}
}
void loop() {
if (digitalRead(deployButtonPin) == HIGH) {
lcd.clear();
lcd.print("Deploying Relay ");
lcd.print(currentRelay + 1);
digitalWrite(relayPins[currentRelay], HIGH); // Activate the current relay (turn on the LED)
delay(2000); // Adjust the delay time according to your needs
// Check the light sensor to see if the LED is still emitting light
int lightSensorValue = analogRead(lightSensorPin);
bool deploymentSuccessful = lightSensorValue < 100; // Adjust the threshold as needed
lcd.clear();
lcd.print("Relay ");
lcd.print(currentRelay + 1);
lcd.print(": ");
if (deploymentSuccessful) {
lcd.print("OK");
currentRelay++;
} else {
lcd.print("Failed");
// Ask user whether to continue or stop
lcd.setCursor(0, 1);
lcd.print("Continue? Y/N");
while (true) {
if (digitalRead(continueButtonPin) == HIGH) {
lcd.clear();
lcd.print("Continuing...");
delay(1000); // Display the message for 1 second (adjust as needed)
break;
} else if (digitalRead(stopButtonPin) == HIGH) {
lcd.clear();
lcd.print("Aborted");
deactivateAllRelays();
while (true) {
// Optional: End the program or perform other actions
}
}
}
}
// Check if the LED is still on before deactivating the relay
if (deploymentSuccessful) {
digitalWrite(relayPins[currentRelay], LOW); // Deactivate the current relay (turn off the LED)
}
delay(2000); // Adjust the delay time according to your needs
if (currentRelay == numRelays) {
lcd.clear();
lcd.print("All relays deployed");
while (true) {
// Optional: End the program or perform other actions
}
}
}
}
void deactivateAllRelays() {
for (int i = 0; i < numRelays; i++) {
digitalWrite(relayPins[i], LOW); // Deactivate all relays
}
}