Sound on
Let's learn what to do with the Piezo buzzer that's soldered onto your Chatter.
As the word itself says, the buzzer is used for making buzzing sounds.
We'll make a very similar sketch to the last one, but this time, pressing the buttons will trigger a particular sound to come out of the buzzer.
Let's start!
Go to the display block section and choose the "fill frame with" block.
Now we'd like to write the instructions that are shown as soon as we press the Run button.
We'll need the "write string" block for that. You can find it here:
Amazing! You can test your code by clicking Run, and then proceed with the sketch and change what happens when a particular event is triggered.
When you press Chatter's buttons, we trigger a particular event.
As we learned in the previous chapter, a specific block defined for that can be found under the I/O section.
The first block from the I/O section we'll use is "when left pressed".
This block will be used to specify the frequency and duration of our sound.
Because we'll be drawing something on the screen, we'll need to fill the screen with the particular color, which you can do by locating the "fill frame" block.
Now, just like in the previous sketch, we'll duplicate this for the three more buttons and change them slightly.
To begin, double-click the I/O block that says "when left pressed" to duplicate everything inside it.
Now, we'll play the sound at 2000 Hz for 200 milliseconds.
To see what happens, click the Run button and then the left button on your
Let's do it again with another button.
To see what happens, click the Run button and then enter:
Finally, we'll repeat this process for the back button.
We'll make the background white and write "Press the buttons" in black, as shown in the image above.
We don't want to forget about the "push frame" block.
Amazing! You can test your code by clicking Run, and then proceed with the sketch and change what happens when a particular event is triggered.
When you press Chatter's buttons, we trigger a particular event.
As we learned in the previous chapter, a specific block defined for that can be found under the I/O section.
The first block from the I/O section we'll use is "when left pressed".
Drag and drop it under the instructions part of the code.
This time, we want a buzzer to make a specific sound and draw one of the shapes from the first chapter.
To do so, we'll need to begin with another I/O block, "play tone (0,0)".
This block will be used to specify the frequency and duration of our sound.
Drag it into the I/O block, change the frequency to 1000 Hz, and play it for 200 milliseconds.
Amazing!
Let's try something similar to what we did in the previous example.
Because we'll be drawing something on the screen, we'll need to fill the screen with the particular color, which you can do by locating the "fill frame" block.
We must now decide what we want to draw, whether it will be filled or outlined, and where we want it to be drawn.
We can do that using this block:
We chose to draw a 6x6 filled red rectangle at the coordinates 40x50.
Finally, at the end of this block, insert the "push frame" block.
Before we can test whether pressing this specific button causes the desired event, we must first find the "loop forever" block to ensure that your buttons are being scanned at all times. Please remember to use this every time you code something with the buttons, or it will not work.
The "loop forever" button can be found here:
Drag and drop it on the drawing area and look for the I/O "scan buttons" block:
You can now test everything by pressing the Run button and then clicking on left button.
When you press that button, your screen should look like this, and you should hear a sound from your Chatter:
Now, just like in the previous sketch, we'll duplicate this for the three more buttons and change them slightly.
To begin, double-click the I/O block that says "when left pressed" to duplicate everything inside it.
This new I/O block will alter what happens when we press the right button.
Now, we'll play the sound at 2000 Hz for 200 milliseconds.
At the same time, we'll draw a 6x6 blue-filled rectangle with coordinates 40x50 on a yellow background.
To see what happens, click the Run button and then the left button on your
Chatter:
Let's do it again with another button.
This time, we'll trigger the event pressing the enter button.
The sound will play at 100 Hz for 150 milliseconds, and we will draw a 6x6 yellow-filled rectangle on a purple background with coordinates 40x50.
To see what happens, click the Run button and then enter:
Finally, we'll repeat this process for the back button.
The sound will play for 50 milliseconds at 3000 Hz, and we will draw a white filled rectangle 6x6 in the coordinates 40x50 on the marron background.
Now that you know how to make sounds and draw different things, you can experiment with the buzzer and the display.
BackNext