1

I am making "Simon says" game using Arduino on TinkerCad. I am facing issue in Buzzer sound function. I want something like this:

  1. 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

  2. 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

  3. 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

  4. 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.

4
  • what's the specific problem? ... all you said is facing issue and not accurate, neither of which describe the actual problem
    – jsotola
    Jun 26 at 3:37
  • The problem is that the Buzzer sound with these functions is not accurate the delay is longer than expected.
    – Snoke
    Jun 26 at 3:45
  • you already said not accurate
    – jsotola
    Jun 26 at 3:47
  • please run a simple sketch that plays the scale
    – jsotola
    Jun 26 at 3:48
1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.