How do I turn on or off an array of LEDs based on the value of a variable? I've created a function whereby an int ('cleanCount') automatically goes up or down (0-1000) based on the situation. The value changes every few milliseconds. 

Based on the value, I would like to turn on or off LEDs. Let's say for every increment of '100' another LED goes on. So if the value is 359 there are 3 leds on, if It would been 899 there would've been 8 LEDs on. 

I could simply write

if (cleanCount &lt;= 100) { // turn on NO LEDs
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    ...
}

...

else if (cleanCount &lt;= 500) { // turn on 4 LEDs
    ...
    digitalwrite(led3, HIGH);
    digitalwrite(led4, HIGH);
    digitalwrite(led5, LOW);
    digitalwrite(led6, LOW);
    ...
}


but that would be a big pile of code which could be way more efficient. Perhaps.. my code isn't even correct.

So how could this be more efficient or perhaps actually working?



The whole code, here below.

int cleanCount = 1000;
int softpotPin = A0; //analog pin 0



void setup () {
    Serial.begin(9600);
    digitalWrite(softpotPin, HIGH); //enable pullup resistor    
}


void loop() {
    int softpotReading = analogRead(softpotPin);
    if (softpotReading &lt; 980) { // IF AANGERAAKT
        digitalWrite(werktIeLED, HIGH); 
        ++cleanCount;
        Serial.println(cleanCount);
        delay(16); // 5x zo snel als leeglopen
    }
    else if (softpotReading &gt; 980) { // IF NIET AANGERAAKT
        digitalWrite(werktIeLED, LOW); 
        --cleanCount;
        Serial.println(cleanCount);
        delay(64); // 1000 / 60 seconden
    }
    else {      // Als er een fout optreed..
        Serial.println("Fout!");
        digitalWrite(werktIeLED, HIGH);    
        delay(250);               
        digitalWrite(werktIeLED, LOW);    
        delay(250);               
    }


}
Here is a suggestion with the led pins in an array that you loop over.

I suggest making the array global, so put it outside any functions (usually at the top):

byte led_pins[] = {pin1, pin2, pin3, pin4, pin5, pin6, pin7, pin8};


Then put this in your loop (assumes that you have a int called cleanCount that is in scope:

const int increment = 100;

for (int i = 0; i &lt; sizeof(led_pins) / sizeof(led_pins[0]); i++) {
  boolean onOrOff = cleanCount &gt; (i+1) * increment;
  digitalWrite(led_pins[i], onOrOff);
}