// Define pin numbers for buttons and relay
const int button1Pin = 2; // Button 1
const int button2Pin = 3; // Button 2
const int button3Pin = 4; // Button 3
const int relayPin = 5; // Relay
// Variables to keep track of test counts
int completeTestCount = 0;
int incompleteTestCount = 0;
int manualTestCount = 0;
// Variables to keep track of relay state, timing, and button 1 cycle
bool isRelayOn = false;
bool isButton1Cycle = false;
unsigned long relayOnTime = 0;
const unsigned long relayDuration = 40000; // 40 seconds
void setup() {
// Initialize the buttons as inputs with internal pull-up resistors
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
// Initialize the relay as an output
pinMode(relayPin, OUTPUT);
// Start with the relay off
digitalWrite(relayPin, LOW);
Serial.begin(9600);
}
void loop() {
// Read the state of the buttons
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
int button3State = digitalRead(button3Pin);
// Handle Button 1 press
if (button1State == LOW && !isRelayOn) {
// Turn on the relay for 40 seconds
digitalWrite(relayPin, HIGH);
isRelayOn = true;
isButton1Cycle = true; // Indicate that relay is on due to button 1
relayOnTime = millis();
}
// Handle Button 2 press
if (button2State == LOW && isRelayOn) {
// Turn off the relay immediately
digitalWrite(relayPin, LOW);
isRelayOn = false;
isButton1Cycle = false; // Reset the button 1 cycle indicator
incompleteTestCount++;
Serial.print("Incomplete Test Counter = ");
Serial.println(incompleteTestCount);
}
// Handle Button 3 press
if (button3State == LOW && !isButton1Cycle) {
// Turn on the relay while the button is pressed, only if not in button 1 cycle
digitalWrite(relayPin, HIGH);
isRelayOn = true;
} else if (button3State == HIGH && isRelayOn && !isButton1Cycle) {
// Turn off the relay when the button is released, only if not in button 1 cycle
digitalWrite(relayPin, LOW);
isRelayOn = false;
manualTestCount++;
Serial.print("Manual Test Counter = ");
Serial.println(manualTestCount);
// Increment the count after the relay turns off
}
// Check if the relay has been on for 40 seconds due to button 1
if (isRelayOn && isButton1Cycle && millis() - relayOnTime >= relayDuration) {
// Turn off the relay after 40 seconds and increment completeTestCount
digitalWrite(relayPin, LOW);
isRelayOn = false;
isButton1Cycle = false; // Reset the button 1 cycle indicator
completeTestCount++;
Serial.print("Complete Test Counter = ");
Serial.println(completeTestCount);
// Increment the count after the relay turns off
}
}