Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
10 LED bar graph controlled by potentiometer- arduino uno
/*
Reads input from a 10,000 ohm potentiometer.
Lights up an array of ten LEDs based on how much signal is going through.
*/
int numPins = 10;
int ledPins[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ledStates[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
// Analog pin A0 is where you hook the potentiometer up. Or microphone. Or w/e.
int sensorPin = A0;
int sensorVal = 0;
void setup() {
// set the digital pin as output:
int i = 0;
for(i = 0; i < numPins; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop(){
sensorVal = analogRead(sensorPin);
sensorVal /= 100; // want this to be btn 0-10
// arduino has a 10bit A->D converter. So divide 1024 by 100 gives us ~ 0-10 scale.
int i = 0;
for(i = 0; i < numPins; i++) {
if(sensorVal > i) {
ledStates[i] = HIGH;
}
else {
ledStates[i] = LOW;
}
digitalWrite(ledPins[i], ledStates[i]);
}
} // End main loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.