user77691: 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.

user37523: Here is a simple sketch to test the tones used for each color of LED. I have not tested it with a speaker, but it compiles in the Arduino IDE, so perhaps it will work as is.
The call to delayMicroseconds() is "blocking" code, so you may want to change it to use "non-blocking" code, e.g. Use the BlinkWithoutDelay example in the IDE to make a micros() based timer.
/* Arduino Uno. IDE 1.8.9
   Sketch uses 2540 bytes (7%) of program storage space.
   Global variables use 28 bytes (1%) of dynamic memory.

   red    LED: Play 440 Hz for 150ms Wait for 1136us Play 440 Hz for 150ms
   yellow LED: Play 784 Hz for 150ms Wait for 638us Play 784 Hz for 150ms
   blue   LED: Play 587 Hz for 150ms Wait for 851us Play 587 Hz for 150ms
   green  LED: Play 880 Hz for 150ms Wait for 568us Play 880 Hz for 150ms
*/
byte tonePin = 8;

void setup(){
  pinMode(tonePin, OUTPUT);
  RedLEDSound();
  YellowLEDSound();
  BlueLEDSound();
  GreenLEDSound();
}

void loop(){}

void PlayTone(unsigned int m_frequency, unsigned long m_duration, unsigned long m_pause){
  tone(tonePin, m_frequency, m_duration);
  delayMicroseconds(m_pause);
  noTone(tonePin);
}

void RedLEDSound(){
  PlayTone(440, 150, 1136);
  PlayTone(440, 150, 0);
}

void YellowLEDSound(){
  PlayTone(784, 150, 638);
  PlayTone(784, 150, 0);
}

void BlueLEDSound(){
  PlayTone(587, 150, 851);
  PlayTone(587, 150, 0);
}

void GreenLEDSound(){
  PlayTone(880, 150, 568);
  PlayTone(880, 150, 0);
}


77691: <p>I am making &quot;Simon says&quot; game using Arduino on TinkerCad.
I am facing issue in Buzzer sound function.
I want something like this:</p>
<ol>
<li><p>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</p>
</li>
<li><p>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</p>
</li>
<li><p>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</p>
</li>
<li><p>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</p>
</li>
</ol>
<p>I have following functions for the above:</p>
<pre><code> void playTone2(int tone, int duration) {
  Serial.println(tone);
  for (long i = 0; i &lt; 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);
  }

}
</code></pre>
<p>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.</p>
 37523: <p>Here is a simple sketch to test the tones used for each color of LED. I have not tested it with a speaker, but it compiles in the Arduino IDE, so perhaps it will work as is.</p>
<p>The call to <code>delayMicroseconds()</code> is &quot;blocking&quot; code, so you may want to change it to use &quot;non-blocking&quot; code, e.g. Use the <code>BlinkWithoutDelay</code> example in the IDE to make a <code>micros()</code> based timer.</p>
<pre class="lang-c++ prettyprint-override"><code>/* Arduino Uno. IDE 1.8.9
   Sketch uses 2540 bytes (7%) of program storage space.
   Global variables use 28 bytes (1%) of dynamic memory.

   red    LED: Play 440 Hz for 150ms Wait for 1136us Play 440 Hz for 150ms
   yellow LED: Play 784 Hz for 150ms Wait for 638us Play 784 Hz for 150ms
   blue   LED: Play 587 Hz for 150ms Wait for 851us Play 587 Hz for 150ms
   green  LED: Play 880 Hz for 150ms Wait for 568us Play 880 Hz for 150ms
*/
byte tonePin = 8;

void setup(){
  pinMode(tonePin, OUTPUT);
  RedLEDSound();
  YellowLEDSound();
  BlueLEDSound();
  GreenLEDSound();
}

void loop(){}

void PlayTone(unsigned int m_frequency, unsigned long m_duration, unsigned long m_pause){
  tone(tonePin, m_frequency, m_duration);
  delayMicroseconds(m_pause);
  noTone(tonePin);
}

void RedLEDSound(){
  PlayTone(440, 150, 1136);
  PlayTone(440, 150, 0);
}

void YellowLEDSound(){
  PlayTone(784, 150, 638);
  PlayTone(784, 150, 0);
}

void BlueLEDSound(){
  PlayTone(587, 150, 851);
  PlayTone(587, 150, 0);
}

void GreenLEDSound(){
  PlayTone(880, 150, 568);
  PlayTone(880, 150, 0);
}
</code></pre>
