Pages: [1] 2 3 4   Go Down
Author Topic: Help with LED VU Meter  (Read 3736 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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?

Code:
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.

Code:
   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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Quote
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.

Quote
Code:
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.

Code:
   for (i = 0; i < left; i++) // turn on the leds up to the volume level
    {
     digitalWrite(led[i], HIGH);
    }
Quote
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Montreal
Offline Offline
Faraday Member
**
Karma: 29
Posts: 2590
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



« Last Edit: August 10, 2011, 02:33:31 pm by Magician » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
-------------------------------------------------------------------------------------------------------------------------------
Quote
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:
Code:
   
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2 3 4   Go Up
Jump to: