I am measuring the level of a water tank using the analog input 0 (A0) with the following characteristics:
- Initially after power up, the current level (levelValue) of the tank is the initial set-point level (setLevel).
- Every time the level of the tank decreases, the set-point follows the current level.
- Every time the level increases, the digital output 0 is SET making a pump to run (to add water to the tank)
for an amount of time proportional to the increasing of the level (deltaLevel*100). - Once the time is reached, the pump stops.
Now, the level of the tank should be greater than the level that tripped the pump. I have written a code that performs
the above tasks but during the pumping time (delay), I have to wait to 'see' the current level tank.
I need help with a simple logic using timers or interrupts to do the previous level control and measuring the level of the tank every second (even when the pump is running).
Here my base code:
int levelPin = A0;
int pumpPin = 0;
int levelValue = 0;
int setLevel = 0;
int deltaLevel = 0;
void setup() {
Serial.begin(9600);
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, LOW); //Pump initially stopped
setLevel = analogRead(levelPin);
}
void loop() {
levelValue = analogRead(levelPin);
if(levelValue <= setLevel + 1)
{
setLevel = levelValue; //set-point follows the current level
}
Serial.print("Set level= ");
Serial.println(setLevel); // show the level value
Serial.print("Current level= ");
Serial.println(levelValue); // show the set level value
delay(1000); // wait for a second
deltaLevel = levelValue - setLevel;
if(deltaLevel > 0) //The level of the tank increases
{
digitalWrite(pumpPin, HIGH); // turn the pump on
delay(deltaLevel*100); // pumping time
digitalWrite(pumpPin, LOW); // turn the pump off
setLevel = levelValue;
}
}
I tried to use the Timer Library...
http://playground.arduino.cc/Code/Timer
...but the periods or duration of the functions is a fix value.
Any help will be highly appreciated. Palliser.