LED Bar Graph in Dot Mode

I have successfully got the LED Bar Graph example on the Arduino Tutorial page up and running. However, I would like to know if someone could tell me how to change this to operate in “dot mode” instead (only one segment on at a time).

The only way I can think of doing it is with a group of “if” statements, however, I would like to know if there is a more efficient way to do it. ie - with 10 LEDs, I would have ten “if” statements, however, it may be possible to do it with just one.

I am only a beginner when it comes to C++, however am picking it up fast.

Thanks in advance.

It would be easier to follow what you are doing, and offer suggestions, if you showed YOUR code, and explained how what it IS doing differs from what you WANT it to be doing.

I have completed the tutorial at http://arduino.cc/en/Tutorial/BarGraph

At the moment, say the pot was halfway, segment 1, 2, 3, 4 and 5 would be lit. However, I just want segment 5 to be lit.

There are two modes for operating the bargraph - bar mode, and dot mode.

Examples (these are not my videos):

Bar Mode: http://www.youtube.com/watch?v=OvXtwmLK4RA&feature=related
Dot Mode: http://www.youtube.com/watch?v=Z8tir8E5F94

Here is the code from the tutorial:
/*
LED bar graph

Turns on a series of LEDs based on the value of an analog sensor.
This is a simple way to make a bar graph display. Though this graph
uses 10 LEDs, you can use any number by changing the LED count
and the pins in the array.

This method can be used to control any series of digital outputs that
depends on an analog input.

The circuit:

  • LEDs from pins 2 through 11 to ground

created 26 Jun 2009
by Tom Igoe

This example code is in the public domain.

*/

// these constants won’t change:
const int analogPin = 0; // 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, 7,8,9,10,11 }; // an array of pin numbers to which LEDs are attached

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);
}
}
}

If you just want one LED on, change:

if (thisLed < ledLevel)

to

if (thisLed == ledLevel-1)

It seems to me that the code should have had if (thisLed <= ledLevel), which you would then change to if (thisLed == ledLevel). Try both ways to see which works best for you.

Thanks! It works well. I never knew it was going to be so easy!

I put this in so that a segment would stay on in the higher range - otherwise it would turn off:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount-1);

I want a segment to be on all the time.

Here is my modified void loop code:

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-1);

// 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);
}
}
}

Basically, the ledCount-1 was put in because the array is numbered from 0 to 9, not 0 to 10.

Now I understand how this program works! It uses the array as a reference table, one could say.

The map function has some quirks that are not documented well.

With a range to 0 to 1023 as the from range, the only time the output value will be at the upper end of the to range (whether that is ledCount or ledCount-1) is when the potentiometer is turned all the way to its lowest setting.

If you change the from range from 0 to 1024, you will get the upper end of the to range as output for a much larger range of travel for the potentiometer.

Then, the to range should be 0 to ledCount, because you will never get ledCount as an output (because you will never supply 1024 as input). The [u]achievable[/u] range of outputs will be from 0 to ledCount-1.