Read an input every second while write to an output for t seconds

I am measuring the level of a water tank using the analog input 0 (A0) with the following characteristics:

  1. Initially after power up, the current level (levelValue) of the tank is the initial set-point level (setLevel).
  2. Every time the level of the tank decreases, the set-point follows the current level.
  3. 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).
  4. 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.

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.

Here's a tank that needs some water. Here's a hose. Fill the tank to the line. How would YOU perform this task? Standing there with the water running for any period of time with your eyes closed is silly, isn't it. Well delay() does just that. You need water in the tank, you turn the water on. Then, periodically (read, on every pass through loop()) you check the water level. If it's still low, do nothing. If it's not, turn the water off.

Read, understand, and embrace the blink without delay philosophy, and never use delay() again.

The answer to such questions as this, often expressed as "I want/need to do two things at once", is usually to direct the questioner to the BlinkWithoutDelay example in the IDE. It will show you how to periodically check if an action is finished and, if not, carry on round the loop() function to do other things. In other words, make an action happen for a user specified time but without blocking the program in the way that delay() does.

For more detail see Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs