Hi guys, I'm working on a project to make a tank level gauge using the LED bar graph. It's similar to the project here https://docs.arduino.cc/built-in-examples/display/BarGraph, but instead of using a potentiometer, how can I just utilize the 2 switches below.
Hi, @birn
Welcome to the forum.
What switches?
What do you want to use as level sensor in your tank?
Hint, The CAD you are using should be able to EXPORT a jpg image that has better to read.
Thanks.. Tom..
![]()
Hi @TomGeorge
We're making a simulator board of an oil tank. You can see the table below. So for the switches, it's 2 way switch, connected to LED lights which indicates if the valves are open or not. Now what we're thinking is for the lights to go up if you open the makeup valve, go down when you open the drain valve, and when you stop the valves or switch, then the indication light would remain on that level. So it's like replacing the potentiometer with 2 buttons or switches. Also, thanks for the hint, I'm only using this for a while, so I don't know much about it.

Hi,
Oh great, no worries.
Have you got any code written.
It will need to be done in stages.
The LED control as one stage.
The up and down switches which will basically increase or decrease a variable.
The combine then so that the variable value corresponds to the number of LEDs.
If you have 10 LED bar graph, ten count from 1 to 10.
The appropriate switch making the count go up or down.
You need to keep checking the count so it doesn't go over 10 or below 0.
Can you please tell us your electronics, programming, arduino, hardware experience?
Thanks.. Tom...
![]()
PS Its almost midnight here, I'm off to bed, G'night...
![]()
Hi, Good morning ![]()
![]()
For the code I just edited the one from https://docs.arduino.cc/built-in-examples/display/BarGraph which is:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {2, 3, 4, 5, 6, 9, 10, 11, 12, 13};
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
For this code, I don't know how to input the two switches to replace the potentiometer since for programming and electronics, I have no experience like I only started this last Wednesday, since I first thought that you need to only line up the potentiometer to the LED with some resistors. The only background I have is electrical and auto.
Hi,
To add code please click this link;
Have you built this project with the potentiometer to see if it works?
Don't go modifying code until you know it works in real life.
Thanks.. Tom....
![]()
Good afternoon Tom, I've tried the code;
arduino_led_bar_graph_and_potentiometer.ino (561 Bytes)
and it worked just fine for the potentiometer. Though for the project we're working on, we're not going to use a potentiometer, instead it would be the switches so how can I write a program wherein if I press the switch like the make-up valve below:
then the LED lights would light up one by one going up in an interval like every 2 seconds and when I switch it back or there's no more current, the LED that's lit would remain lit up. For the second scenario is when I would switch the drain valve then, the LED that's lit would be turned off one at a time going down with the same interval of 2 seconds. So this is like using switches instead of the potentiometer to light up or shut off the LED.
Thanks for the reply and the tips.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.



