Hi,
I am a novice with Arduino.
In this project I am using an Arduino nano. I’d like to use a push button switch to control a valve. The valve is to close only after a specified time or time intervals has occurred. It will start with an open valve. The valve has only 2 wires which opens and closes via reverse polarity using a 2-channel relay.
If the button is pressed and held down, print to serial monitor “Switch Press Start Timer”. If the switch is let go, print to the serial monitor “Switch Not Pressed. Timer Reset”. But if the button is pressed and held down for longer than 2 minutes, close the valve and turn on a red LED and stop the loop until reset button is pressed.
If a switch is pressed and held down for 10 seconds or more but less than 30 seconds (only within this time interval. We will call this condition “switch short press”. Have the serial monitor to print “switch short press”.
If the “switch short press” condition occurs 3 times within 15 minutes, close the valve and turn on a yellow LED and stop the loop until reset button is pressed.
With the current code, if the switch is pressed and held down it will start the “Switch Short Press Count” and continue to count up (past 3). Then at the 2-minute mark the valve closes and lights up the Red LED.
I would like it to only count the “Switch Short Press Count” if only the parameters above are seen (switch is pressed and held down for 10 seconds or more but less than 30 seconds).
I would really appreciate any help and advice.
Thank you in advance.
```cpp
const int switchPin = 3; // Pin to check for 5V
const int ledPinYellow = 5; // LED pin to indicate 3 switch short press counts
const int ledPinRed = 6; // LED pin to indicate switch pressed longer than 2 minutes
int switchShortPressCount = 0; // Counter for switch short presses
unsigned long startTime = 0; // Timer start time
const int valveOpen = 11; // Valve open pin
const int valveClose = 12; // Valve close pin
// previous time for tasks depending upon time.
unsigned long prevTime_T1 = millis();
unsigned long prevTime_T2 = millis();
unsigned long prevTime_T3 = millis();
// time intervals for the tasks
long interval = 120000; // Interval for valve closure
long interval_T1 = 10000; // switch pressed for 10 seconds or more
long interval_T2 = 30000; // switch pressed for 30 seconds or less
long interval_T3 = 900000; // Interval for 3 switch short presses
void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(ledPinYellow, OUTPUT);
pinMode(ledPinRed, OUTPUT);
digitalWrite(ledPinYellow, LOW); // Turn off the LED initially
digitalWrite(ledPinRed, LOW); //Turn off the LED initally
pinMode(valveOpen, OUTPUT);
pinMode(valveClose, OUTPUT);
digitalWrite(valveOpen, HIGH);
digitalWrite(valveClose, HIGH);
}
void loop() {
digitalWrite(valveOpen, LOW);
delay(4500);
digitalWrite(valveOpen, HIGH);
Serial.println("Valve Open");
unsigned long currentMillis = millis();
unsigned long currentTime = millis();
int voltage = digitalRead(switchPin); // Read voltage on pin D3
if (voltage == HIGH && startTime == 0) {
startTime = currentMillis; // Set the timer start time when switch is pressed
Serial.print(startTime);
Serial.println(" Switch Press Start Timer");
}
if (startTime > 0 && voltage == LOW) {
startTime = 0; // Reset the timer when switch is not pressed
Serial.println("Switch Not Pressed. Timer Reset");
}
if (startTime > 0 && (currentMillis - startTime) > interval) {
digitalWrite(valveClose, LOW);
delay(4500);
digitalWrite(valveClose, HIGH);
Serial.println("Valve Closed Until Reset");
digitalWrite(ledPinRed, HIGH); // Turn on the LED to indicate valve closed
for (;;) {
// Code inside this loop will run indefinitely
}
}
if (voltage == HIGH && currentTime - prevTime_T1 >= interval_T1 && currentTime - prevTime_T2 <= interval_T2) {
prevTime_T1 = currentTime;
prevTime_T2 = currentTime;
switchShortPressCount++;
Serial.print("Switch Short Press Count: ");
Serial.println(switchShortPressCount);
} else if (currentTime - prevTime_T2 >= interval) {
prevTime_T2 = currentTime;
}
if (switchShortPressCount >= 3 && currentTime - prevTime_T3 >= interval_T3) {
digitalWrite(valveClose, LOW);
delay(4500);
digitalWrite(valveClose, HIGH);
Serial.println("Valve Closed Until Reset");
digitalWrite(ledPinYellow, HIGH);
for (;;) {
// Code inside this loop will run indefinitely
}
}
}