I am working on automation of aquarium. In which when the device is powered it will check the water level in the tank. It will drain water it the lower float switch (There will be 2 float switch( one on the top and other in the bottom).Once water is drained till lower indicator. Then it will add fresh water with another water pump, till the top float switch. Then the system should stop. It should only work once when the device is powered.
I am facing problem in coding. Please check my code. Now when the device is turned on both the drain pump and filling pump is turning on
CODE
int pump = 9;
int drain_valve = 8;
int top_switch = 7;
int bottom_switch =6;
int Top_switch_status = 0;
int Bottom_switch_status = 0;
void setup()
{
Serial.begin(9600);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
//Initial both pumps are off
digitalWrite(drain_valve,LOW);
digitalWrite(pump,LOW);
}
void loop()
{
// 0 Full Water / default
// 1 No Water
Top_switch_status = digitalRead(top_switch);
Bottom_switch_status = digitalRead(bottom_switch);
do
{
digitalWrite(drain_valve,HIGH); // Removes water
}while(Bottom_switch_status == 1);// Till the lower float switch
delay(1000);
do
{
digitalWrite(pump,HIGH); // Filling water
}while(Top_switch_status == 1);// Till the Top float switch
}
if you add a flag after the cycle, you can make it work only one time.
but then what ? how often do you want to do this ?
is it automatic or just once ?
how do you start it ? or decide when to have it run ?
int pump = 9;
int drain_valve = 8;
int top_switch = 7;
int bottom_switch =6;
int Top_switch_status = 0;
int Bottom_switch_status = 0;
int cycle_complete ;
void setup()
{
Serial.begin(9600);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
//Initial both pumps are off
digitalWrite(drain_valve,LOW);
digitalWrite(pump,LOW);
cycle_complete=0;
}
void loop()
{
// 0 Full Water / default
// 1 No Water
Top_switch_status = digitalRead(top_switch);
Bottom_switch_status = digitalRead(bottom_switch);
If(cycle_complete=0{
do
{
digitalWrite(drain_valve,HIGH); // Removes water
}while(Bottom_switch_status == 1);// Till the lower float switch
delay(1000);
do
{
digitalWrite(pump,HIGH); // Filling water
}while(Top_switch_status == 1);// Till the Top float switch
Cycle_complete=1;
) // end of if cycle complete
}
A very compact and overwiewable code Delta_G but to me it looks like a continous pumping water in and out. What about the stop after one draining and filling cycle?
avr_fred:
To run once, move the code to setup() rather than in loop().
Exactly...
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got your float switches wired, with pullup or pull down resistors?
Do your float switches close when lifted or when low?
Do you have a system to turn off if you have an overflow?
Or if a pump runs for an abnormally long period of time?
Will this be on a time switch to start or operator started?
The code dave-in-nj posted won't even compile, because it is missing a ")". And it won't work anyway, as comparisons are made with "==". Using "=" makes the if test false, always.
If(cycle_complete=0{
This is an infinite loop because the while condition cannot change.
do
{
digitalWrite(drain_valve,HIGH); // Removes water
}while(Bottom_switch_status == 1);// Till the lower float switch