Buzzer sounding wayyyyy off

When I upload the code, it makes a horrible sound; it's correct, but sounds extremely off.
Heres the code:

#define	C0  16.35
#define	Db0	17.32
#define	D0	18.35
#define	Eb0	19.45
#define	E0	20.60
#define	F0	21.83
#define	Gb0	23.12
#define	G0	24.50
#define	Ab0	25.96
#define	LA0	27.50
#define	Bb0	29.14
#define	B0	30.8752
#define	C1	32.70
#define	Db1	34.65
#define	D1	36.71
#define	Eb1	38.89
#define	E1	41.20
#define	F1	43.65
#define	Gb1	46.25
#define	G1	49.00
#define	Ab1	51.91
#define	LA1	55.00
#define	Bb1	58.27
#define	B1	61.74
#define	C2	65.41
#define	Db2	69.30
#define	D2	73.42
#define	Eb2	77.78
#define	E2	82.41
#define	F2	87.31
#define	Gb2	92.50
#define	G2	98.00
#define	Ab2	103.83
#define	LA2	110.00
#define	Bb2	116.54
#define	B2	123.47
#define	C3	130.81
#define	Db3	138.59
#define	D3	146.83
#define	Eb3	155.56
#define	E3	164.81
#define	F3	174.61
#define	Gb3	185.00
#define	G3	196.00
#define	Ab3	207.65
#define	LA3	220.00
#define	Bb3	233.08
#define	B3	246.94
#define	C4	261.63
#define	Db4	277.18
#define	D4	293.66
#define	Eb4	311.13
#define	E4	329.63
#define	F4	349.23
#define	Gb4	369.99
#define	G4	392.00
#define	Ab4	415.30
#define	LA4	440.00
#define	Bb4	466.16
#define	B4	493.88
#define	C5	523.25
#define	Db5	554.37
#define	D5	587.33
#define	Eb5	622.25
#define	E5	659.26
#define	F5	698.46
#define	Gb5	739.99
#define	G5	783.99
#define	Ab5	830.61
#define	LA5	880.00
#define	Bb5	932.33
#define	B5	987.77
#define	C6	1046.50
#define	Db6	1108.73
#define	D6	1174.66
#define	Eb6	1244.51
#define	E6	1318.51
#define	F6	1396.91
#define	Gb6	1479.98
#define	G6	1567.98
#define	Ab6	1661.22
#define	LA6	1760.00
#define	Bb6	1864.66
#define	B6	1975.53
#define	C7	2093.00
#define	Db7	2217.46
#define	D7	2349.32
#define	Eb7	2489.02
#define	E7	2637.02
#define	F7	2793.83
#define	Gb7	2959.96
#define	G7	3135.96
#define	Ab7	3322.44
#define	LA7	3520.01
#define	Bb7	3729.31
#define	B7	3951.07
#define	C8	4186.01
#define	Db8	4434.92
#define	D8	4698.64
#define	Eb8	4978.03
 

const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int BUZZER_PIN = 3; // Arduino pin connected to Buzzer's pin

// notes in the melody:
int melody[] = {
C3, G3, C4, D4, E4, G3, C3, G3, C4, D4, E4, G3,
LA2, G3, C4, D4, E4, G3, LA2, G3, C4, D4, E4, G3, 
F2, G3, C4, D4, E4, G3, F2, G3, C4, D4, E4, G3, 
G2, G3, C4, D4, E4, G3, G2, G3, C4, D4, E4, G3, 
C3, G3, C4, D4, E4, G3, C3, G3, C4, D4, E4, G3, 
LA2, G3, C4, D4, E4, G3, LA2, G3, C4, D4, E4, G3, 
F2, G3, C4, D4, E4, G3, F2, G3, C4, D4, E4, G3, 
G2, G3, C4, D4, E4, G3, G2, G3, C4, D4
};

// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
  4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8,
 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8,
 4, 16, 16, 16, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8,
 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8, 4, 16, 16, 16, 8, 8,
 4, 16
};

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read new state

  if (buttonState == LOW) { // button is pressed
    Serial.println("The button is being pressed");
    buzzer();
  }
}

void buzzer() {
  // iterate over the notes of the melody:
  int size = sizeof(noteDurations) / sizeof(int);

  for (int thisNote = 0; thisNote < size; thisNote++) {
    // to calculate the note duration, take one second divided by the notetype.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(BUZZER_PIN, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(BUZZER_PIN);
  }
}

Did you perhaps purchase the wrong type of buzzer?

how do use that frequencies defines?
did you try the tone() example from Arduino IDE basic examples?

