unable to produce the sounds as desired with my Piezo Buzzers

I am making a piano/synth and for some reason my sharp notes produce the same at the flats essentially. Also a few notes a wrong. I cannot find the fault. Its like the Hz numbers I input are being rounded somewhere... Here is my code:

#define B0 31
#define C1 33
#define Cs1 35
#define D1 37
#define Ds1 39
#define E1 41
#define F1 44
#define Fs1 46
#define G1 49
#define Gs1 52
#define A1 55
#define As1 58
#define B1 62
#define C2 65
#define Cs2 69
#define D2 73
#define Ds2 78
#define E2 82
#define F2 87
#define Fs2 93
#define G2 98
#define Gs2 104
#define A2 110
#define As2 117
#define B2 123
#define C3 131
#define Cs3 139
#define D3 147
#define Ds3 156
#define E3 165
#define F3 175
#define Fs3 185
#define G3 196
#define Gs3 208
#define A3 220
#define As3 233
#define B3 247
#define C4 262
#define Cs4 277
#define D4 294
#define Ds4 311
#define E4 330
#define F4 349
#define Fs4 370
#define G4 392
#define Gs4 415
#define A4 440
#define As4 466
#define B4 494
#define C5 523
#define Cs5 554
#define D5 587
#define Ds5 622
#define E5 659
#define F5 698
#define Fs5 740
#define G5 784
#define Gs5 831
#define A5 880
#define As5 932
#define B5 988
#define C6 1047
#define Cs6 1109
#define D6 1175
#define Ds6 1245
#define E6 1319
#define F6 1397
#define Fs6 1480
#define G6 1568
#define Gs6 1661
#define A6 1760
#define As6 1865
#define B6 1976
#define C7 2093
#define Cs7 2217
#define D7 2349
#define Ds7 2489
#define E7 2637
#define F7 2794
#define Fs7 2960
#define G7 3136
#define Gs7 3322
#define A7 3520
#define As7 3729
#define B7 3951
#define C8 4186
#define Cs8 4435
#define D8 4699
#define Ds8 4978

int keyA4;
int keyAs4;
int keyB4;
int keyC5;
int keyCs5;
int keyD5;
int keyDs5;
int keyE5;
int keyEs5;
int keyF5;
int keyFs5;
int keyG5;
int keyGs5;
int keyA5;
int keyAs5;
int keyB5;
int keyC6;
int tune;

void setup() {
pinMode(22, INPUT);
pinMode(23, INPUT);
pinMode(24, INPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);
pinMode(28, INPUT);
pinMode(29, INPUT);
pinMode(30, INPUT);
pinMode(31, INPUT);
pinMode(32, INPUT);
pinMode(33, INPUT);
pinMode(34, INPUT);
pinMode(35, INPUT);
pinMode(36, INPUT);
pinMode(37, INPUT);
pinMode(13, OUTPUT);
tune = 13;
}

void loop() {
keyC6 = digitalRead(22);
if(keyC6 == HIGH)
{
tone(tune, C6);
}
if(keyC6 == LOW);
{
noTone(tune);
}

keyB5 = digitalRead(23);
if(keyB5 == HIGH)
{
tone(tune, B5);
}
if(keyB5 == LOW);
{
noTone(tune);
}

keyAs5 = digitalRead(24);
if(keyAs5 == HIGH)
{
tone(tune, As5);
}
if(keyAs5 == LOW);
{
noTone(tune);
}

keyA5 = digitalRead(25);
if(keyA5 == HIGH)
{
tone(tune, A5);
}
if(keyA5 == LOW);
{
noTone(tune);
}

keyGs5 = digitalRead(26);
if(keyGs5 == HIGH)
{
tone(tune, Gs5);
}
if(keyGs5 == LOW);
{
noTone(tune);
}

keyG5 = digitalRead(27);
if(keyG5 == HIGH)
{
tone(tune, G5);
}
if(keyG5 == LOW);
{
noTone(tune);
}

keyFs5 = digitalRead(28);
if(keyFs5 == HIGH)
{
tone(tune, Fs5);
}
if(keyFs5 == LOW);
{
noTone(tune);
}

keyF5 = digitalRead(29);
if(keyF5 == HIGH)
{
tone(tune, F5);
}
if(keyF5 == LOW);
{
noTone(tune);
}

keyE5 = digitalRead(30);
if(keyE5 == HIGH)
{
tone(tune, E5);
}
if(keyE5 == LOW);
{
noTone(tune);
}

keyDs5 = digitalRead(31);
if(keyDs5 == HIGH)
{
tone(tune, Ds5);
}
if(keyDs5 == LOW);
{
noTone(tune);
}

keyD5 = digitalRead(32);
if(keyD5 == HIGH)
{
tone(tune, D5);
}
if(keyD5 == LOW);
{
noTone(tune);
}
keyCs5 = digitalRead(33);
if(keyCs5 == HIGH)
{
tone(tune, Cs5);
}
if(keyCs5 == LOW);
{
noTone(tune);
}

keyC5 = digitalRead(34);
if(keyC5 == HIGH)
{
tone(tune, C5);
}
if(keyC5 == LOW);
{
noTone(tune);
}

keyB4 = digitalRead(35);
if(keyB4 == HIGH)
{
tone(tune, B4);
}
if(keyB4 == LOW);
{
noTone(tune);
}

keyAs4 = digitalRead(36);
if(keyAs4 == HIGH)
{
tone(tune, As4);
}
if(keyAs4 == LOW);
{
noTone(tune);
}

keyA4 = digitalRead(37);
if(keyA4 == HIGH)
{
tone(tune, A4);
}
if(keyA4 == LOW);
{
noTone(tune);
}
}

