I'm trying to rewrite the LED Bar Graph example to use analog fading as it goes up and down the bar graph. There's something wrong with my code, as every time the fading map hits the next LED, the previous LEDs go down to 0 before continuing. Coming doen the bar graph, since there are no previous LEDs to turn off, this effect is not noticed, but I assume it's still there.
/* Fading Bar Graph
Wade Pedersen
Read a potentiometer and fade up the bar graph accordingly.
Created Jan 10, 2012 10:54pm */
// the pin that the potentiometer is attached to
const int analogPin = A0;
// the number of LEDs in the bar graph
const int ledCount = 5;
// an array of pin numbers to which LEDs are attached
int ledPins[] = { 10,9,6,5,3 };
int pot = 0;
// the maximum value to use on the potentiometer before switching to the next LED
int fadeMaxRot = 1023/ledCount;
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
//set the Serial for debugging Serial.begin(9600);
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
pot = analogRead(analogPin);
// read the input pin
//divide potentiometer into as many sections as LEDs
int ledLevel = map(pot, 0 , 1023, 0, ledCount);
//map fade so full bright LED is one segment
//fade - fix map so it doesn't loop
int fade = map(pot, 0, fadeMaxRot, 0, 255);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
//rewrite so fades in each LED, keeps it on, and moves to the next.
for (int x = 0; x < 255; x++) {
if (thisLed < ledLevel) {
analogWrite(ledPins[thisLed], fade);
}
//if this statement isn't in, the LEDs don't turn off properly as the potentiometer winds down to zero.
else
analogWrite(ledPins[thisLed], 0);
}
}
}
Can you give more information about what you are trying to do? I see you are trying to control five LEDs. I know you want the analog input to control the LEDs. My question is how exactly. For example, if the analog input reads as 512, do you want half the LEDs on and half off? You say to want fading. How should the LEDs fade with respect to the analog input?
In respect to how the fading works, for example the 512 value:
I was thinking by the time it got to 512, two LEDs would be at full brightness, and one would be at half brightness (For five segments or LEDs, each LED has a range of 204 on the potentiometer).
/* Fading Bar Graph
Wade Pedersen
Read a potentiometer and fade up the bar graph accordingly.
Created Jan 10, 2012 10:54pm */
// the pin that the potentiometer is attached to
const int analogPin = A0;
// the number of LEDs in the bar graph
const int ledCount = 5;
// an array of pin numbers to which LEDs are attached
int ledPins[] = { 10,9,6,5,3 };
int pot = 0;
// the maximum value to use on the potentiometer before switching to the next LED
int fadeMaxRot = 1023/ledCount;
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() {
pot = analogRead(analogPin);
// read the input pin
// divide potentiometer into as many sections as LEDs
int ledLevel = map(pot, 0 , 1023, 0, ledCount);
int fade = map(pot % fadeMaxRot, 0, fadeMaxRot, 0, 255);
for (int thisLed = 0; thisLed < ledCount; thisLed++)
{
if (thisLed < ledLevel)
analogWrite(ledPins[thisLed], 255);
else if (thisLed > ledLevel)
analogWrite(ledPins[thisLed], 0);
else
analogWrite(ledPins[thisLed], fade);
}
}
if (thisLed < ledLevel)... When the potentiometer is not at zero, the first iteration will be if (0 < 1), correct? Why does the light not turn on to full at this point?
It's sad, I'm a third year programming student, I should understand this...
It's sad, I'm a third year programming student, I should understand this...
The sad thing is that you don't seem able to debug this on your own. I don't see anything obviously wrong, so I would start adding Serial.print() and Serial.println() statements to see, on the Serial Monitor, what is going on.
We are operating on the assumption that all your hardware is wired correctly. We can't see how it is wired to confirm this, so it may not be a valid assumption.
What value are you reading from the potentiometer? What value are you mapping that to?
What value is in fade?
Using analogWrite() to turn a pin on or off seems silly to me. If you want to pin HIGH, use digitalWrite() and make it so. If you want the pin LOW, use digitalWrite() and make it so.