Hi,
First off a little introduction, I'm completely new to arduino but do have a pretty good electronics knowledge, I've purchased a couple of Arduino books and starter kits to learn, I've built a few of the basic projects and successfully written the code.
I'm now looking for some help with a project I've started making...
I have a ready made off the shelf Infra-Red based liquid level sensor, this is mounted inside an aquarium, when the water level in the aquarium drops (evaporation) the sensors output goes high (+5v)
and when the sensor is covered with a liquid its output goes low 0v (the output actually varies dependent on how much the sensor is covered, for me thou I'm happy to use it as a digital input).
So what I need to happen is when the water level drops it turns on a 240v pump to fill the aquarium
once the level is ok again, the pump stops.
So far I've built my circuit, written the code and all is well ![]()
/*
Aquarium Automatic Top Off
*/
const int PumpDelay=1000; // Delay time before pump starts to avoid stuttery operation
const int LevelSensor=12; // Connect to Display Tank IR Level Sensor
const int WaterPump=2; // Water Pump via Relay
#define LEDGreen 5 // Level OK LED
#define LEDRed 6 // Level Low LED
void setup() {
pinMode(LevelSensor, INPUT_PULLUP); // Display Tank IR Sensor
pinMode(WaterPump, OUTPUT); // Water Pump
pinMode(LEDGreen, OUTPUT); // Level OK LED
pinMode(LEDRed, OUTPUT); // Level Low LED
digitalWrite(WaterPump, LOW); // Pump OFF by default
}
void loop() {
if(digitalRead(LevelSensor) == HIGH)
{
delay(PumpDelay);
digitalWrite(WaterPump, LOW);
digitalWrite(LEDRed, LOW);
digitalWrite(LEDGreen, HIGH);
}
else
{
delay(PumpDelay);
digitalWrite(WaterPump, HIGH);
digitalWrite(LEDRed, HIGH);
digitalWrite(LEDGreen, LOW);
}
}
The above code works perfectly however I wanted to add to this some way of monitoring how long the pump has been running for, (in case the level senor fails) and if the pump run time exceeds a predetermined time threshold then it will shut off the pump, turn on an error led and halt the loop until a reset button is pushed.
I tried to achieve this with the code below and was so excited when it compiled thinking I've cracked it, that was until I physically wired it up and realized it doesn't work.
With the sensor placed in water it boots up and the green led comes on as expected, i take the sensor out of water and the red led comes on as expected, after that it just halts and goes no further...
/*
Aquarium Automatic Top Off - With Pump Run Monitor, IF Pump Runs for more than preset time then there is a possible issue with level
sensor so turn off pump and turn on Error LED and wait for Reset button to be pushed to restart the loop
*/
const int PUMPMAXRUNTIME=5000; // Set Max Time Pump Runs befor Time Out Interlock Occurs
int LevelSensor=2; // Connect to Display Tank IR Level Sensor
int WaterPump=4; // Water Pump via Relay
int LEDGreen=5; // Level OK LED
int LEDRed=6; // Level Low LED
int LEDError=7; // Pump has run too long via "PumpMAXRunTime" and possible sensor fault LED
int PumpDelay=1000; // Delay time before pump starts to avoid stuttery opeation
long startTime; // for Pump run Time Calculation (millis curent time)
long duration; // for Pump run Time Calculation (run time duration minus millis current time)
void setup() {
pinMode(LevelSensor, INPUT); // Display Tank IR Sensor
pinMode(WaterPump, OUTPUT); // Water Pump
pinMode(LEDGreen, OUTPUT); // Level OK LED
pinMode(LEDRed, OUTPUT); // Level Low LED
pinMode(LEDError, OUTPUT); // Pump has run too long and possible ensor fault LED
digitalWrite(WaterPump, LOW); // Pump OFF by default
}
void loop() {
if(digitalRead(LevelSensor) == LOW)
{
delay(PumpDelay);
digitalWrite(WaterPump, HIGH);
digitalWrite(LEDRed, HIGH);
digitalWrite(LEDGreen, LOW);
}
else
{
delay(PumpDelay);
digitalWrite(WaterPump, LOW);
digitalWrite(LEDRed, LOW);
digitalWrite(LEDGreen, HIGH);
}
if(digitalRead(WaterPump) == HIGH)
{
startTime = millis();
while(digitalRead(WaterPump) == HIGH);
long duration = millis() - startTime;
}
if(digitalRead(duration) >= PUMPMAXRUNTIME) // If pump runs for more than this time turn on Error LED & Halt Program until Reset)
{
digitalWrite(WaterPump, LOW);
digitalWrite(LEDError, HIGH);
digitalWrite(LEDGreen, LOW);
digitalWrite(LEDRed, LOW);
exit(0);
}
}
Any suggestions would be greatly appreciated, I guess I need to fully understand the millis function which I kind of do but struggling with the: start timer, if timer over runs, then stop, if not then carry on as normal.
In my world / understanding I'm basically trying to create a delay on timer
I've seen a few examples where others include librarys although id rather learn how to do it properly myself.