Help with LED VU Meter

I have made an LED VU Meter that lights up LED’s to a corresponding volume level and I am having some trouble. When I turn the volume up too loud, all the LED’s shut off and the LED soldered to digital pin 13 starts blinking really fast and everything stops working. I have to disconnect power from my Arduino to get it to work again. I would like to figure this out.

Here is the code I need help with. It does work, I also will put a link under the code to the instructions and such.

// Led VU Meter Example
// Version 1.0
// Written by James Newbould


int led[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // Assign the pins for the leds
int leftChannel = 0;  // left channel input
int left, i;

void setup()
{
for (i = 0; i < 10; i++)  // Tell the arduino that the leds are digital outputs
  pinMode(led[i], OUTPUT);
}

void loop()
{
left = analogRead(leftChannel);  // read the left channel
left = left / 2;    // adjusts the sensitivity [b](I changed this to 2 from 50 so it lights up better)[/b]

  if (left == 0)  // if the volume is 0 then turn off all leds
   {
   for(i = 0; i < 10; i++)
     {
     digitalWrite(led[i], LOW);
     }
  }
  
  else
  {
   for (i = 0; i < left; i++) // turn on the leds up to the volume level
    {
     digitalWrite(led[i], HIGH);
    }
    
    for(i = i; i < 10; i++)  // turn off the leds above the voltage level
     {
      digitalWrite(led[i], LOW);
     }
  }
}

I tried deleting the last part (turn off LED’s above voltage level), it works without that chunk. What I would like to know is what is “i” in this code and why the LED’s all light up at once, then turn off with the pin 13 LED blinking really fast until you dis- and re-connect power to the Arduino. Is there like an overload error or something here?

Link: http://tinyurl.com/vumeter

Unrelated: If I have 8 volts going into power to my Arduino, will the digital pins output 8 volts?
Is it possible to have USB and power hooked up to my Arduino at the same time? USB = 5v, power input 6v. 5v+6v=11v?

If I have 8 volts going into power to my Arduino, will the digital pins output 8 volts?

No, you have regulator in the circuit, which supplies the AVR processor with 5V.

You are obviously not building this sketch in stages, testing each stage.

left = analogRead(leftChannel);  // read the left channel

The variable left will contain a value between 0 and 1023. Your particular input may result in a lower upper bound, but do you know what that upper bound is?

left = left / 2;    // adjusts the sensitivity [b](I changed this to 2 from 50 so it lights up better)[/b]

So, now left is between 0 and 512.

   for (i = 0; i < left; i++) // turn on the leds up to the volume level
    {
     digitalWrite(led[i], HIGH);
    }

Now, I ranges from 0 to an upper limit as high as 512. The led array does not contain anywhere near that many elements.

Well, I didn’t write it so I don’t entirely understand it. And I am very new to programming and C and all this, so please cope with me.

The variable left will contain a value between 0 and 1023. Your particular input may result in a lower upper bound, but do you know what that upper bound is?

No, I don’t. Could it be 1023? The part where you put this (under the analogread) doesn’t make much sense to me.

left = left / 2;    // adjusts the sensitivity (I changed this to 2 from 50 so it lights up better)

So, now left is between 0 and 512.

Where do you get between 0 and 512? Is this like 1024KB in a MB kinda thing? Is that how you got the 1023 and 512 being ~half of 1023? Well the sensitivity number has to be low so the lights light up better.

   for (i = 0; i < left; i++) // turn on the leds up to the volume level
    {
     digitalWrite(led[i], HIGH);
    }

Now, I ranges from 0 to an upper limit as high as 512. The led array does not contain anywhere near that many elements.

What? There are 10 elements (LED’s) I don’t quite understand.

What? There are 10 elements (LED's) I don't quite understand

Ten elements 0 to 9 correct, so what happens when you try to address element, say,400?

Also, is there some sort of overload system set in place for the Arduino? I have a Duemillanove, and I guess when the Arduino can’t output enough power, it shuts it all down so it doesn’t overheat and makes the LED on pin 13 blink very fast. Please correct me if I’m wrong.

AWOL:

What? There are 10 elements (LED’s) I don’t quite understand

Ten elements 0 to 9 correct, so what happens when you try to address element, say,400?

There aren’t any numbers in this code over 10 so I don’t see any kind of problem there. Also, when 10 is mentioned it is like this “… < 10” which means anything LESS than 10, so nothing can be above?

and I guess when the Arduino can't output enough power, it shuts it all down so it doesn't overheat

Sadly, nothing that complex or helpful. If you're lucky, the fuse will pop, if you're unlucky, bye-bye Arduino.

There aren’t any numbers in this code over 10 so I don’t see any kind of problem there. Also, when 10 is mentioned it is like this “… < 10” which means anything LESS than 10, so nothing can be above?

What about the value of “left”, mentioned above?

So you connected audio jack directly to analog input? That really bad idea, you easily can fry your arduino with such hardware design. As audio signal has both positive and negative semi-wave, you need at least a diode to protect input against negative voltage. More advanced circuit should have a resistor and zener.

You plug some voltage source into the analog pin, where the voltage can vary between 0 and 5V (or 3.3V on some Arduinos). The analogRead function returns the ratio of the measure voltage to the reference voltage (typically the nominal board voltage - 5V or 3.3V) expressed as a value between 0 and 1023.

If you KNOW that the voltage to be measure will range from 0 to 1.25V, on a 5V board, then you can determine that the upper value returned by analogRead will be limited to 1.25/5.0 * 1023, or 255.

If you don’t KNOW that the voltage will be limited to some upper bound less than 5.0V, you have two choices. Plug it in to the Arduino, and call analogRead() and Serial.print() as you vary the voltage, to determine the upper limit with your setup OR assume that the upper limit returned by analogRead() will be 1023.

Since you did not come back and say “but I KNOW that the highest value returned by analogRead() will be X”, I have to assume that you don’t KNOW what the highest value will be, so we have to assume that it will be 1023.

You divide the value by 2, reducing the upper limit to 511. The comment says that the value used to be divided by 50, which would have produced an upper limit of 20.

Well the sensitivity number has to be low so the lights light up better.

OK. But the code needs to be written in a way that doesn’t try to turn on all 512 of the 10 LEDs at once.

You can fix your problem easily:
if(left > 10)
left = 10;
for (i = 0; i < left; i++) // turn on the leds up to the volume level
{
digitalWrite(led*, HIGH);*

  • }*

Magician: So you connected audio jack directly to analog input? That really bad idea, you easily can fry your arduino with such hardware design. As audio signal has both positive and negative semi-wave, you need at least a diode to protect input against negative voltage. More advanced circuit should have a resistor and zener.

I've never heard of this, how would I do this?

What does it mean when the pin 13 LED blinks really fast and nothing is working but the LED? Pressing Reset doesn't fix it.

Everything you people are saying is so confusing. Getting 1023, 255, 512 from 10? What? I understand what those values are in terms of data but I am completely lost here with the whole 10=512 and all that.

Everything you people are saying is so confusing. Getting 1023, 255, 512 from 10? What?

A ten bit binary number can represent all the numbers from zero to 1023 inclusive. The analog to digital converter produces a ten bit result, so an analogRead returns values in the range 0 to 1023. If you use that value to index an array with only ten elements, the results can be (will be) unpredictable.

AWOL:

Everything you people are saying is so confusing. Getting 1023, 255, 512 from 10? What?

A ten bit binary number can represent all the numbers from zero to 1023 inclusive.
The analog to digital converter produces a ten bit result, so an analogRead returns values in the range 0 to 1023.
If you use that value to index an array with only ten elements, the results can be (will be) unpredictable.

I still don’t get it. Thanks for trying to explain it, but I’m still confused.

OK. But the code needs to be written in a way that doesn’t try to turn on all 512 of the 10 LEDs at once.

You can fix your problem easily:

if(left > 10)

left = 10;
  for (i = 0; i < left; i++) // turn on the leds up to the volume level
    {
    digitalWrite(led, HIGH);
    }

My initial problem of having the LED’s not light up is already fixed, I just didn’t connect ground to a few LED’s so it works now. I am just curious as to why the system stops working if there is too much input. Is there an error message or anything as to why this happens? I still don’t get the numbers and bits and the like. I know how many bits are in a byte and so on but this to me makes no sense. If I look into it a little more I’m sure Ill start understanding a little better.

Thank you to everyone helping.

I still don't get it.

Simple binary arithmetic. A one bit number can represent two states zero or one. A two bit number can represent four states: 0, 1, 2, 3 (in binary 00, 01, 10, 11) A three bit number can represent eight states 0, 1, 2, 3, 4, 5, 6, 7 (000, 001, 010, 011, 100, 101, 110, 111)

For every extra bit, double the number of states.

This is basic stuff, and pretty much essential that you understand

Simple binary arithmetic. A one bit number can represent two states zero or one. A two bit number can represent four states: 0, 1, 2, 3 (in binary 00, 01, 10, 11) A three bit number can represent eight states 0, 1, 2, 3, 4, 5, 6, 7 (000, 001, 010, 011, 100, 101, 110, 111)

For every extra bit, double the number of states.

This is basic stuff, and pretty much essential that you understand

Now that you put it that way, it all makes perfect sense. Its so simple, and yet I couldn't see it. Thank you for explaining this, for it will help me out greatly in my future endeavors. So when there is the "10" in the code that could mean a 10 bit number such as 0001110001?

No, if you see decimal 10 in your code, it takes just four bits (1010) to represent in binary.

AWOL: No, if you see decimal 10 in your code, it takes just four bits (1010) to represent in binary.

Now it's getting confusing. So 1 in binary is 10? How does 2 numbers come from one number? I get how it's on and off but not really how 2 numbers come from 1 or 0. Could you maybe link me to a chart or something about this?

So 1 in binary is 10?

No, the great thing about 1 is that is only ever 1.
Think about the number 15 in decimal
What does the 1 represent?
It actually represents 10 decimal, and the 5 represents 5 times 1.

In binary (not decimal), each digit’s position represents an increase not of ten decimal, but two.

AWOL:

So 1 in binary is 10?

No, the great thing about 1 is that is only ever 1. Think about the number 15 in decimal What does the 1 represent? It actually represents 10 decimal, and the 5 represents 5 times 1.

In binary (not decimal), each digit's position represents an increase not of ten decimal, but two.

Okay, 1 is always 1. Now for 15 the one is 1, and the 5 is 5 ones. So thats 111111?