tone keyboard

Hi there, I’m playing with the tone() function. I have 8 pushbuttons, and I’d like to play a different note for each of them. So far it works although there’s an issue with the noTone. Basically when no button is pressed, then noTone() should be called.

Here’s my code generating noise with the noTone()

byte btns[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
byte btnVals[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
byte speaker = 13;

int mel[] = { 262, 294, 330, 349, 392, 440, 494, 523 };

void setup() {
  Serial.begin(9600);
  
  for(int i = 0; i< sizeof(btns); i++)
    pinMode(btns[i], INPUT_PULLUP);
    
  pinMode(speaker, OUTPUT);

}

void loop() {
  
  for(int i = 0; i<sizeof(btns);i++)
    btnVals[i] = digitalRead(btns[i]);

  for(int i = 0; i < sizeof(btnVals); i++){
    Serial.println(btnVals[0]);

  if(btnVals[i] == 0)
    tone(speaker, mel[i]);
  else
    noTone(speaker);
   
  }

}

Thank you!

  boolean noButtonPressed = true;
 for(int i = 0; i < sizeof(btnVals); i++){
    Serial.println(btnVals[0]);
    if(btnVals[i] == 0) {
      tone(speaker, mel[i]);
      noButtonPressed = false;
    }
  }
  if (noButtonPressed)
    noTone(speaker);