Unfortunately I think you need a different approach. Consider this:
1 2 3
|
if(digitalRead(potValue) >= 921);
{digitalWrite(Pump1, HIGH);
delay(30000);}
|
This starts pump1 and then waits 30 seconds. What if the water pumps out in 10 seconds? You could empty the sump and run the pump dry which will burn it out if you do it enough.
A better approach would be for loop() to do something like this:
1 2 3 4 5 6
|
loop()
{
// examine the state of the system
// make any changes necessary
sleep(1);
}
|
In other words, once per second you wake up, see what's going on, and change things accordingly.
You have to read the pot pin with analogRead() instead of digitalRead()
There's another nasty gotchya: when the water level is right near the high or low mark, different readings will show different values. For example, with the high mark at 921, you're likely to see readings like 920 922 919 918 921 925 920 920 923 921 922 918... You can't assume that each transition from "below high" to "above high" is a transition that you should act upon.
To avoid this, you should a technique called hysteresis. Keep a variable that says whether you were last in a low water condition or high water. A transition below the low reading only "counts" if the current state is high water. And a transition above the high reading only counts if the current state is low. You can represent the state with a single boolean
1 2 3
|
// set when transitioning above high mark. Cleared when transitioning below
// low mark
bool isHigh=false;
|
It's handy to think about what events can cause something to happen. There are 4 of them:
- transition to high water
- transition to low water
- first timer triggers
- second timer triggers
If you think of it this way, the code is very easy to write. Here is a skeleton of the code. Just fill in the code where I have the comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
int HighWaterMark = 1023*0.9;
int LowWaterMark = 1023*0.1;
int Pump1=8;
int Pump2=9;
int Alarm=7;
int potPin= 1;
// The pumps. start pumps[0] first and pumps[1] second.
// swap values when you reach the low water mark.
int pumps[2] = {Pump1, Pump2};
// set when above high mark. Cleared when transitioning below
// low mark
bool isHigh=false;
// Timer 1 is set to 30 seconds after pump1 turns on.
// Timer 2 is set to 30 seconds after pump2 turns on.
time_t timer1;
time_t timer2;
void setup() {
pinMode(Pump2,OUTPUT);
pinMode(Pump1,OUTPUT);
pinMode(Alarm,OUTPUT);}
void loop()
{
// read the water level and get the current time.
int potValue = analogRead(potPin);
time_t now = time(0);
if (timer1 && timer1 < now) {
// first timer triggered
// start second pump
// set timer2 to 30 seconds from now
// disable timer1 by setting it to zero.
} else if (timer2 && timer2 < now) {
// second timer triggered
// set alarm
// disable timer2
} else if (potValue < LowWaterMark && isHigh) {
// transition to low water level
// change isHigh to indicate low water
// disable both timers, just to be sure
// turn off both pumps
// turn off alarm in case it was on
// swap pumps[0] and pumps[1] so the next time you'll use them in the opposite order
} else if (potValue > HighWaterMark && !isHigh) {
// level reached high
// turn on pumps[0]
// set timer1 to 30 seconds from now;
set isHigh to indicate you've reached high water.
}
// now sleep for 1 second to conserve power.
sleep(1);
}
|