Hi everyone, this is my first time having to use code for the arduino and was hopeing someone with more experience could check over my code. below is the requirements for the project followed by my code. Thank you.
A control system used to maintain the water level in a
sump between the levels high and low. Two transducers (HIGH and LOW) are
used to monitor the level. The water is pumped out by two pumps, P1 and P2.
The water level in the sump is to be controlled as follows:
If the level reaches high one pump will start to pump out.
If the level does not reach low within about 30 seconds, the second pump
will come into action. If the level does reach low before 30 second
pumping should stop.
If the level does not reach low within about 30 seconds of the second
pump starting, an alarm is given. The alarm is sounded until the level
reaches low. If the level does reach low before 30 seconds the pumping
should stop.
The pumps will stop when low is detected.
To give even loading of the pumps they are to be used alternately, e.g. if
on one pump cycle P1 starts first and P2 is the ‘back up’ then on the next
cycle P2 will start first and P1 is the ‘back up’
I'm using the motor simulators to
model the two pumps. Also a potentiometer
as a ‘level’ transducer so that, say, 10% travel is ‘low level’ and 90% ‘high
level’.
int HighWaterMark = 10230.9;
int LowWaterMark = 10230.1;
int Pump1=8;
int Pump2=9;
int Alarm=7;
int potPin= 1;
int potvalue= A5;
// The pumps. start pumps[0] first and pumps[1] second.
// swap values when you reach the low water mark.
int pumps[2] = {Pump1, Pump2};
int delayTime = 0;
void setup() {
pinMode(Pump2,OUTPUT);
pinMode(Pump1,OUTPUT);
pinMode(Alarm,OUTPUT);
}
void loop()
{
delayTime = 0; //This is required to ensure that delayTime is 0 at the begining
//Stop pump1 if running //This is required to ensure that pump1 is at rest at the begining
//Stop pump2 if running//This is required to ensure that pump2 is at rest at the begining
int potValue = analogRead(potPin);
if(potValue>=HighWaterMark){ //if potValue exceeding the upper mark enter the loop
//Trigger First Pump
while(delayTime < 30000){ //delay for max 30 sec by running this loop[Note that I am not considering loop execution time, if you want to consider, take 1 or 2 sec less in the condition]
delay(1000);//delay 1 sec
delayTime+=1;
int potValue = analogRead(potPin);
if(potValue<=LowWaterMark){
break; //If reached lower mark before 30 sec exit the loop
}
}
if(delayTime >= 30000){ //SECOND PUMP Enter this loop if potValue fails to reach lower mark after 30 sec of running 1st pump
delayTime = 0;
int potValue = analogRead(potPin);
if(potValue>LowWaterMark){ //if potValue is still above lower mark enter the loop
//Trigger Second Pump
while(delayTime < 30000){ //delay for max 30 sec by running this loop[Note that I am not considering loop execution time, if you want to consider, take 1 or 2 sec less in the condition]
delay(1000); //delay 1 sec
delayTime+=1;
int potValue = analogRead(potPin);
if(potValue<=LowWaterMark){
break; //If reached lower mark before 30 sec exit the loop
}
}
if(delayTime >= 30000){ //ALARM Enter this loop if potValue fails to reach lower mark after 30 sec of running 2nd pump
delayTime = 0;
int potValue = analogRead(potPin);
//Trigger alarm
while(potValue>LowWaterMark){ //delay until potValue reach lower mark
int potValue = analogRead(potPin);
// delay for few time in between(optional)
}
//Stop the alarm
}
//Stop pump2
}
}
//Stop pump1
// swap pumps[0] and pumps[1] so the next time you'll use them in the opposite order
}
//sleep(1); or delay(1000); if you want to give rest
}