I am not sure, but I thought tone() takes an int for frequency...
And as @Coding_Badly suggested...
...do you have an active buzzer? (You should gave a passive buzzer in this case).

Tone takes an unsigned int for a frequency, you have floats in your defines but are loading those values into an signed int array. All of that seems wrong.

Do you have the datasheet?

Some people (and some suppliers) get the terminology wrong but a "buzzer" is normally active. You supply DC power and the buzzer has an internal circuit to generate a sound/tone. You can't control the tone except it gets louder with higher voltage (depending on the specs, etc.). If you feed-in a signal instead of DC power you're going to get messed-up sound.

A piezo "transducer" or "speaker" reproduces the signal you feed it. This is what you want. With DC you might hear a "click" when you connect or disconnect it, but DC is zero-Hz so it doesn't make a continuous sound.

Also, piezo transducers are basically tweeters with maximum efficiency around 2kHz (or higher). Your ears are also most-sensitive at around 2kHz. You aren't going to hear much around 30Hz, but since it is a square wave you may hear harmonics. (30Hz takes a BIG woofer and a BIG amplifier.)

Assuming you have some kind of musical keyboard or other instrument, pick a note around 2kHz and check it against a known-good instrument. It shouldn't be THAT bad because the Arduino clock (oscillator) has accuracy around 1/2 percent. You can make a correction factor (which will be the same up or down percentage for each frequency) but there are resolution limitations.

tone() might not have 1Hz resolution. In any case you will loose the decimal places. It comes from a frequency divider and there may be some rounding. If there are rounding errors, some notes may have errors that can't be corrected.

The minimum tone() is 31Hz but like I said the piezo doesn't go that low anyway.

Sorry I haven't responded to y'all. Coding_Badly: Don't think so, I got the buzzer out of one of the boxes in my robotics class. Juraj: No, I use the web editor since I'm on a chromebook; I don't have examples. Build_1971: Honestly... I might. I'm suck at wiring so I don't really know the difference, and the box just said "buzzers". Er_name_not_found: I don't use tone? At all? In the entire coding scheme? DVDdoug: uhh nice essay. I don't have a datasheet, whatever that is. I copied the circuit off the internet as well as the code, but I'mma do a shit ton of modifications to the code. I'll send a picture of the wires.

Wires:

Heres the link for the one it says to use. https://www.amazon.com/gp/product/B01JU3SK1I/ref=as_li_tl?ie=UTF8&tag=zlufy-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01JU3SK1I&linkId=49610f0dee5990dcf75da0c4bb74bc06
It says this is an active, but you're saying to use a passive. I'm using a tutorial on arduinogetstarted

Connect red wire of buzzer to plus (5V), black wire to minus(GND).
If it says 'tick...... ' you ar fine.
If it says 'beeeeeeeeep' you have the wrong buzzer.
Your connections are a bit unconventional, but should work.
Convention is to use the blue marked row for GND and the red marked row for 5V.

Would you like to take another guess? :wink:

Five volts may not be enough to fire it (9 V to 12 V according to the very poorly written advertisement).

I don't actually have my arduino stuff with me, it's at school. Is there any simulator I could use to replicate this process? Wokwi only has "buzzer" not passive or active. Otherwise, I'll get back to you tomorrow.

Looks like you do

Are you a musician?
You use float values.
The tone function takes int values.
If you type 100.9, the value will be truncated to 100.
So that is almost 1% off.
That is clearly out of key (6% is a half tone).
The closest you can get with tone is 101 (which is far less off and should sound acceptable).

Defining a float, adding it to a signed int array, and passing that as an unsigned int may have even bigger issues than just truncation.

remove the all decimals and get an InActive Buzzer,(has an pcb under buzzer), tone is only compatible with int

Had a friend recomment a different tutorial, but this one makes no sound. Heres the code

#include <pitches.h>

int melody[]={NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};

int buttonPin= 12;

int noteDurations[]={4, 8, 8, 4, 4, 4, 4, 4};


void setup(){

  pinMode(12, INPUT);
}


void loop(){

  int buttonState = digitalRead(12);

  if (buttonState == 1){

    for (int thisNote=0; thisNote <8; thisNote++){

      int noteDuration = 1000 / noteDurations [thisNote];
      tone(8, melody [thisNote], noteDuration);

    
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

    
      noTone(8);
    }
  }
}

In setup:
pinMode(8, OUTPUT);
Is missing.
Can you also show pitches.h?
Did you test the buzzer with a battery?
How us your button connected?

@build_1971 tone() doesn't require to set pinMode

@markusduck5 try the tone() example from Arduino IDE Examples menu

yeah a small error

pinMode(8, OUTPUT);
pins gonna be automaticlly/without setting input