Checking for a ground present on pin for a minimum time.

I am using a (very) old Duemilanove to control a valve which switches the water on to a shower in my garden. I want the shower to turn on when I press a button near the shower which puts a ground on pin 3 which then turns pin 2 high, driving a relay which sends 24v to the valve to open the valve. The Arduino then waits five minutes and then switches the valve off again. So the shower only has a water supply for 5 minutes at a time. The sketch works for two or three times but then remains on. Also when powering up, the relay is sometimes activated and one has to power up 2-3 times to get it to settle in the off state.

const int pushButton = 3;
const int led = 13;
const int relay = 2;
int minutes = 5;

void setup() {
  pinMode(pushButton, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
}

void loop() {
  //delay(5000);
  boolean buttonVal = digitalRead(pushButton); //No need for debounce because of toggleRelay()
  if (buttonVal == 0) { //check for low on pin 2 because of the internal pullup
    toggleRelay();
  }
}

void toggleRelay() {
  digitalWrite(relay, HIGH);
  digitalWrite(led, HIGH);
  delay(minutes * 60 * 1000UL);
  digitalWrite(relay, LOW);
  digitalWrite(led, LOW);
  delay(10000);
}

What can I do to increase the stability? I have thought of re-setting pin 2 to high at the end of the function toggleRelay. Would this fix the issue? Or, if it is stray interference triggering the pin2, to only trigger the function relayToggle if the ground on pin 2 is there for say, 100mS. If checking for a ground being present for a certain time is a solution, how would I program that?

I would start by using millis() timing for the "delays". Although the program does not need to do anything during the delay()s as currently written, switching to using millis() would allow you to print debugging information, such as pin states, during the "delay"

As for only starting if the input pin, (I assume that you mean pin 3), is LOW for a period you can save the value of millis() when the pin is LOW and check each time through loop() whether the required period has elapsed. Set the saved value to zero when the pin is HIGH and only check the elapsed period if the value is not zero

steveinaustria:
The Arduino then waits five minutes and then switches the valve off again. So the shower only has a water supply for 5 minutes at a time. The sketch works for two or three times but then remains on.

I can't reconcile that part of your problem description with your Title which says "Checking for a ground present on pin for a minimum time."

I wonder are there two or three separate problems that each need to be described separately?

if you just want to check that a pin is held LOW for a period of time you can do it like this (I am assuming you are using pinMode(buttonPin, INPUT_PULLUP) which keeps the pin HIGH when the button is not pressed.)

buttonValue = digitalRead(buttonPin);
if (buttonValue == HIGH) {
   lastTimeButtonWasHigh = millis();
}
if (millis() - lastTimeButtonWasHigh >= interval) {
   // button has been LOW throughout the interval
   // do something
}

However I can't see that having any relevance to the problems in the piece in quotes

...R

(I am assuming you are using pinMode(buttonPin, INPUT_PULLUP)

That is a good assumption seeing as that is what is written in the code in the OP.

However I can't see that having any relevance to the problems in the piece in quotes

Most of the bit you quoted was simply additional information which can safely be ignored if you don't want to know what I am actually tring to achieve. The important bit is that after two or three times the relay remains on and that somtimes on power up it switches on. I suspect (naively) that the reason may be interference grounding pin 3, but as you can see I am no expert.

steveinaustria:
That is a good assumption seeing as that is what is written in the code in the OP.

The bit you quoted was simply additional information. It can safely be ignored if you don't want to know what I am acrually tring to achieve.

If all that means that you prefer to ignore my suggestion and my comments then please feel free to do so. It was all made in good faith.

...R