I am making "Simon says" game using Arduino on TinkerCad. I am facing issue in Buzzer sound function. I want something like this:
Use the following steps to generate the tone for the red LED: Play 440 Hz for 150ms Wait for 1136us Play 440 Hz for 150ms
Use the following steps to generate the tone for the yellow LED: Play 784 Hz for 150ms Wait for 638us Play 784 Hz for 150ms
Use the following steps to generate the tone for the blue LED: Play 587 Hz for 150ms Wait for 851us Play 587 Hz for 150ms
Use the following steps to generate the tone for the green LED: Play 880 Hz for 150ms Wait for 568us Play 880 Hz for 150ms
I have following functions for the above:
void playTone2(int tone, int duration) {
Serial.println(tone);
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWriteGeneric(buzzer, true);
delayMicroseconds(tone);
digitalWriteGeneric(buzzer, false);
delayMicroseconds(tone);
}
}
void playTone(int tone1) {
Serial.println(tone1);
if (tone1 == 440) {
playTone2(440, 150);
delayMicroseconds(1136);
playTone2(440, 150);
}
if (tone1 == 784) {
playTone2(784, 150);
delayMicroseconds(638);
playTone2(784, 150);
}
if (tone1 == 587) {
playTone2(587, 150);
delayMicroseconds(851);
playTone2(587, 150);
}
if (tone1 == 880) {
playTone2(880, 150);
delayMicroseconds(568);
playTone2(880, 150);
}
if (tone1 == 2700) {
playTone2(250, 250);
delay(250);
playTone2(150, 250);
}
}
First there was only playTone code but I needed different values for different LEDs so I included playTone function and called playTone2 to it. But the tones that I am getting are not accurate may be because of two functions. I will be able to resolve it if I could understand the working of playTone2 function. I tried but no use, kindly share how exactly playtone2 functions here so that I can edit my playTone function accordingly.
facing issue
andnot accurate
, neither of which describe the actual problemnot accurate