cpennock:
for some reason my sharp notes produce the same at the flats essentially

I don't know what this means. With equal temper sharps and flats are the same notes e.g. Bb and A# are the same note/frequency.

So give us a bit more of a hint...WHICH notes do you think are producing the wrong frequency?

Steve

I cannot find the fault. Its like the Hz numbers I input are being rounded somewhere...

I believe that's true but I don't know what the actual resolution is or exactly how it's limited by the internal registers.

(There will also be an absolute error because the Arduino's clock isn't perfect but I'm not sure if that error can be large enough for it to sound out-of-tune.)

C natural and C sharp make the same sound. Also G Gsharp A and A sharp all make the same sound.... Which doesn't align with my code at all. I wired it properly. each button has a wire going to the indicated digital pin. Should I not use digital pins? Is there a way to float the Hz number better?

Should I not use digital pins?

No digital pins are fine.
The problem might be the piezo buzzer. These make bad speakers as they have naturally occurring resonances which make reproducing tones difficult.

There is something very odd about your code. If one key is held down that will start the tone but then the next key that is not held down will stop the tone. The effect of this is that the tone gets turned on and off very rapidly. This is maybe causing your problem. With only one output the code should look to see if a key is held down and if it is start the tone. Then it should enter a while loop that looks at that key until it is released and then you turn the tone off.

Alternatively add a delay of say 100 ms after the tone is turned on. Not as good but simpler to do.

The code is long and turgid and would be a very lot shorter using a for loop and arrays.

Your code

 void setup() {
  pinMode(22, INPUT);
  pinMode(23, INPUT);
  pinMode(24, INPUT);
  pinMode(25, INPUT);
  pinMode(26, INPUT);
  pinMode(27, INPUT);
  pinMode(28, INPUT);
  pinMode(29, INPUT);
  pinMode(30, INPUT);
  pinMode(31, INPUT);
  pinMode(32, INPUT);
  pinMode(33, INPUT);
  pinMode(34, INPUT);
  pinMode(35, INPUT);
  pinMode(36, INPUT);
  pinMode(37, INPUT);

Can be replaced with

 for(byte i =22 ; i < 38; i ++) {
pinMode (i , INPUT);}

If you used INPUT_PULLUP Then you would not need the pull down resistors your code suggests you have. Your switched should then be wired correctly between input and ground and you would look for a LOW to turn tones on and HIGH too turn them off.

Several points:

Firstly this style of repetitive code is a red flag that somethings not done the right way. Its like
seeing a recipe that says:

1) Peel potato number 1
2) Peel potato number 2
3) Peel potato number 3
4) Peel potato number 4
5) Peel potato number 5
6) Peel potato number 5
7) Peel potato number 6
8) Peel potato number 7
9) Peel potato number 8
6) Peel potato number 9
9) Peel potato number 9

You need to use arrays - one for the pin numbers, one for the frequencies - then the code itself
is a few lines. A few lines means far fewer places for bugs to creep in (look carefully at my example
above), and its twenty times easier to change the code in the future.

Secondly you need to detect key-down and key-up events - checkout the StateChangeDetection
example perhaps. You should only call tone() once when a key is pressed, not continually while it remains
down.

I am looking into the StateChangeDetection and refining that process. However I am trying what y’all said plus this is a newer version in a deferent style using while loops. ITS SOOO GOOOD! HOWEVER it has a constant tone over top everthing I play. It like a constant A note is always being played, besides that is great.

