Need help making 3 LEDs blink on and off while a melody plays

Hello everyone. I am new to Arduino and so far I have been doing some simple projects with it. For most of my troubleshooting, I usually find the information I need from a google search, but for this project I cannot find the solution on any google search I do. So I am hoping that someone here can help me out.

I have 3 LEDs, 1 piezo buzzer, and 1 button connected to my Arduino UNO. I want the LEDs to blink on and off (similar to an strobe light), and a melody to play on the piezo buzzer, when the button is pressed.

The way I started this project was to first make the code for the LEDs, and that worked fine. Then I made the code for the piezo melody, and that worked fine as well. Then I tried to combine both codes, and it did not work as I want it to. The melody plays fine, but the LEDs only blink once.

I want the LEDs to keep blinking on and off when the button is pressed, and to be completely off when the button is released. It would be nice to have the melody stop when the button is released as well.

Any help anyone can provide will be highly appreciated. Below is my code.
Thanks in advance.

Code

[code]// LED Strobe Light with Melody
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494

#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625

int tune[] =
{
  NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3, NOTE_A3,
  NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3,
  NOTE_E3, NOTE_G3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3,
  NOTE_C4, NOTE_D4
};

float duration[] = 
{
  EIGHTH, QUARTER+EIGHTH, SIXTEENTH, QUARTER, QUARTER, HALF, HALF, HALF,
  QUARTER, QUARTER, HALF+QUARTER, QUARTER, QUARTER, QUARTER, QUARTER+EIGHTH,
  EIGHTH, QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH, QUARTER, QUARTER, QUARTER,
  QUARTER, HALF+QUARTER
};

int length;

const int BUTTON = 13;          // Button is on pin 13


const int LED1 = 2;             // LED is on pin 2
const int LED2 = 7;              // LED is on pin 7
const int LED3 = 8;            // LED is on pin 10    

int val = 0;                    // val will be used to store state of input pin

void setup()
{
  pinMode(10, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(BUTTON, INPUT);
  length = sizeof(tune) / sizeof(tune[0]);
}

void loop()
{
  val = digitalRead(BUTTON);
  
  if (val == HIGH)
  {

  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  delay(40);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  delay(40);
  
  for (int x=0; x<length; x++)
  {
    tone(10, tune[x]);
    delay(1100 * duration[x]);
    noTone(10);
  }

  }
  else
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone(10);
  }
}

[/code]

You should try to move your button test inside the playing loop, else the song will go on until finished, regardless of the state of the button.

You should also format your code BEFORE you post it. Use Tools + Auto Format.

At first glance, it looks your else block follows the for statement.

Using delays is a good example of something called blocking code. The Cpu will do Noting but wait until he delayed time is passed. Unfortunately the basic examples exhibit this and lure people this way of programming.
Of course you can put the tone() and digitalWrite() commands together and get around the problem this time, but believe me, blocking code is a bad habit.

// LED Strobe Light with Melody
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494

#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625

int tune[] =
{
  NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3, NOTE_A3,
  NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3,
  NOTE_E3, NOTE_G3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3,
  NOTE_C4, NOTE_D4
};

float duration[] = 
{
  EIGHTH, QUARTER+EIGHTH, SIXTEENTH, QUARTER, QUARTER, HALF, HALF, HALF,
  QUARTER, QUARTER, HALF+QUARTER, QUARTER, QUARTER, QUARTER, QUARTER+EIGHTH,
  EIGHTH, QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH, QUARTER, QUARTER, QUARTER,
  QUARTER, HALF+QUARTER
};

int length;

const int BUTTON = 13;          // Button is on pin 13
const int led[] = {2, 7, 8};
int currentNote;
unsigned long nextNote;

void setup(){
  pinMode(10, OUTPUT);
  for(int i = 0; i < 3; i++) pinMode(led[i], OUTPUT);
  length = sizeof(tune) / sizeof(tune[0]);
}//setup()

void loop(){
  if(digitalRead(BUTTON)){
    if(nextNote < millis()){//time is up for next note
      nextNote = millis()+1100*duration[currentNote];//calcultate time for next tone
      digitalWrite(led[currentNote%3], 0);//previous led off
      noTone(10);//this tone off
      tone(10, tune[currentNote++]);//next tone
      digitalWrite(led[currentNote%3], 1);//turn next led on
      currentNote %= length;//wrap around
    }//if(nextNote)
  }else{
    for(int i = 0; i < 3; i++) digitalWrite(led[i], 0);//all leds off
    noTone(10);
    currentNote = 0;
  }
}//loop()