Need some help with combing some audio related code

This is what I'm working with:

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

int pinSpeaker = 11;

 

//

int led[10] = { 2, 3, 4, 5, 6, 7, 8, }; // Assign the pins for the leds

int leftChannel = 0;  // left channel input

int left, i;

 

 

 

void setup () {

    pinMode(pinSpeaker, OUTPUT);

 

    //

    for (i = 0; i < 10; i++)  // Tell the arduino that the leds are digital outputs

  pinMode(led[i], OUTPUT);

//  Serial.begin(9600); // Uncomment to enable troubleshoo

 

//

}

 

void loop () {

 

  //

 

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

// Serial.println(left);  // uncomment to check the raw input.

left = left / 5;    // adjusts the sensitivity  

  Serial.println(left);  // uncomment to check the modified input.

// left = 1500;  // uncomment to test all leds light.

// left = 0;    // uncomment to check the leds are not lit when the input is 0.

 

  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);

     }

  }

 

  //

 

 

 

    playTone(50, 500);

 

 

    playTone(100, 1000);

 

    playTone(50, 500);

 

    playTone(100, 2000);

 

    playTone(750, 50);

 

 

 

    //no

 

 

}

 

// duration in mSecs, frequency in hertz

void playTone(long duration, int freq) {

    duration *= 1000;

    int period = (1.0 / freq) * 1000000;

    long elapsed_time = 0;

    while (elapsed_time < duration) {

        digitalWrite(pinSpeaker,HIGH);

        delayMicroseconds(period / 2);

        digitalWrite(pinSpeaker, LOW);

        delayMicroseconds(period / 2);

        elapsed_time += (period);

    }

}

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I stole some code a while back that makes an LED VU type meter and more recently I figured out how to manipulate some code to generate little tones for my robot. I have a vague notion about how the 2 sets of code work and I've learned a little about manipulating stuff to tailor them but I'm not sure if I can do what I'm setting out to do which is playing some tones with the arduino and having them show up as a simple LED display.

 

Any help would be awesome!  The code compiles OK (Which is a minor miracle) but when testing it out it plays the tones 8 times (Why 8 times?) and goes silent. 

 

I'm trying to learn proper structure and such but for some reason it just eludes me...

Thanks guys. I’m going to

Thanks guys. I’m going to check it out some more later.

And it did compile and loaded onto my UNO. It just didn’t work properly…at all :open_mouth:

I doubt that this code

I doubt that this code will compile…

Start with…

int led[7] = { 2, 3, 4, 5, 6, 7, 8};


for (i = 0; i < 7; i++) {
pinMode(led[i], OUTPUT);

}