Sound is an important component in electronics. Sound can be used for a number of things, from playing a tune to sounding an alarm. Sounds generated can be as simple as just a beep or as complex as a full medley every time we press a button.
Sound produces simple tones by generating a square wave at specific frequencies. Those square waves become sound using a buzzer element. BrainPad Pulse is equipped with a buzzer and can generate sounds on its own. You can also generate sound using an external buzzer, like the ones found in accessories.
We use the Beep() function to generate sound/tone. This function works with any pin. IT requires three arguments, the pin, frequency, and duration.
This function requires 3 arguments: first the pin number you’ll be using, followed by frequency and duration.
Beep(0, 3000, 100)
Below is an image showing one octave of notes and their frequencies. More information about notes and their frequencies can be found on the Internet.
While any pin can be used with Beep(), there is a special pin that lets you use Beep with the onboard buzzer found on BrainPad Pulse. The pin number is ASCII ‘P’.
Beep('P', 261, 100)
Beep() is a blocking function, which blocks the system until it is done. If Beep() is making a sound for 100ms then the system will block for 100ms until it is done. No need to add delays when generating notes, unless it is desired to have a period of silence.
@Loop Beep('P', 261, 100) Beep('P', 294, 100) Beep('P', 261, 100) Beep('P', 294, 100) Wait(500) Goto Loop
Let’s play around with the frequency to play either a really high pitch note or a really low pitch note. Everyone’s hearing is different, so some people will hear certain frequencies while others cannot. What’s the highest frequency you can still hear?
Now that we understand how electronic devices play sounds, we can create a song. Trying changing the code to create a simple song.