Switching button read

how would i change this code so that the note plays when the buttons are pressed

#include <Tone.h>

#define SPEAKER 9
#define BUTTON 4
#define BUTTON2 2
#define BUTTON3 7
#define BUTTON4 8


Tone tone1;

int buttonState = 0; //store value of button pin
int button2State = 0;
int button3State = 0;
int button4State = 0;

void setup()
{
  pinMode(BUTTON, INPUT);
  tone1.begin(SPEAKER);
  pinMode(BUTTON2, INPUT);
  tone1.begin(SPEAKER);
}

void loop()
{
  buttonState = digitalRead(BUTTON);
  if (buttonState == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_A4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }
    button2State = digitalRead(BUTTON2);
  if (button2State == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_B4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }
      button3State = digitalRead(BUTTON3);
  if (button3State == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_C4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }    button4State = digitalRead(BUTTON4);
  if (button4State == HIGH)
  {
    // play a tone, then wait for a bit before checking the button again
    tone1.play(NOTE_D4);
    delay(500);  // 500 = 500 ms or 1/2 second
    tone1.stop();
  }
}

You need to read each button, not only BUTTON, but also BUTTON2, BUTTON3 etc. To do that you need to put all those pins in to INPUT mode also. After you can and read the button inputs you can use your current logic I believe.

Second, you don't have to delay after playing a tone (tone.play() is non-blocking). You can simply put tone.play(NOTE_A4, 500); in the code instead.

edit: Missed the INPUT problem in the initial post

I dont know what happened, but now it doesn’t do anything where as the last code would play and stop when the buttons were pressed

#include <Tone.h>

#define SPEAKER 9
#define BUTTON1 4
#define BUTTON2 2
#define BUTTON3 7
#define BUTTON4 8


Tone tone1;

int button1State = 0; //store value of button pin
int button2State = 0;
int button3State = 0;
int button4State = 0;

void setup()
{
  pinMode(BUTTON1, INPUT);
  tone1.begin(SPEAKER);
  pinMode(BUTTON2, INPUT);
  tone1.begin(SPEAKER);
  pinMode(BUTTON3, INPUT);
  tone1.begin(SPEAKER);
  pinMode(BUTTON4, INPUT);
  tone1.begin(SPEAKER);
}

void loop()
{
  button1State = digitalRead(BUTTON1);
  if (button1State == HIGH)
  {
    tone1.play(NOTE_A4);
  }
  button2State = digitalRead(BUTTON2);
  if (button2State == HIGH)
  {
    tone1.play(NOTE_B4);
  }
  button3State = digitalRead(BUTTON3);
  if (button3State == HIGH)
  {
    tone1.play(NOTE_C4);
  }    
  button4State = digitalRead(BUTTON4);
  if (button4State == HIGH)
  {
    tone1.play(NOTE_D4, 500);
  }
}