simple keyboard using the tone() function

I'm following one of the tutorials on the Arduino website (https://www.arduino.cc/en/Tutorial/toneKeyboard)
So, I'm trying to make a program where when I press one of the 3 buttons, it outputs a different sound through the tone function out to the speaker.
and because I don't have any force sensing resistors I used 3 push buttons instead. I changed the code up a little but it won't seem to work.
And i connected a 220 resistor to the speaker.
Please help me! I can't find the problem!

//i included a library for this
#include "pitches.h"
const int threshold = 10;

int notes[] = {
  NOTE_A4, NOTE_B4, NOTE_C3
};

void setup() {
  // put your setup code here, to run once:
 // digital pins 2,3,4 are connected to a push button each
 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);
 //pin 8 is connected to the speaker
 pinMode(8,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int thisButton = 0; thisButton < 3; thisButton++) {
  //i did thisButton+2 because the pins are 2,3,4 not 0,1,2  
  int buttonReading = digitalRead(thisButton+2);

    if (buttonReading < threshold) {
      tone(8, notes[thisButton], 20);
    }
  }

}

With a pushbutton there's no point having a threshold. It's a digital pin which will read HIGH or LOW. Try:

if (buttonReading == LOW)

Do you have pullup resistors on the pins? If not use INPUT_PULLUP, instead of INPUT, on each pin to turn on the internal pullup.

That should get you started.

Pete

You need to make some changes.

You need to remove this line:

const int threshold = 10;

If your push buttons do not have pullup resistors, then you need to change these three lines:

from

 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);

to

 pinMode(2,INPUT_PULLUP);
 pinMode(3,INPUT_PULLUP);
 pinMode(4,INPUT_PULLUP);

and make sure that one end of each push button is wired to ground, and the other end is wired to one of the pins (2, 3, 4).

You need to change this line:

from

    if (buttonReading < threshold) {

to
from

    if (buttonReading == LOW) {

20 milliseconds of a tone is not very much. You might want to try 200 or 2000 for testing purposes.

this is the circuit i made
please tell me what the problem is

I'm following one of the tutorials on the Arduino website (https://www.arduino.cc/en/Tutorial/toneKeyboard)
So, I'm trying to make a program where when I press one of the 3 buttons, it outputs a different sound through the tone function out to the speaker.
and because I don't have any force sensing resistors I used 3 push buttons instead. I changed the code up a little but it won't seem to work.
And i connected a 220 resistor to the speaker.
Please help me! I can't find the problem!
+this time i added a picture of the circuit!

//i included a library for this
#include "pitches.h"
const int threshold = 10;

int notes[] = {
  NOTE_A4, NOTE_B4, NOTE_C3
};

void setup() {
  // put your setup code here, to run once:
 // digital pins 2,3,4 are connected to a push button each
 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);
 //pin 8 is connected to the speaker
 pinMode(8,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int thisButton = 0; thisButton < 3; thisButton++) {
  //i did thisButton+2 because the pins are 2,3,4 not 0,1,2  
  int buttonReading = digitalRead(thisButton+2);

    if (buttonReading < threshold) {
      tone(8, notes[thisButton], 20);
    }
  }

but it won't seem to work.

That does not mean much; please describe what it does not do and what you think it should do.

Your button reading will either be 0 or 1 (LOW or HIGH); both are lower than the threshold. Change

if (buttonReading < threshold) {

to

if (buttonReading == LOW) {

or

if (buttonReading == HIGH) {

If that does not work, you will need to check the behaviour of the button using a multimeter; you might have wired it wrong.

//Edit
Please do not double post the same question multiple times.

This is how I wire buttons

  GND
   |
   |  +-- pin
   |  |
+--o--o--+
|        |
|        |
+--o--o--+

And I will make use of the internal pull-up.

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

When the button is pressed, it will read low

Duplicate post of simple keyboard using the tone() function - Programming Questions - Arduino Forum
DON"T DO THAT!!! You waste people's time giving you help that was already provided in your other post. If you had bothered to read the answers in your original post you'd already know what the problem is. I've placed you on my personal blacklist.

Duplicates merged.