function "tone"

I'm working in a keyboard project and I wrote this simple code:

void setup(){

}

void loop(){
tone(2, 264);
tone(3, 297);
}

I put a push-button attached to each pin that when pressed make the piezoelectric speaker work.

But the problem is that, only the pin 2 works. Why and how to fix?

I put a push-button attached to each pin that when pressed make the piezoelectric speaker work.

How did you connect the pushbuttons ? Was there any extra code involved ?

From the tone() reference page:

A duration can be specified, otherwise the wave continues until a call to noTone().
...
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect.

Does it make sense now?

Alright, thank you guys.
Now I have a new code:

/*
1dó = 264
2ré = 297
3mi = 330
4fa = 352
5sol = 396
6la = 440
7si = 495
8dó = 528
*/
int speaker = 12;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(speaker, OUTPUT);
}

void loop() {

int i;
int button;
for (i = 2; i < 8; i++) {

if (digitalRead(i) == HIGH) {
button = i;

switch (button) {
case 2:
tone(speaker, 264);
break;
case 3:
tone(speaker, 297);
break;
case 4:
tone(speaker, 352);
break;
case 5:
tone(speaker, 396);
break;
case 6:
tone(speaker, 440);
break;
case 7:
tone(speaker, 495);
break;
default:
noTone(speaker);
break;
}
}
}
}

But now when i press any button, the speaker don't stop. Even with the noTone.
What's wrong??

Code logic, that what's wrong.

When no button is pressed, the digitalRead() will see no HIGH and the noTone() is not called.

THANKS A LOT, GUYS! Now the code works! If you have any tips for me, I will appreciate :slight_smile:

int speaker = 12;

void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(speaker, OUTPUT);
}

void loop() {

int i;

for (i = 2; i < 8; i++) {

if (digitalRead(i) == HIGH) {

switch (i) {
case 2: do {
tone(speaker, 264);
} while (digitalRead(2) == HIGH);
noTone(speaker);
break;
case 3: do {
tone(speaker, 297);
} while (digitalRead(3) == HIGH);
noTone(speaker);
break;
case 4:
do {
tone(speaker, 352);
} while (digitalRead(4) == HIGH);
noTone(speaker);
break;
case 5: do {
tone(speaker, 396);
} while (digitalRead(5) == HIGH);
noTone(speaker);
break;
case 6: do {
tone(speaker, 440);
} while (digitalRead(6) == HIGH);
noTone(speaker);
break;
case 7: do {
tone(speaker, 495);
} while (digitalRead(7) == HIGH);
noTone(speaker);
break;
}
}
}
}

A software engineer looks from a larger distance at the problem. You have buttons and frequencies with code that glues it together.

Let's put the data in arrays:

const int buttonPins[] = { 2, 3, 4, 5, 6, 7 };
const int freqList[] = {264, 297, 352, 396, 440, 495 };   // 330 and 528 are missing ?

Then you can use the array. The array starts at index 0.

for (i = 0; i < 6; i++) {
    if (digitalRead( buttonPins[i] ) == HIGH) {
      switch (i) {
        case 0: do {
            tone(speaker, freqList[i] );
        ...

Then you can get rid of the switch-case statement.

The moment that a button is pressed, a tone is started.
The moment that a button is released, the tone is stopped.
That is the State Change Detection: https://www.arduino.cc/en/Tutorial/StateChangeDetection

To add the State Change Detection to your sketch, you have to reorganize your sketch.

For some fun: the toneAC library.
It uses fixed pins 9 and 10 for the speaker.
It is louder and it has a basic volume control. That makes it possible to create others sounds, even a fading out sound. A piano sound instead of a boring beep !

If you think that is fun, there is also a Mozzi library. That is for advanced Arduino users, and you need an amplifier. The sounds on that website are the sounds that a Arduino Uno can make.

Do you have a resistor with the speaker ?

You have now a working sketch. I suggest to keep that sketch. When you want to change things, you can create a new sketch, for example with version2 in its name.

I didn't know all those things. Certainly I will take a look, thanks!

No, I haven't a resistor with the speaker. Isn't a problem, right?

A normal speaker and a piezo speaker require a series resistor of about 150 Ω. That makes the sound less loud, but that's the way it is.