I need some help with the sketch i am working on when I have only one function running the sketch functions correctly. If I only have just the sump related functions running they work correctly,if the ATO functions are running they work correctly,ETC
But when i try to have two functions working together only one will work the other acts as if it is commented out .
This sketch is to control the water levels in 3 separate tanks.
1- Sump tank
2- ATO tank aka Auto Top Off tank
3- W/C tank aka Water Change tank.
1-It is supposed to pump water from the ATO tank into the sump tank to replace evaporated water.
2-Refill the ATO tank once it gets low back to full level.
3- Finally to pump water from the ATO tank into the W/C tank to replace water that has been used for a water change in a salt water aquarium system.
here is the sketch any help would be greatly appreciated
thanks James
byte sumplowWaterPin = 24;
byte atolowWaterPin = 23;
//byte wclowWaterPin = 34;
byte sumpmidWaterPin =26;
byte sumphighWaterPin = 25;
byte atohighWaterPin = 22;
//byte wchighWaterPin = 37;
byte sumppumpPin = A9;
byte atopumpPin = A8;
//byte wcpumpPin = 30;
unsigned long maxsumpRunTime = 8000;
unsigned long maxatoRunTime = 8000;
//unsigned long maxwcRunTime = 8000;
unsigned long minsumpOffTime = 4000;
unsigned long minatoOffTime = 4000;
//unsigned long minwcOffTime = 4000;
unsigned long sumpswitchDebounceTime = 10;
unsigned long atoswitchDebounceTime = 10;
//unsigned long wcswitchDebounceTime = 10;
unsigned long lastsumpPumpTime = 0;
unsigned long lastatoPumpTime = 0;
//unsigned long lastwcPumpTime = 0;
unsigned long lastsumpmidWaterDetectTime = 0;
unsigned long lastatolowWaterDetectTime = 0;
//unsigned long lastwclowWaterDetectTime = 0;
boolean lastmidsumpWaterState = HIGH;
boolean lastlowatoWaterState = HIGH;
//boolean lastlowwcWaterState = HIGH;
boolean sumppumpRunning = true;
boolean atopumpRunning = true;
//boolean wcpumpRunning = true;
void setup() {
Serial.begin (9600);
pinMode (sumpmidWaterPin, INPUT_PULLUP);
pinMode (sumplowWaterPin, INPUT_PULLUP);
pinMode (atolowWaterPin, INPUT_PULLUP);
//pinMode (wclowWaterPin, INPUT_PULLUP);
pinMode (sumphighWaterPin, INPUT_PULLUP);
pinMode (atohighWaterPin, INPUT_PULLUP);
//pinMode (wchighWaterPin, INPUT_PULLUP);
pinMode (sumppumpPin, OUTPUT);
pinMode (atopumpPin, OUTPUT);
//pinMode (wcpumpPin, OUTPUT);
}
void loop(){
unsigned long currentMillis = millis();
boolean midsumpWaterState = digitalRead(sumpmidWaterPin);
boolean highsumpWaterState = digitalRead(sumphighWaterPin);
if(midsumpWaterState != lastmidsumpWaterState){
lastsumpmidWaterDetectTime = currentMillis;
}
if (sumppumpRunning) { // if the pump is on then let's see if we should turn it off yet
if ((highsumpWaterState == HIGH) || (currentMillis - lastsumpPumpTime >= maxsumpRunTime)){
digitalWrite(sumppumpPin, HIGH);
sumppumpRunning = false;
lastsumpPumpTime = currentMillis;
}
}
else { // pump is not running, see if we need to turn it on
if((midsumpWaterState == HIGH) && (currentMillis - lastsumpmidWaterDetectTime >= sumpswitchDebounceTime) && (currentMillis - lastsumpPumpTime > minsumpOffTime)){ // switch is low and has been for at least 3 seconds
digitalWrite(sumppumpPin, LOW);
sumppumpRunning = true;
lastsumpPumpTime = currentMillis;
}
}
lastmidsumpWaterState = midsumpWaterState;
boolean lowatoWaterState = digitalRead(atolowWaterPin);
boolean highatoWaterState = digitalRead(atohighWaterPin);
if(lowatoWaterState != lastlowatoWaterState){
lastatolowWaterDetectTime = currentMillis;
}
if (atopumpRunning) { // if the pump is on then let's see if we should turn it off yet
if ((highatoWaterState == LOW) || (currentMillis - lastatoPumpTime >= maxatoRunTime)){
digitalWrite(atopumpPin, HIGH);
atopumpRunning = true;
lastatoPumpTime = currentMillis;
}
}
else { // pump is not running, see if we need to turn it on
if((lowatoWaterState == HIGH) && (currentMillis - lastatolowWaterDetectTime >= atoswitchDebounceTime) && (currentMillis - lastatoPumpTime > minatoOffTime)){ // switch is low and has been for at least 3 seconds
digitalWrite(atopumpPin, LOW);
atopumpRunning = false;
lastatoPumpTime = currentMillis;
}
}
lastlowatoWaterState = lowatoWaterState;
//boolean lowwcWaterState = digitalRead(wclowWaterPin);
//boolean highwcWaterState = digitalRead(wchighWaterPin);
//if(lowwcWaterState != lastlowwcWaterState){
//lastwclowWaterDetectTime = currentMillis;
//}
//if (wcpumpRunning) { // if the pump is on then let's see if we should turn it off yet
//if ((highwcWaterState == HIGH) || (currentMillis - lastwcPumpTime >= maxwcRunTime)){
//digitalWrite(wcpumpPin, HIGH);
//wcpumpRunning = false;
//lastwcPumpTime = currentMillis;
//}
//}
//else { // pump is not running, see if we need to turn it on
//if((lowwcWaterState == HIGH) && (currentMillis - lastwclowWaterDetectTime >= wcswitchDebounceTime) && (currentMillis - lastwcPumpTime > minwcOffTime)){ // switch is low and has been for at least 3 seconds
//digitalWrite(wcpumpPin, LOW);
//wcpumpRunning = true;
//lastwcPumpTime = currentMillis;
//}
//}
//lastlowwcWaterState = lowwcWaterState;
}
Don't you have your true and false backwards here. If the pump is on and it's time to turn it off you set atopumpRunning to true? It already was true, so the else clause will certainly never happen again.
if (atopumpRunning) { // if the pump is on then let's see if we should turn it off yet
if ((highatoWaterState == LOW) || (currentMillis - lastatoPumpTime >= maxatoRunTime)){
digitalWrite(atopumpPin, HIGH);
atopumpRunning = true;
lastatoPumpTime = currentMillis;
}
}
else { // pump is not running, see if we need to turn it on
if((lowatoWaterState == HIGH) && (currentMillis - lastatolowWaterDetectTime >= atoswitchDebounceTime) && (currentMillis - lastatoPumpTime > minatoOffTime)){ // switch is low and has been for at least 3 seconds
digitalWrite(atopumpPin, LOW);
atopumpRunning = false;
lastatoPumpTime = currentMillis;
}
}
ok i made sure that the ATO function is working correctly when i load the sketch below with the sump and W/C functions commented out.
When i load the above sketch with the sump functions included in the sketch it works correctly but the ATO function no longer works.
i have checked the ATO and sump functions separately and have made sure that they work correctly when only one is loaded, but when both are enabled only the sump functions works the ATO functions act like they are commented out.
//byte sumplowWaterPin = 24;
byte atolowWaterPin = 23;
//byte wclowWaterPin = 34;
//byte sumpmidWaterPin =26;
//byte sumphighWaterPin = 25;
byte atohighWaterPin = 22;
//byte wchighWaterPin = 37;
//byte sumppumpPin = A9;
byte atopumpPin = A8;
//byte wcpumpPin = 30;
//unsigned long maxsumpRunTime = 8000;
unsigned long maxatoRunTime = 8000;
//unsigned long maxwcRunTime = 8000;
//unsigned long minsumpOffTime = 4000;
unsigned long minatoOffTime = 4000;
//unsigned long minwcOffTime = 4000;
//unsigned long sumpswitchDebounceTime = 10;
unsigned long atoswitchDebounceTime = 10;
//unsigned long wcswitchDebounceTime = 10;
//unsigned long lastsumpPumpTime = 0;
unsigned long lastatoPumpTime = 0;
//unsigned long lastwcPumpTime = 0;
//unsigned long lastsumpmidWaterDetectTime = 0;
unsigned long lastatolowWaterDetectTime = 0;
//unsigned long lastwclowWaterDetectTime = 0;
//boolean lastmidsumpWaterState = HIGH;
boolean lastlowatoWaterState = HIGH;
//boolean lastlowwcWaterState = HIGH;
//boolean sumppumpRunning = true;
boolean atopumpRunning = true;
//boolean wcpumpRunning = true;
void setup() {
Serial.begin (9600);
//pinMode (sumpmidWaterPin, INPUT_PULLUP);
//pinMode (sumplowWaterPin, INPUT_PULLUP);
pinMode (atolowWaterPin, INPUT_PULLUP);
//pinMode (wclowWaterPin, INPUT_PULLUP);
//pinMode (sumphighWaterPin, INPUT_PULLUP);
pinMode (atohighWaterPin, INPUT_PULLUP);
//pinMode (wchighWaterPin, INPUT_PULLUP);
//pinMode (sumppumpPin, OUTPUT);
pinMode (atopumpPin, OUTPUT);
//pinMode (wcpumpPin, OUTPUT);
}
void loop(){
unsigned long currentMillis = millis();
//boolean midsumpWaterState = digitalRead(sumpmidWaterPin);
//boolean highsumpWaterState = digitalRead(sumphighWaterPin);
//if(midsumpWaterState != lastmidsumpWaterState){
//lastsumpmidWaterDetectTime = currentMillis;
//}
//if (sumppumpRunning) { // if the pump is on then let's see if we should turn it off yet
//if ((highsumpWaterState == HIGH) || (currentMillis - lastsumpPumpTime >= maxsumpRunTime)){
//digitalWrite(sumppumpPin, HIGH);
//sumppumpRunning = false;
//lastsumpPumpTime = currentMillis;
//}
//}
//else { // pump is not running, see if we need to turn it on
//if((midsumpWaterState == HIGH) && (currentMillis - lastsumpmidWaterDetectTime >= sumpswitchDebounceTime) && (currentMillis - lastsumpPumpTime > minsumpOffTime)){ // switch is low and has been for at least 3 seconds
//digitalWrite(sumppumpPin, LOW);
//sumppumpRunning = true;
//lastsumpPumpTime = currentMillis;
//}
//}
//lastmidsumpWaterState = midsumpWaterState;
boolean lowatoWaterState = digitalRead(atolowWaterPin);
boolean highatoWaterState = digitalRead(atohighWaterPin);
if(lowatoWaterState != lastlowatoWaterState){
lastatolowWaterDetectTime = currentMillis;
}
if (atopumpRunning) { // if the pump is on then let's see if we should turn it off yet
if ((highatoWaterState == HIGH) || (currentMillis - lastatoPumpTime >= maxatoRunTime)){
digitalWrite(atopumpPin, HIGH);
atopumpRunning = false;
lastatoPumpTime = currentMillis;
}
}
else { // pump is not running, see if we need to turn it on
if((lowatoWaterState == LOW) && (currentMillis - lastatolowWaterDetectTime >= atoswitchDebounceTime) && (currentMillis - lastatoPumpTime > minatoOffTime)){ // switch is low and has been for at least 3 seconds
digitalWrite(atopumpPin, LOW);
atopumpRunning = true;
lastatoPumpTime = currentMillis;
}
}
lastlowatoWaterState = lowatoWaterState;
//boolean lowwcWaterState = digitalRead(wclowWaterPin);
//boolean highwcWaterState = digitalRead(wchighWaterPin);
//if(lowwcWaterState != lastlowwcWaterState){
//lastwclowWaterDetectTime = currentMillis;
//}
//if (wcpumpRunning) { // if the pump is on then let's see if we should turn it off yet
//if ((highwcWaterState == HIGH) || (currentMillis - lastwcPumpTime >= maxwcRunTime)){
//digitalWrite(wcpumpPin, HIGH);
//wcpumpRunning = false;
//lastwcPumpTime = currentMillis;
//}
//}
//else { // pump is not running, see if we need to turn it on
//if((lowwcWaterState == HIGH) && (currentMillis - lastwclowWaterDetectTime >= wcswitchDebounceTime) && (currentMillis - lastwcPumpTime > minwcOffTime)){ // switch is low and has been for at least 3 seconds
//digitalWrite(wcpumpPin, LOW);
//wcpumpRunning = true;
//lastwcPumpTime = currentMillis;
//}
//}
//lastlowwcWaterState = lowwcWaterState;
}
arduinodlb you are correct i have 3 things that i want to run in parallel but when more than one is enabled the first function the sump level control code is the only one that will work i havent verified that the settings for the W/c function works yet but that is not a big deal my problem is getting them all to work at the same time.
could i be trying to have the arduino do too much at the same time?
should i have went about getting these functions to work in a different way?
the sketch was given to me on a different site and only for one function the sump water level control i expanded upon it to do the 3 functions, when i was doing the expansion and found that it compiled i was thrilled that i had accomplished this but when putting it into use i found out that seperately they worked but wouldnt work when put together.
Look at reply #1. Then look at the code that doesn't work, it's cut out and posted in reply #1. Then go look at the same section in your working code. If you still don't see it, then go back to reply #1 and read it again and repeat the process until you do.
I'll even give you a hint. Something is very different about the code that works and the code that dosn't and it has nothing to do with the commented out code. There is something very different about the ato section in those two pieces of code.
i corrected the true or false statement to make the ATO function work correctly those changes were made when i was trying to make the Sump and ATO functions work together at the same time.
have i missed something else that could be causing them to not work together?
The sump function starts to pump water when the high sump switch is in a high state and the mid sump switch is in a low state once the pump starts it runs until the mid sump switch is back in a high state and the high sump switch is back to its low state.This way the pump will only run once a day for around 1/2 hour to replenish the evaporated water from the sump tank. when this part of the sketch is only enabled it functions as it should.
The ATO function opens a solenoid that refills the ATO storage tank once the high ATO switch is in a high state and the low ATO switch is in a low state again.
the solenoid is energized until the switches are back to their full tank state. this way the solenoid is only energized once every 2 to 3 days and runs for a few hours refilling the ATO tank to cut down on wear on the water filter that removes all contaminates from the water. When this part of the sketch is only enabled it functions correctly.
when i have the sump and ATO functions enabled in the sketch the sump function,functions as it should but the ATO function will not behave as it should it acts as if i have it commented out of the sketch.
all 3 systems function in the same way but when 2 or all 3 functions are enabled in the sketch only the sump function behaves as it should, the others will not function as they should, as if they are both commented out even though they are not.
So what you're trying to say is that the ato and water change refil pumps never come on?
Try putting some Serial prints in some strategic places and watch with the serial monitor while the program is running. Make sure the code is going where you expect it.
I notice that these are all initialized to true. So when the code first runs, it will think all three pumps are already on, but no code will have actually turned them on yet.
Still, it is going to wait at least the 4 second minimum off time before it will come back on and maybe the 8 second maximum depending on how the switches are sitting. If you keep fiddling with the switches during that time tring to make it work and don't wait the minimum off time, then the pumps will never come on.
One more question. How have you got this all wired up? You're not driving relays straight off the board or anything are you? That can cause the old any one piece work but they all don't work together syndrome.
As far as i can see from your desription and your code you want every pump to be controlled from a state machine described by the attached diagram. If correct it would be quite easy to code a state machine that corresponds to the diagram.
none of the functions start until the lower switch is triggered then the function runs until the upper switch is triggered again signalling that the tank is again full, with a time limit on the run time length to avoid over filling when a float switch does malfunction which will happen sooner or later!
the run times will have to be adjusted to correspond with the actual times it takes the pump to refill the sump tank and the time it takes the water filtration system i have in use to refill the ATO and water change tanks respectively.
I am also going to incorporate ping sensors into the sketch to back-up the float switches and the max run times to further avoid any chance of over filling and resulting water spillage onto the main floor of my home. Which has happened twice now because i have been doing these chores manually and the wife distracted me with some other chore LOL!
Delta_G
As to the (boolean pump running true states) that was the way it was suggested to be done by another person if you think I should change this, should it just be changed to false or done another way? I am a noob at arduino programming trying to learn the proper way of getting what I want to accomplish done. The mechanical and electrical part of this project is no problem for me.
again thanks for all your suggestions and use of your knowledge!
James