In this lesson, we'll learn below.
* While: Often used in cyclic process
* Array: To make program efficiently
* Install of library that is uncommon
-Table of Contents
0:00 Opening
0:09 What's buzzer
0:51 What we make
1:01 Material preparation
1:11 Circuit diagram
1:23 Assemble circuit
1:49 Make program
3:50 Write to board
4:01 Conclusion
-Related Link
-Introduction
Hardware engineer
9 years experience as a Maker
-SNS
Web : start-electronics.com/
Twitter : / buonoatsushi
#Electronics #Arduino
-Content
In this lesson, we will use buzzers to learn about while statements, which are often used in repetitive processing, and arrays to make programs more efficient.
First, let's understand what a buzzer is. A buzzer has a coil and a metal plate inside. By applying a positive or negative voltage to the coil and shaking the metal plate, the metal plate vibrates back and forth, shaking the space and making a sound. For example, the sound of "la" is 880 Hz in frequency, so by inputting an 880 Hz pulse signal and shaking it, it sounds like the sound of "la" is being played. There are also two types of buzzers: active buzzers and passive buzzers. An active buzzer has an internal control circuit and produces a certain sound simply by applying a DC voltage. On the other hand, a passive buzzer has no internal circuit and produces a sound by inserting a pulse signal from outside. In this case, we will use a passive buzzer because we want to change the sound freely.
This is the circuit we have created this time. Nothing happens while the switch is not pressed, and the buzzer repeats in the order of Do Re Mi only while the switch is pressed. Now let's make this circuit.
The materials we will use this time are an Arduino Uno, a breadboard, a passive buzzer, a push switch, a 10kΩ resistor, and five male and five female jumper wires.
Here is the schematic we will be creating. The switch has a pull-up resistor in series with it so that pin 5 is at a HIGH level when the switch is off and a LOW level when the switch is on. The buzzer is connected directly to the digital pin.
Next, here is a breadboard diagram following the schematic. The passive buzzer is just a coil, so it will sound in either direction, but the manufacturer may not guarantee the operation, so it is safer to put the pulse signal in the direction marked with a plus sign. Now, let's build the circuit according to this diagram. Yes, we have completed the circuit.
Now let's start coding. First, install the necessary libraries as preparation. In this case, we will use a library called pitches, in which the frequencies for each pitch of Do Re Mi are predefined as constants. pitches is not available in the standard library, so download the ZIP file from this page on GitHub, include the sketch, include the library, and click "Install ZIP library" to start coding. Install library in ZIP format" and select the file you just downloaded to import it. Now, create a new file and enter the program on the screen. Here, we have created an array named melody, which contains eight constants that correspond to the do-re-mi-fa-solasid scale defined in the pitches function. An array is a collection of multiple data, and acts like a storage box. When you create an array, you write the data type, array name, and put the values in braces like this. You can write similar code without using arrays, but it is more efficient to use arrays in this way because the code is easier to read and can be written in a smaller amount of time.
In the loop function, we use a while statement to write a process that repeats itself: given a while statement, the computer first checks the condition in the round brackets, and if the condition is satisfied, it performs the process in the braces. If the condition is no longer satisfied, the computer exits the while statement and moves on to the next line. The melody array contains the melody tones, so all you have to do is enter the position number of the melody you wish to retrieve and the melody will be read. The melody is read out simply by entering the number of the position you want to get. Two points to note are that we also need a digitalRead function in the while to check if the switch is turned off. Also, if you want to use the tone function continuously, put a delay in between. This is because the buzzer makes a sound by vibrating, and if the frequency is suddenly changed, the vibration will not be able to keep up, resulting in a strange sound.Finally, save the file and write it by pressing the "write to microcontroller board" button on the toolbar. If the program was created without any problems, you will see that the Do-Re-Mi sound is repeated in sequence only while the switch is pressed.
Негізгі бет Тәжірибелік нұсқаулар және стиль [First Arduino] 8. Buzzer | Starter course with Hands-on
Пікірлер: 12