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 <= 100) { // turn on NO LEDs
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
...
}
...
else if (cleanCount <= 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 < 980) { // IF AANGERAAKT
digitalWrite(werktIeLED, HIGH);
++cleanCount;
Serial.println(cleanCount);
delay(16); // 5x zo snel als leeglopen
}
else if (softpotReading > 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);
}
}
examples>Display>barGraph
maps analog values to ten LEDsmap()
function and will lite an LED per 100, it also keeps LEDs on below the current lit