Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« on: August 10, 2011, 02:03:15 pm » |
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/vumeterUnrelated: 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
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: August 10, 2011, 02:08:34 pm » |
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.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #2 on: August 10, 2011, 02:09:00 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #3 on: August 10, 2011, 02:17:46 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: August 10, 2011, 02:23:25 pm » |
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.
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #5 on: August 10, 2011, 02:25:46 pm » |
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
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #6 on: August 10, 2011, 02:28:15 pm » |
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
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: August 10, 2011, 02:29:41 pm » |
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.
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: August 10, 2011, 02:30:48 pm » |
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.
|
|
|
|
Montreal
Offline
Edison Member
Karma: 16
Posts: 2209
Per aspera ad astra.
|
 |
« Reply #9 on: August 10, 2011, 02:31:16 pm » |
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
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #10 on: August 10, 2011, 02:32:26 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #11 on: August 10, 2011, 02:53:05 pm » |
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
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: August 10, 2011, 03:03:50 pm » |
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.
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #13 on: August 10, 2011, 03:19:10 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: August 10, 2011, 03:24:43 pm » |
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.
|
|
|
|
|