 }
 
 void loop() {    
+  Serial.print("dropCounter: ");
+  Serial.println(dropCounter);
+  managePump();
+  makeADrop();
+}
 
-  // print the results to the serial monitor:
-        
-
-  // wait 2 milliseconds before the next loop
-  // for the analog-to-digital converter to settle
-  // after the last reading:
+void managePump() {
   sensorValue = analogRead(analogInPin);
   Serial.print("Sensor: ");
   Serial.println(sensorValue);
-  levelIsDown = sensorValue>512;
-  Serial.print("dropCounter: ");
-  Serial.println(dropCounter);
   
-  if (dropCounter>150) //This should normaly never happen
+  waterLevelIsUp = sensorValue>sensorMax/2;
+  
+  if (dropCounter>150) //This should normaly never happen, the sensor should start the pump before
   {
-    if (levelIsDown)
+    if (waterLevelIsUp)
     { //Too many drops without sensor activation: sensor is not working, stop everything!
       freeze = true;
     }


// These constants won't change.  They're used to give names
// to the pins used:
const int analogOutPin = 3; // Analog output pin that the LED is attached to
const int analogInPin = A0;
const int pumpPin = 4;
const int ledPin = 13;

boolean stringComplete = false;  // Whether the string is complete
int outputValue = 0;
float receivedValue = 0.0;
int sensorMax = 1024;

boolean waterLevelWasUp = true;
boolean waterLevelIsUp = true;
boolean freeze = false;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
    pinMode(pumpPin, OUTPUT); 
    pinMode(valvePin, OUTPUT); 
}

void serialEvent() {
