Vu meter from mono to stereo

HI,
@0212995500229 code, thanks for posting, it helps the forum when an un-terminated thread causes problems

//STEREO LED VU

int led[11] = {4, 5, 6, 7, 8};         //Here you can set led pins for LEFT CHANNEL, add more pins for more LEDs, or remove for less
int rled[11] = {9, 10, 11, 12, 13};    //Here you can set led pins for RIGHT CHANNEL, add more pins for more LEDs, or remove for less
int input, l;
int rinput, r;
void setup()
{
  for (l = 0; l < 11; l++)
    for (r = 0; r < 11; r++)
      pinMode(led[l], OUTPUT);
  pinMode(rled[l], OUTPUT);
}
void loop()
{
  input = analogRead(A0);  //Here you can select analog pin for LEFT CHANNEL
  rinput = analogRead(A1); //Here you can select analog pin for RIGHT CHANNEL

  input = input / 12;      //You can change sensitivity FOR LEFT CHANNEL by changing divider (12)
  rinput = rinput / 12;    //You can change sensitivity FOR RIGHT CHANNEL by changing divider (12)
  if (input < 12)
  {
    if (input == 0)
    {
      for (l = 0; l < 11; l++)
      {
        digitalWrite(led[l], LOW);
      }
    }
    else
    {
      for (l = 0; l < input; l++)
      {
        digitalWrite(led[l], HIGH);
        delay(4);
      }
      for (l = l; l < 11; l++)
      {
        digitalWrite(led[l], LOW);
      }
    }
  }


  if (rinput < 12)
  {
    if (rinput == 0)
    {
      for (r = 0; r < 11; r++)
      {
        digitalWrite(rled[r], LOW);
      }
    }
    else
    {
      for (r = 0; r < rinput; r++)
      {
        digitalWrite(rled[r], HIGH);
        delay(4);
      }
      for (r = r; r < 11; r++)
      {
        digitalWrite(led[r], LOW);
      }
    }
  }
}

Thanks… Tom… :slight_smile:

vu_LED_stereo.ino (1.49 KB)

I have some comments/queries about the code

int led[11] = {4, 5, 6, 7, 8}; 
int rled[11] = {9, 10, 11, 12, 13};

Why are there 11 values allowed for in the LED pin arrays, but only 5 values ?
Why are these arrays of pin numbers declared as int when byte is plenty big enough ?
Why are these arrays not declared as const because their contents do not change in the program ?
Why aren’t the array names consistent and more explicit ? rightLedPins and leftLedPins would be preferable

  for (l = 0; l < 11; l++)
    for (r = 0; r < 11; r++)
      pinMode(led[l], OUTPUT);
  pinMode(rled[l], OUTPUT);

Again, the variable names are are at best confusing, especially using lowercase l
Because the arrays are global the undefined entries between 5 and 10 will be set to zero so

pinMode(led[l], OUTPUT);

will set pin 0 to OUTPUT for entries 5 to 10. As pin 0 is the Serial interface receive pin this is probably not a good idea.
Worse than that the pinMode() command is in a for loop inside a second for loop so will be executed 121 times
How many times, however, will

  pinMode(rled[l], OUTPUT);

be executed ?

I stopped looking at that point because the “sins” of meaningless variable names and the obsession with 11 outputs per channel continues throughout the program