Hi, I'm very new to Arduino (and coding in general, honestly). I'm trying to make a metronome that receives user input, but when I upload my code onto the Arduino board (which is connected to a breadboard with a speaker), a shrill and continuous beep sound is emitted from the speaker. I don't get any error messages, but the program isn't even close to doing what I want it to do. Can someone please tell me what I'm doing wrong? My code is below. Thanks so much.
- I don't have a touch shield or anything, so I use the Serial Monitor to input numbers for the metronome.
int pin = 8;
int bpg; // number of beats on page
int bpm; // tempo of the piece
float answer = bpg * 60000.00 / bpm; // number of milliseconds it takes to play the page of music
float tpbeat = answer / bpg; // number of milliseconds between each beat
float t = 0.00;
void setup() {
Serial.begin(9600);
tone (pin, 30, 50);
}
void loop() {
Serial.println("Enter number of beats on page:");
while (Serial.available() == 0){
};
bpg = Serial.parseInt();
Serial.println("Enter tempo:");
while (Serial.available()==0){
};
bpm = Serial.parseInt();
float answer = Serial.parseFloat();
float tpbeat = Serial.parseFloat();
for (float t = 0.00; t <= answer; t += tpbeat) {
tone (pin, 30, 50);
delay (tpbeat);
} // speaker is supposed to beep every 'tpbeat' milliseconds, until 'tppg' milliseconds pass
}