Arduino – Pond Filler Upper

I have this little pond see.  Its not terribly big and it looks quite nice – but it has a small leak somewhere.  And its driving me nuts. So in a moment of geekery I thought the problem could be solved with a bit of software and an Arduino board.  So I concocted a small plan to keep the pond topped up.  Nothing too complex – just used a water level sensor and a solenoid driven water valve.  I also used the excellent Ciseco relay for Arduino. Simple and elegant.  Some pics here of the installation.  It’s really just a prototype – if it works I’ll put a more rugged version together.

Parts List

  • Arduino Duemilove
  • Hose Solenoid ( This is one from Ebay ) Its 12v
  • Tank Sensor ( Again from Ebay )
  • Ciseco Relay 
  • Wires of various kinds
  • Project Box
  • Switches
  • LED
  • Resistors – 10k, 1k – switch controls
  • 12-9V Regulator – I used this with a Capacitor so I can drive everything off a 12v DV PSU
  • 12V DC PSU

This is the Sketch to control the board

The sketch is rather simple and a bit clumsy.  I used the timerone library.  It works by trying to detect if the water has been running for more than WATERTIMEOUT.  If it does it waits for WAITIME. If it does that more than SYSTEMJAMED times – then the system switches off.  This should protect it if the sensor jams open.  I reckoned 1 minute at my water pressure was enough to fix the leak.

Still got to check if the sensor bounces for a few seconds but so far it works quite well.

#include <TimerOne.h>

/*
 *  Pond Relay
 *  August 2011 (c) Dave Robertson
 *	Arduino based pond relay with CISECO relay http://www.ciseco.co.uk/content/
 */

#define SYSTEMJAMMED 10      // Number of times the system starts a wait look
#define WATERTIMEOUT 60    // Number of seconds to run the sensor
#define WAITTIME 60    // Number of seconds to wait until running again
// constants won't change. They're used here to
// set pin numbers:
const int sensorPin = 2;     // the water sensor switch
const int switch1Pin = 3;     // control switch 1 - Manual Override
const int switch2Pin = 5;     // control switch 2
const int ledPin =  13;      // the number of the LED pin ( ardunio diag only )
const int relayPin =  7;      // the number of the relay pin that controls the valve

long timerRunning;        // Number of seconds since we started
long waterrunningTimer = 0;
long waitingTimer=0;
const int ledStatus =  4;      // the number of the LED pin

int systemJammed=0;        // The system is jammed so switch it off
int overrideSystem=0;      // Override the system and shut off the valve regardless

int s1State = 0;         // variable for reading the pushbutton status
int s2State = 0;         // variable for reading the pushbutton status
int sensorState = 0;         // variable for reading the pushbutton status
int oldsensorState = 0;         // variable for reading the pushbutton status
int ledFlash =0;
int beginwait=0;    // Controls waiting for next

void setup()
{
  pinMode(10, OUTPUT);
  Timer1.initialize(1000000);         // initialize timer1, and set a 1/2 second period

  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt

  pinMode(ledPin, OUTPUT);
    pinMode(ledStatus, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(switch1Pin, INPUT);
  pinMode(switch2Pin, INPUT);
  pinMode(sensorPin, INPUT);

  pinMode(relayPin, OUTPUT);

  digitalWrite(relayPin, HIGH);
  delay(2);
    digitalWrite(relayPin, LOW);
  Serial.begin(9600);
  Serial.println("Starting Now 5 Second Delay");

  // Flash to indicate we starting up

  for (int i=0;i<100;i++)  {
        delay(50);
        digitalWrite(ledStatus, digitalRead(ledStatus) ^ 1);
  }
    Serial.println("Entering loop");
}


//
//  Timer call back here - update timers and LED for status
//
void callback()
{

  // Flash the led to show valve is on

    Serial.println(s1State);
    Serial.println(s2State);


  if (systemJammed>SYSTEMJAMMED) {

    Serial.println("System Jammed - all off");

    for(int i=0;i<10;i++) {
      digitalWrite(ledStatus, digitalRead(ledStatus) ^ 1);
      delay(1000);

    }
    return;
  }

//   if (ledFlash)
//      digitalWrite(ledStatus, LOW);
//   else
//      digitalWrite(ledStatus, HIGH);

    if (sensorState && !beginwait) {
       waterrunningTimer+=1;
       Serial.print("Water is running now for secs: ");
       Serial.println(waterrunningTimer);

    }

    timerRunning += 1;
    // We rest after 24 hours as this a fine time window for what we want
    if (timerRunning == 5184000) {
      timerRunning = 0;
      waterrunningTimer = 0;
      Serial.println("Time now");
    }

    // Increment the waiting timer
    if (beginwait) {
      digitalWrite(ledStatus, digitalRead(ledStatus) ^ 1);

      Serial.println("Water ran too long - switched off waiting");
      waitingTimer++;
    }
}

void loop()
{


  // Get the state of water sensor  and switches
  sensorState = digitalRead(sensorPin)^1;
  s1State = digitalRead(switch1Pin)^1;
  s2State = digitalRead(switch2Pin)^1;

  if (s2State == HIGH) {
   // System off - no action
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    waterrunningTimer=0;
    digitalWrite(ledStatus, LOW);
    oldsensorState = LOW;
    waitingTimer=0;
    beginwait=0;
    systemJammed=0;  // Reset jammed sensor counter

    //Timer1.stop();
    return;
  }
    else {
    //  Timer1.start();

    }

  if (s1State == HIGH) {
    // Override sensors and reset everything
    if ( oldsensorState == LOW)
    {
       waterrunningTimer = 0;
       oldsensorState = HIGH;
    }
    //digitalWrite(ledStatus, LOW);
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledStatus, HIGH);
    systemJammed=0;
    return;
   }

  if (systemJammed >  SYSTEMJAMMED)
     return;


  if (sensorState == LOW ) {  // Clear everything as water has filled
    // Sensor has read water filled
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    waterrunningTimer=0;
    digitalWrite(ledStatus, LOW);
    oldsensorState = LOW;
    waitingTimer=0;
    beginwait=0;
    systemJammed=0;  // Reset jammed sensor counter
  }


  if (waterrunningTimer > WATERTIMEOUT ) {
      // Water has been running for over  a minute and the sensor is still high

    sensorState=LOW;    // Switch it off
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    digitalWrite(ledStatus, LOW);

    oldsensorState = LOW;
    if (!beginwait) {
          Serial.println("Water running too long");
          waitingTimer=0;
          beginwait=1;
          systemJammed++;
      }


  }

  if (beginwait && waitingTimer>WAITTIME){

    Serial.println("Timeout - switching off");
    beginwait=0;
    waitingTimer=0;
    waterrunningTimer=0;

  }


    // The sensor state read

  if (sensorState == HIGH ) {

    if ( oldsensorState == LOW)
    {
       waterrunningTimer = 0;
       oldsensorState = HIGH;

    }
    //digitalWrite(ledStatus, LOW);

    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledStatus, HIGH);

  }

}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s