digitalRead on tone pin to light up LED

I have a circuit useing the arduino lilypad snap and i want the code to detect when a sound is being played and then light up an LED. I wanted to have it do this so that i do not have to write out each led turning on and off. The code is

 #include "pitches.h"
int sound = 0;
int led = 5;
int melody[] = {
  NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_D4,NOTE_E4,NOTE_G4,NOTE_G4,
    NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_E4,NOTE_D4,NOTE_C4};

int noteDurations[] = {
  4,4,4,4,4,4,2,4,4,2,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,2};

  
void loop() {
  pinMode(led, OUTPUT);
   sound = digitalRead (9);
  if ( sound == HIGH){
    digitalWrite (led, HIGH);
  }
  else if(sound == LOW){
    digitalWrite (led, LOW);
  }
}

void setup() {
  
    for (int thisNote = 0; thisNote < 26; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(9, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    }
    
}

Any help you can provide would be greatly appreciated.

So (despite the order you list them in) first your setup will run, that sends out all the tones, THEN once that is all over with, your loop is going to run and, surprise surprise, it's not going to find any tones playing.

You've got your loop() and setup() functions completely mixed up.

Surely this is a pointless exercise. If your arduino is sending the tone, at the same time it can send the LED pin high.

It really doesn't matter what order you define your setup and loop functions in. They will still be called as setup first then the loop indefinitely.

Sitting behind it all there is a function that does this.

int main(void)
{
setup();
//this for loop will run indefinitely
  for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
  }
  return 0;//this bit never get's reached.
}

Sitting behind it all there is a function that does this.

It does a little more than that. init() is missing from your code, as is the test for the presence of serial data and the call to serialEvent() if the function is defined and there is serial data to be read.

PaulS:

Sitting behind it all there is a function that does this.

It does a little more than that. init() is missing from your code, as is the test for the presence of serial data and the call to serialEvent() if the function is defined and there is serial data to be read.

Yes but irrelevant to this post. (hence I didn't bother showing them)

I agree that in a way it is irrelevant to this post, and init() is mainly hardware setup. The actual main() function can be seen here in the repository and is nearly the same, with small exceptions:

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

DivinityArcane:
I agree that in a way it is irrelevant to this post, and init() is mainly hardware setup. The actual main() function can be seen here in the repository and is nearly the same, with small exceptions:

Yep, the small exceptions are the bits I edited out to save muddying the water.