Adding a timed fail safe and delay read to an autmatic top off

Hello,

I am making an automatic top off for an aquarium. There are two flow switches, when the lower one is activated it turns on a pump that adds water to the tank and then stops pumping when a second float switch is encountered. I have successfully used the following code to do this taken from another author, using the buttons for the float switches and the led for the pump:

 /*
  Button

Turns on and off a light emitting diode(LED) connected to digital 
pin 13, when pressing a pushbutton attached to pins 2 and 3.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* pushbutton attached to pin 3 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified 10 Apr 2012
by mpilchfamily

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin[] = {2,3};     // the number of the pushbutton pins
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  for(int x=0; x<2; x++)
  {
    pinMode(buttonPin[x], INPUT); 
  }  
}

void loop(){
  // read the state of the pushbutton value:
  for(int x=0; x<2; x++)
  {
    buttonState = digitalRead(buttonPin[x]);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH && buttonPin[x] == 2) {    
      // turn LED on:   
      digitalWrite(ledPin, HIGH); 
    }
    if (buttonState == HIGH && buttonPin[x] == 3) {
      // turn LED off:
      digitalWrite(ledPin, LOW);
    }
  }
}

I am horrible at coding so I cant figure out if this can be done and how to modify the above code to do so:

  1. I want a fail-safe such that if the high float switch fails to turn the pump off the pump will turn off after a predetermined amount of time.

  2. I also would like to make it so that float switches must be activated for a a second before a state in the pump is charged to ensure that the switch is actually being activated, ie not activated by a random temporary change in water level.

Thanks in Advance
Sean

Hi Sean, this shouldn't be difficult.

Some questions you need to think about: what is the minimum time the "low" float switch needs to be high before pumping should start; what are the minimum and maximum times the pump should run for; the minimum time the "high" float switch needs to be high to switch the pump off?

Have a look at the millis () function in the reference section. Also have a look at the switch debouncing example sketches. Your float switches are a little like switches with very long "bounce" periods, due to the ripples in the water. Your debounce periods need to be maybe ten seconds rather than a few tens of milliseconds.

Paul

  1. I want a fail-safe such that if the high float switch fails to turn the pump off the pump will turn off after a predetermined amount of time.

You probably also want to ensure that if this happens that the pumping system is locked out until you reset it.

Our old friend the Blink Without Delay example sketch shows the technique to use for timing things. You can easily use it to cause a wait period before acting on a switch and to manage a timeout for the filling process.

There is an extended example in the first post in this Thread.

...R