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.

Snoke
  • 23
  • 5

1 Answers1

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

VE7JRO
  • 2,515
  • 19
  • 27
  • 29