Trying to play 2 differents melodies with 2 buttons but only 1 works...

Hi! Sorry if i'm not in the best section or if I do something bad.. not used to forums.

I usually try to find by myself but I can't figure it out this time.

I managed playing a melody using pitches.h with a button and light a led.

I just tried to double the code for a second button and a second led.

The 2 using the same little piezo speaker.

I just can't play the second one. Tried many changes in the code without success.

Here's my code:

#include "pitches.h"

const int sPin = 8; 
const int ledPin1 = 10;
const int ledPin2 = 12;
const int buttonPin1 = 2;
const int buttonPin2 = 4;
int buttonState = 0;


int melody1[] = {
  NOTE_FS4, NOTE_F4, NOTE_FS4, NOTE_F4, NOTE_DS4, NOTE_GS4, NOTE_GS4,
};

int noteDurations1[] = {
  1, 3, 3, 3, 3, 6, 6, 2 
};

int melody2[] = {
  NOTE_DS4, NOTE_DS4, NOTE_DS4, NOTE_DS4, NOTE_FS4, NOTE_F4, NOTE_F4, NOTE_DS4, NOTE_DS4, NOTE_D4, NOTE_DS4,
};

int noteDurations2[] = {
  2, 2, 6, 2, 2, 6, 2, 6, 2, 6, 1
};

void setup() {

  pinMode(sPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {

// --------------------------------- MELODY1 ------------------------------------------
  buttonState = digitalRead(buttonPin1);

  if (buttonState == HIGH) {
    digitalWrite(sPin, HIGH);{
    digitalWrite(ledPin1, HIGH);

  for (int thisNote = 0; thisNote < 7; thisNote++) {

    int noteDuration = 1000 / noteDurations1[thisNote];
    tone(8, melody1[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  } 
// ------------------------------ END OF MELODY1 --------------------------------------

// --------------------------------- MELODY2 ------------------------------------------
  buttonState = digitalRead(buttonPin2);
  
  if (buttonState == HIGH) {
    digitalWrite(sPin, HIGH);{
    digitalWrite(ledPin2, HIGH);
    
   for (int thisNote = 0; thisNote < 11; thisNote++) {
    int noteDuration = 1000 / noteDurations2[thisNote];
    tone(8, melody2[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.20;
    delay(pauseBetweenNotes);
    noTone(8);
  }
// ------------------------------ END OF MELODY2 --------------------------------------
  
  }   
 }  else {
      digitalWrite(sPin, LOW);    
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      }
    }
  }
}

...not sure if the "else" section is blocking something or not but all my tests didn't worked.

I'm not a genius programmer nor an expert coder.
If anyone could just give me a lead, would be appreciated.

Thanks in advance.

check your brackets... {} You have an extra set which does nothing but the real problem is that your test for button2 is inside the if() statement for the test of button1. This means that if button1 is not pressed, you never check button2. Also, the tones are done playing by the time you get to your else() clause so there really isn't a point for that. Just turn off the leds.

#include "pitches.h"

const int sPin = 8;
const int ledPin1 = 10;
const int ledPin2 = 12;
const int buttonPin1 = 2;
const int buttonPin2 = 4;
int buttonState = 0;


int melody1[] = {
  NOTE_FS4, NOTE_F4, NOTE_FS4, NOTE_F4, NOTE_DS4, NOTE_GS4, NOTE_GS4,
};

int noteDurations1[] = {
  1, 3, 3, 3, 3, 6, 6, 2
};

int melody2[] = {
  NOTE_DS4, NOTE_DS4, NOTE_DS4, NOTE_DS4, NOTE_FS4, NOTE_F4, NOTE_F4, NOTE_DS4, NOTE_DS4, NOTE_D4, NOTE_DS4,
};

int noteDurations2[] = {
  2, 2, 6, 2, 2, 6, 2, 6, 2, 6, 1
};

void setup() {

  pinMode(sPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {

  // --------------------------------- MELODY1 ------------------------------------------
  buttonState = digitalRead(buttonPin1);

  if (buttonState == HIGH) {
    digitalWrite(sPin, HIGH);
    digitalWrite(ledPin1, HIGH);

    for (int thisNote = 0; thisNote < 7; thisNote++) {

      int noteDuration = 1000 / noteDurations1[thisNote];
      tone(8, melody1[thisNote], noteDuration);

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(8);
    }
  }
  // ------------------------------ END OF MELODY1 --------------------------------------

  // --------------------------------- MELODY2 ------------------------------------------
  buttonState = digitalRead(buttonPin2);

  if (buttonState == HIGH) {
    digitalWrite(sPin, HIGH);
    digitalWrite(ledPin2, HIGH);

    for (int thisNote = 0; thisNote < 11; thisNote++) {
      int noteDuration = 1000 / noteDurations2[thisNote];
      tone(8, melody2[thisNote], noteDuration);

      int pauseBetweenNotes = noteDuration * 1.20;
      delay(pauseBetweenNotes);
      noTone(8);
    }
    // ------------------------------ END OF MELODY2 --------------------------------------

  }
  digitalWrite(sPin, LOW);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}

wow Thanks! wasn't expecting an answer so fast...

I was close. I see it now...
Experience is all.

I'll go and upgrade this now.

Thanks again. Really helped!