 void loop() {
 
 	unsigned long currentTime = millis();
+	int newLedLevel;
+
 	if ((unsigned long)(currentTime - timer) >= TIME_INTERVAL) {
 
 		// enable probe
 		digitalWrite(ENABLER_PIN, HIGH);
 		delay(100);
 		// read the value from the sensor:
-		sensorValue = analogRead(SENSOR_PIN);
+		sensorValue = readSensor();
 		// disable probe after reading values
 		digitalWrite(ENABLER_PIN, LOW);
 
-		// breakpoints for different led levels:
-		if (sensorValue > 550) ledLevel = 5;
-		if (sensorValue < 550) ledLevel = 4;
-		if (sensorValue < 450) ledLevel = 3;
-		if (sensorValue < 350) ledLevel = 2;
-		if (sensorValue < 250) ledLevel = 1;
-		setLedLevel(ledLevel);
+		// thresholds for different LED levels:
+		if (sensorValue >= 550) newLedLevel = 5;
+		if (sensorValue < 550) newLedLevel = 4;
+		if (sensorValue < 450) newLedLevel = 3;
+		if (sensorValue < 350) newLedLevel = 2;
+		if (sensorValue < 250) newLedLevel = 1;
+
+		// update if the value changes
+		if (ledLevel != newLedLevel) {
+			ledLevel = newLedLevel;
+			setLedLevel(ledLevel);
+		}
 
 		// debug data
 		Serial.print("[DEBUG]: current value: ");

		timer = currentTime;
	}

}

void setLedLevel(int level) {
	int ledArraySize = (sizeof(ledPins)/sizeof(int));
	if (level < 0) level = 0;
	if (level > (ledArraySize) )
		level = ledArraySize;

	for (int pin = 0; pin < ledArraySize; pin++ ) {
		pinMode(ledPins[pin], OUTPUT);

			digitalWrite(ledPins[pin], LOW);
	}
}

/**
 * Read moisture sensor multiple times and return the average
 * @return Integer
 */
int readSensor() {
	int average;
	float sensorValue, sum;
	for (int i = 0; i < 50; i++) {
		sensorValue = analogRead(SENSOR_PIN);
		sum += sensorValue;
		delay(1);
	}
	// cast to int
	average = (int)(sum/50.0);
	return average;
}
