// Example of pretty code and a pretty circuit
//
// Version 1, 18 January 2022, by Koepel, Public Domain
// The led pins in a array makes the code easier to read
const int ledPins[] = { 8, 9, 10, 11, 12, 13 };
const int potPin = A0;
int oldIndex;
void setup()
{
// 9600 baud is so 1980's, let's do 115200 baud.
// The pinMode does not have to be set for a analog input.
// The range-based for-loop is a addition to the 'C++' language
Serial.begin( 115200);
for( auto a:ledPins)
{
pinMode( a, OUTPUT);
}
}
void loop()
{
// Using local variables where appropriate
// Assuming that VCC is 5.0V
// Using 1024 for the divider, because there are 1024 ADC steps.
int value = analogRead( potPin);
float voltage = float( value) * 5.0 / 1024.0;
Serial.print( "Potentiometer voltage is ");
Serial.println( voltage);
// Split the voltage into values for the leds.
// This is a so called stone-sifting-method.
// The big rocks are filtered at the top, and each sift below that
// has smaller holes, and the smallest pebbles fall out of the bottom.
int index = 0;
if( voltage > 4.2)
{
index = 0;
}
else if( voltage > 3.4)
{
index = 1;
}
else if( voltage > 2.6)
{
index = 2;
}
else if( voltage > 1.8)
{
index = 3;
}
else if( voltage > 1.0)
{
index = 4;
}
else
{
index = 5;
}
// Does an other led has to be turned on ?
// Then also turn off the previous led.
if( oldIndex != index)
{
digitalWrite( ledPins[oldIndex], LOW);
digitalWrite( ledPins[index], HIGH);
}
// Remember the led that is now on
oldIndex = index;
// slow down the sketch
delay( 200);
}