Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Arduino code for Automatic Christmas Tree Waterer
/*
Auto Tree Waterer 3.0
Self-Watering Christmas Tree
by Bill Catherall
Dec 10, 2012
Version 2.0 fix: Changed sensor readings to use analog inputs instead
of digital.
Version 3.0 fix: Turning off voltage to sensor wires until needed to
reduce electro etching. Taking readings less frequently.
*/
//default pump status off
int IndicatorLight = 1;
const int tank_led = 2; //Digital Output Pin 2
const int tree_led = 13; //Digital Output Pin 13
const int pump = 3; //Digital Output Pin 3
const int sensor = 10; //Digital Output Pin 10
const int low_water = A2; //Analog Pin 2
const int high_water = A3; //Analog Pin 3
const int tank_water = A4; //Analog Pin 4
long checkTankRate = 5000; //5 seconds
int blinkRate = 500; //0.5 seconds
long checkTreeRate = 300000; //5 minutes
int dryValue = 50;
int loopCount = 0;
int HighSensor = 0;
int LowSensor = 0;
int TankSensor = 0;
void setup() {
pinMode(pump, OUTPUT); //output to pump relay
pinMode(tank_led, OUTPUT); //output to Tank status light
pinMode(tree_led, OUTPUT); //output to Tree status light
pinMode(sensor, OUTPUT); //output to sensor
digitalWrite(pump, LOW); //Default pump relay off
digitalWrite(tank_led, HIGH); //Default Tank status light on
digitalWrite(tree_led, HIGH); //Default Tree status light on
digitalWrite(sensor, LOW); //Default sensors off
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
//turn on the sensors
digitalWrite(sensor, HIGH);
//read the water level sensor values
int HighSensor = analogRead(high_water);
int LowSensor = analogRead(low_water);
int TankSensor = analogRead(tank_water);
Serial.print("Tank=");
Serial.println(TankSensor);
Serial.print("High=");
Serial.println(HighSensor);
Serial.print("Low=");
Serial.println(LowSensor);
//Turn on the pump if tank has water and sensors are dry
//Turn off the pump when the high water sensor is wet
if (LowSensor < dryValue){
digitalWrite(tree_led, LOW);
Serial.println("Filling");
Serial.print("Low=");
Serial.println(LowSensor);
while ((HighSensor < dryValue) && (TankSensor >= dryValue)){
digitalWrite(pump, HIGH);
delay(500);
HighSensor = analogRead(high_water); //check high_water
TankSensor = analogRead(tank_water); //check tank_water
Serial.print("Tank=");
Serial.println(TankSensor);
Serial.print("High=");
Serial.println(HighSensor);
}
}
//Turn off the pump
digitalWrite(pump, LOW);
//Turn off the sensors
digitalWrite(sensor, LOW);
//when the tank is low and the tree is dry, blink the light
if ((LowSensor < dryValue) && (HighSensor < dryValue)){
while (TankSensor < dryValue){
loopCount = checkTankRate/blinkRate;
for (int num=1; num < loopCount; num++){
//toggle the indicator light status
IndicatorLight = (IndicatorLight + 1) % 2;
if (IndicatorLight == 1){
digitalWrite(tank_led, HIGH);
digitalWrite(tree_led, HIGH);
}
if (IndicatorLight == 0){
digitalWrite(tank_led, LOW);
digitalWrite(tree_led, LOW);
}
delay(blinkRate);
}
//turn on the sensors
digitalWrite(sensor, HIGH);
//read the water level sensor values
TankSensor = analogRead(tank_water);
Serial.print("Tank=");
Serial.println(TankSensor);
//turn off the sensors
digitalWrite(sensor, LOW);
}
}
//Turn the light back on
if (TankSensor >= dryValue){
IndicatorLight = 1;
digitalWrite(tank_led, HIGH);
}
if (LowSensor >= dryValue){
digitalWrite(tree_led, HIGH);
}
//Turn off the sensors
digitalWrite(sensor, LOW);
//slow down the program
delay(checkTreeRate);
//blink tree light to show loop end;
digitalWrite(tree_led, LOW);
delay(blinkRate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment