So, here's my problem (i'm a bit of a noob with arduino).
I'm making a timer/switchbox for an outdoor fountain, with a momentary backlit pushbutton, wich if pressed for less than 2 seconds, will turn the fountain on for 30 minutes.
If you'd press the same button (when the fountain is off) for longer than 2 seconds, the fountain will work for 4 hours.
const int ledPin0 = 4;
const int solidState = 13;
int pompaan = 0;
//================
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 160 * 25 = 2000 ms = 2 seconds
byte longKeyPressCount = 0;
byte prevKeyState = HIGH; // button is active HIGH
const byte keyPin = 6; // button is connected to pin 6 and GND
//=================
void setup() {
pinMode(ledPin0, OUTPUT);
pinMode(solidState, OUTPUT);
pinMode(keyPin, INPUT);
digitalWrite(keyPin, HIGH);
}
//=================
// called when key goes from pressed to not pressed
void keyRelease() {
if (longKeyPressCount >= longKeyPressCountMax) {
longKeyPress();
}
else {
shortKeyPress();
}
}
// called when button is kept pressed for less than 2 seconds
void shortKeyPress() {
pompaan=1;
digitalWrite(ledPin0 ,HIGH);
delay(100);
digitalWrite(ledPin0 ,LOW);
delay(100);
digitalWrite(ledPin0 ,HIGH);
delay(100);
digitalWrite(ledPin0 ,LOW);
delay(100);
digitalWrite(ledPin0 ,HIGH);
delay(100);
digitalWrite(ledPin0 ,LOW);
delay(100);
digitalWrite(ledPin0 ,HIGH);
delay(100);
digitalWrite(ledPin0 ,LOW);
delay(100);
digitalWrite(ledPin0 ,HIGH);
delay(100);
digitalWrite(ledPin0 ,LOW);
digitalWrite(solidState, HIGH);
delay(11800000);
digitalWrite(solidState, LOW);
}
// called when button is kept pressed for more than 2 seconds
void longKeyPress() {
pompaan=1;
digitalWrite(ledPin0, HIGH);
delay(4000);
digitalWrite(ledPin0, LOW);
digitalWrite(solidState, HIGH);
delay(14400000);
digitalWrite(solidState, LOW);
}
// called when key goes from not pressed to pressed
void keyPress() {
longKeyPressCount = 0;
}
void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(keyPin);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keyPress();
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
else if (currKeyState == LOW) {
longKeyPressCount++;
}
prevKeyState = currKeyState;
}
}
This part works.
Now, i want to make it when you'd press the button for longer than 2 seconds while the fountain is on, the fountain gets turned off, so i added
void resetPomp(){
if((pompaan==1)&&(longKeyPressCount >= longKeyPressCountMax)) {
digitalWrite(solidState, LOW);
}
But, when i press the button for >2 seconds while the pump is on, nothing happens.
I have no idea why, tried some things but nothing seems to work.
Any idea what might be the solution to this problem ? Any help would be much appreciated ![]()