I have started with the buzzer. In this code you use some numbers that I don't understand. I understand that you switch on and off the buffer changing the period to create different tones but I don't understand the way to calculate the frequency and the period. I have attached this piece of code :
int freq = 220 * pow(1.059463, note);
float period = 1000000.0 / freq / 2.0;
for (byte r = 0; r < repeat; r++) {
for (float t = 0; t < duration * 1000; t += period * 2) {
analogWrite(BUZZER, 150); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delayMicroseconds(period); // wait for a delayms ms
analogWrite(BUZZER, 0); // 0 turns it off
delayMicroseconds(period); // wait for a delayms ms
}
delay(pause);
}
As a programming tip you shouldn't use numbers in the code. It makes the code hard to understand. You can use #defines instead. This numbers are named magic numbers.
I have started with the buzzer. In this code you use some numbers that I don't understand. I understand that you switch on and off the buffer changing the period to create different tones but I don't understand the way to calculate the frequency and the period. I have attached this piece of code :
As a programming tip you shouldn't use numbers in the code. It makes the code hard to understand. You can use #defines instead. This numbers are named magic numbers.