Automatic Aquarium water changer

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
  
}

What is the problem with Your code? What happends and what does not happend?

looks like you constantly fill and drain.

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
  
}

I need to run one cycle when manually powered the device.

MG1997:
I need to run one cycle when manually powered the device.

check the code in post #2
you would need to re-set the power to the Arduino for it to perform one cycle.

But according my code it keeps on draining and filling. (Even if the tank is full)

Yes. You have to close the filling when You set Cycle_complete=1.That looks like missing.

Railroader:
Yes. You have to close the filling when You set Cycle_complete=1.That looks like missing.

Sorry I didn't got that.

Like cleaning up when activities are finished....

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?

To run once, move the code to setup() rather than in loop().

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?

Thanks.. Tom... :slight_smile:

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
int pump = 11;
int drain_valve = 12;
int top_switch = 2;
int bottom_switch =3;
int Top_switch_status ;
int Bottom_switch_status ;
int cycle = 0;

void setup()
{
    Serial.begin(9600);
    pinMode(top_switch, INPUT);
    pinMode(bottom_switch, INPUT);
    pinMode(drain_valve, OUTPUT);
    pinMode(pump, OUTPUT);
}

void loop() 
{
   
   Top_switch_status = digitalRead(top_switch);
   Bottom_switch_status = digitalRead(bottom_switch); 

    if(Top_switch_status == 1 && Bottom_switch_status == 1 && cycle ==0)     //full tank
    {
        digitalWrite(drain_valve, HIGH);//Draining.............
        digitalWrite(pump, LOW);   
    }

    else if(Top_switch_status == 0 && Bottom_switch_status == 0 && cycle ==0)   //Drained
    {
        stat = 1;
        digitalWrite(drain_valve, LOW);
        digitalWrite(pump, HIGH);       // Filling Fresh         
    }
    
    else if(Top_switch_status == 1 && Bottom_switch_status == 1 && cycle != 0)     //full tank
    {
        digitalWrite(drain_valve, LOW);// Do nothing.............
        digitalWrite(pump, LOW);  
    } 
}

Hi,
Please read post #13 with reguard to how you have your float switches wired.

Thanks.. Tom... :slight_smile: