// Pin definitions
const int inputPin = 2; // Input signal pin
const int relayPin = 3; // Relay pin for solenoid control
const int resetButtonPin = 4; // Reset button pin
// Time thresholds in milliseconds
const unsigned long timeX = 5000; // Greater than 5 seconds
const unsigned long timeY = 2000; // Less than 2 seconds
bool relayLogic = LOW; // Set relay Logic to HIGH/LOW accoring to your relay
bool signalLogic = HIGH; // Set the logic to detect input signal
// Variables for tracking time
unsigned long inputStartTime = 0;
bool inputReceived = false;
bool solenoidActive = false;
void setup() {
pinMode(inputPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, !relayLogic); // Initially turn off relay
pinMode(resetButtonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// Read the input signal
int inputSignal = digitalRead(inputPin);
// Check if input signal is received and solenoid was not active
if (inputSignal == signalLogic && !inputReceived && solenoidActive == false) {
inputStartTime = millis(); // Record the time when input signal is received
inputReceived = true;
}
// If the input signal is released and solenoid was not active
if (inputSignal == !signalLogic && inputReceived && solenoidActive == false) {
unsigned long inputDuration = millis() - inputStartTime; // Get the time duration of input signal
if (inputDuration > timeX) {
// Trigger solenoid for duration greater than timeX
digitalWrite(relayPin, relayLogic);
Serial.println("Triggered solenoid: Input duration greater than timeX");
solenoidActive = true;
} else if (inputDuration < timeY) {
// Trigger solenoid for duration less than timeY
digitalWrite(relayPin, relayLogic);
Serial.println("Triggered solenoid: Input duration less than timeY");
solenoidActive = true;
}else{
Serial.println("Time is in between X and Y don't take action");
}
inputReceived = false; // Reset the flag for input signal
}
// Check if the reset button is pressed
if (digitalRead(resetButtonPin) == LOW && solenoidActive) {
digitalWrite(relayPin, !relayLogic); // Turn off solenoid
Serial.println("Solenoid state reset");
solenoidActive = false;
}
}