VU meter.- Led bar display

Hi

I'm working on a project, I've an Arduino Uno with 2x 10 leds connected. These two rows are lighting up on the music, these are responding to a Left and right channel input. The project works, I wrote this code with help from the internet.

My question is, can someone please explain to me how this code works? I really want to understand it!

Regards
Xander Michiels

//L
int musicL = A0;
int outputL,L;
int number_of_led_L[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

//R
int musicR = A1;
int outputR,R;
int number_of_led_R[8] = {10, 11, 12, 13, A2, A3, A4, A5}; 

void setup()
{
for (L = 0; L < 10; L++)  
  pinMode(number_of_led_L[L], OUTPUT);

for (R = 0; R < 8; R++)  
  pinMode(number_of_led_R[R], OUTPUT);
}

void loop() {
  
//L
outputL = analogRead(musicL);

outputL = outputL/8;   

  if (outputL == 0) 
   {
   for(L = 0; L < 10; L++)
     {
     digitalWrite(number_of_led_L[L], LOW);
     }
  }
  
  else
  {
   for (L = 0; L < outputL; L++)
    {
     digitalWrite(number_of_led_L[L], HIGH);
    }
    
    for(L = L; L < 10; L++) 
     {
      digitalWrite(number_of_led_L[L], LOW);
    
     }
  }

//R
outputR = analogRead(musicR);

outputR = outputR/8;   

  if (outputR == 0) 
   {
   for(R = 0; R < 8; R++)
     {
     digitalWrite(number_of_led_R[R], LOW);
     }
  }
  
  else
  {
   for (R = 0; R < outputR; R++)
    {
     digitalWrite(number_of_led_R[R], HIGH);
    }
    
    for(R = R; R < 10; R++) 
     {
      digitalWrite(number_of_led_R[R], LOW);
    
     }
  }
  
}

Vu_meter_-_music_bar_display.ino (1.1 KB)

Does it work?
Or are you missing a couple of map operations?
They're a bit more flexible than simple division

(deleted)

TheMemberFormerlyKnownAsAWOL:
Does it work?
Or are you missing a couple of map operations?
They're a bit more flexible than simple division

The code does work, everything is fine! But I Want to understand how this code works, I'm a newbie... :stuck_out_tongue:

spycatcher2k:
What part do you not understand? I see nothing that is not covered in the Example sketches supplied with the IDE!

Yeah I've looked into every function, but all these together I don't see how it works. I just want to know how it comes that my leds react to the music.

Let's hope it never gets here

for(R = R; R < 10; R++)
     {
      digitalWrite(number_of_led_R[R], LOW);
    
     }

But it's working, right?

TheMemberFormerlyKnownAsAWOL:
Let's hope it never gets here

for(R = R; R < 10; R++)

{
      digitalWrite(number_of_led_R[R], LOW);
   
    }




But it's working, right?

Yes, it's working.

xandro_2000:
Yes, it's working.

You got lucky.

That's nice.

Don't rely on it happening again

You say you wrote the code so you must understand SOME of it. Why not put comments against all the bits you do understand and then we'll help you to fill in the rest.

The basics are it reads values from musicL and musicR and then it switches LEDs on and off depending on those values. Are you really saying you can't see where it's reading the values in and you can't see where it's writing HIGHs and LOWs to the LEDs?

Steve

TheMemberFormerlyKnownAsAWOL:
You got lucky.

That's nice.

Don't rely on it happening again

It is working for over a week? What do you mean?

slipstick:
You say you wrote the code so you must understand SOME of it. Why not put comments against all the bits you do understand and then we'll help you to fill in the rest.

The basics are it reads values from musicL and musicR and then it switches LEDs on and off depending on those values. Are you really saying you can't see where it's reading the values in and you can't see where it's writing HIGHs and LOWs to the LEDs?

Steve

I do see where its getting the inputs from and digital writing them out but it's just the 'for(L = 0; L < 10; L++)' parts I don't get.

Xander

It's a perfectly standard for loop.

And the last one is writing to pins you may not own.