#define B0 31
#define C1 33
#define Cs1 35
#define D1 37
#define Ds1 39
#define E1 41
#define F1 44
#define Fs1 46
#define G1 49
#define Gs1 52
#define A1 55
#define As1 58
#define B1 62
#define C2 65
#define Cs2 69
#define D2 73
#define Ds2 78
#define E2 82
#define F2 87
#define Fs2 93
#define G2 98
#define Gs2 104
#define A2 110
#define As2 117
#define B2 123
#define C3 131
#define Cs3 139
#define D3 147
#define Ds3 156
#define E3 165
#define F3 175
#define Fs3 185
#define G3 196
#define Gs3 208
#define A3 220
#define As3 233
#define B3 247
#define C4 262
#define Cs4 277
#define D4 294
#define Ds4 311
#define E4 330
#define F4 349
#define Fs4 370
#define G4 392
#define Gs4 415
#define A4 440
#define As4 466
#define B4 494
#define C5 523
#define Cs5 554
#define D5 587
#define Ds5 622
#define E5 659
#define F5 698
#define Fs5 740
#define G5 784
#define Gs5 831
#define A5 880
#define As5 932
#define B5 988
#define C6 1047
#define Cs6 1109
#define D6 1175
#define Ds6 1245
#define E6 1319
#define F6 1397
#define Fs6 1480
#define G6 1568
#define Gs6 1661
#define A6 1760
#define As6 1865
#define B6 1976
#define C7 2093
#define Cs7 2217
#define D7 2349
#define Ds7 2489
#define E7 2637
#define F7 2794
#define Fs7 2960
#define G7 3136
#define Gs7 3322
#define A7 3520
#define As7 3729
#define B7 3951
#define C8 4186
#define Cs8 4435
#define D8 4699
#define Ds8 4978

int keyA4;
int keyAs4;
int keyB4;
int keyC5;
int keyCs5;
int keyD5;
int keyDs5;
int keyE5;
int keyEs5;
int keyF5;
int keyFs5;
int keyG5;
int keyGs5;
int keyA5;
int keyAs5;
int keyB5;
int keyC6;
int tune;

void setup() {
for(byte i =22 ; i < 38; i ++) {
pinMode (i , INPUT);}

pinMode(13, OUTPUT);
tune = 13;
}

void loop() {
while(digitalRead(22) == HIGH);
{
tone(tune, C6);
}

while(digitalRead(23) == HIGH);
{
tone(tune, B5);
}
while(digitalRead(24) == HIGH);
{
tone(tune, As5);
}
while(digitalRead(25) == HIGH);
{
tone(tune, A5);
}
while(digitalRead(26) == HIGH);
{
tone(tune, Gs5);
}
while(digitalRead(27) == HIGH);
{
tone(tune, G5);
}

while(digitalRead(28) == HIGH);
{
tone(tune, Fs5);
}
while(digitalRead(29) == HIGH);
{
tone(tune, F5);
}
while(digitalRead(30) == HIGH);
{
tone(tune, E5);
}
while(digitalRead(31) == HIGH);
{
tone(tune, Ds5);
}

while(digitalRead(32) == HIGH);
{
tone(tune, D5);
}

while(digitalRead(33) == HIGH);
{
tone(tune, Cs5);
}
while(digitalRead(34) == HIGH);
{
tone(tune, C5);
}
while(digitalRead(35) == HIGH);
{
tone(tune, B4);
}
while(digitalRead(36) == HIGH);
{
tone(tune, As4);
}

while(digitalRead(37) == HIGH);
{
tone(tune, A4);
}
}

Maybe something to do with you never calling notone() anywhere. So with no keys pressed it will carry on playing the last note you pressed for ever. Pianos and synths don't usually work like that.

Steve

 void loop() {
  while(digitalRead(22) == HIGH);
    {
      tone(tune, C6); 
    }

No, that is as bad as you had before. You are reportedly repeatedly turning the tone on. You want to turn it on and the use the while loop to see when the switch is off then turn the note off. You make the same mistake over and over and over. Please read the answers you have been given again.

Only a few posts away this person had the same problem, received the same advice and got things working.

Please please do not post code like that again. Use the code tags. Read the how to use this forum sticky post, if you don’t know how to do this.

I tried what that other guy did, it made the static and constant note less pronounced but its still there. Like should I do an
If(read == high)
while read == high
tone(tune, c6)

also I did try noTone in alot of different places and it helps but it isnt perfect

 Like should I do an 
If(read == high)
    while read == high
       tone(tune, c6)

No, that is what you are trying already and it is not working.

 if(digitalRead(22) == HIGH){    
       tone(tune, C6);   
       while(digitalRead(22) == HIGH) { } // hold here while it is high
      noTone(tune); // switch off the tone when the switch becomes low
    }