[Fundamentals] Arduino - Making a Digital Piano

주피터 · 2019-10-23 · 4
Creating a Digital Piano Using Micro Switches ⓞ Diagram explaining switch shape, symbols, and internal configuration Micro switches are used as piano keys. A micro switch is a switch that operates sensitively with only light contact. A micro switch has three terminals: COM, NO, and NC. COM is a terminal used in common whether the circuit is open or closed. NC is a terminal that is normally closed. NO is a terminal that closes when the switch is pressed, allowing current to flow. Therefore, in normal conditions, the contact point is connected to the NC terminal, but when the handle is pressed, it connects to the NO terminal. As shown in the diagram, the circuit is connected to the COM and NO terminals. If ground is connected to the NO terminal, the switch returns a value of 0, or FALSE, when pressed. ⓞ Arduino board with digital input terminals marked The Arduino has 14 digital terminals numbered 0 to 13. You can use 8 of these terminals, which corresponds to the number of keys. To read input values from a digital terminal in the sketch, call the digitalRead() function and provide the terminal number as an argument. Since the NO terminal is connected to ground, when the switch is pressed, it outputs 0 (0V). ⓞ Diagram explaining switch shape, symbols, and internal configuration The pitch of sound is expressed in frequency. For example, a high C note has a frequency of 523.25Hz (hertz). This means the sound vibrates 523.25 times per second. Therefore, the time it takes for one vibration is 1/523.5 seconds, or 3.820ms (milliseconds), or 3820μs (microseconds). So within 3820μs, 1 and 0 repeat once. The delay time for 1 and 0 is half the period. By alternating and repeating 1 for 1910μs and 0 for 1910μs, you can produce a sound corresponding to 523.25Hz, which is a high C note. By utilizing the frequency representation of pitch, if you repeat 1 and 0 values for half the period duration in microseconds, you can create sound matching that pitch. ⓞ Sketch for creating sound Looking at the sketch that creates a high C note with a period of 3820 microseconds, we assume a speaker is connected to pin 7 to output sound. Since the period is 3820 microseconds, outputting 5V for 1910 microseconds (half the period) and maintaining 0V for another 1910 microseconds creates a square wave pulse, which produces the high C sound. ⓞ Piezo buzzer For an electronic component to use as a speaker, we will use a piezo buzzer. A piezo buzzer, also called a piezoelectric element, converts pressure into electrical signals or electrical signals into pressure. It has a structure similar to a microphone or speaker. A speaker is essentially a type of electromagnet made up of magnets and coils. Looking at the principle, an electromagnet changes polarity as electricity is applied, becoming positive or negative. When positioned near a magnet, it repels when polarities are the same and attracts when polarities are opposite. What happens when an electrical signal is applied as shown in the diagram? When there is a positive electrical signal, it pushes the coil outward, and when there is a negative electrical signal, it pulls the coil inward, causing the diaphragm to vibrate. When the diaphragm vibrates, the air vibrates, and our ears hear this air vibration. ⓞ Arduino board with digital input terminals marked On the Arduino, the speaker uses 1 of the 0-13 terminals (1 speaker) for output. Connect it to an Arduino output pin and send signals of 0 and 1 (0V and 5V) to the speaker. The speaker then repeatedly pushes the diaphragm outward as far as possible and returns it to its original position. When writing a sketch, use the digitalWrite(terminal number) function. ⓞ Diagram explaining the operation sequence of the digital piano Voltage values are received from digital terminals connected to 8 key switches. When a key is pressed, it outputs 1 (5V), and according to the frequency of the note corresponding to the pressed key, electromagnetic waves that repeat with 1 and 0 are output to the speaker to produce sound. ⓞ List of required materials Let's organize what components are needed. You need 8 micro switches for keys, 1 piezo buzzer for the speaker, and 4 breadboards to plug in components to make the circuit. In cases with small components like this, you can connect 4 together. If the horizontal length is 15cm or longer, it can be configured as one. Additionally, you need 1 set of jumper wires to connect components, 1 Arduino board, 1 USB cable to connect the Arduino board and computer, and 1 personal computer. ⓞ Circuit configuration and circuit diagram Arrange components on the breadboard according to the circuit configuration as shown and connect them using jumper wires. After connecting everything, check the direction of components and terminal numbers once more. ⓞ Sketch flowchart In the initial setup() function, configure 1 output terminal in output mode and 8 input terminals in input mode. Then check the output value for each switch and store it in a state variable for each switch. If switch 1 state variable value is 0, the 'C' key is pressed, so output 'C' to the speaker. If switch 2 state variable value is 0, the 'D' key is pressed, so output 'D' to the speaker. If switch 3 state variable value is 0, the 'E' key is pressed, so output 'E' to the speaker. If switch 4 state variable value is 0, the 'F' key is pressed, so output 'F' to the speaker. If switch 5 state variable value is 0, the 'G' key is pressed, so output 'G' to the speaker. If switch 6 state variable value is 0, the 'A' key is pressed, so output 'A' to the speaker. If switch 7 state variable value is 0, the 'B' key is pressed, so output 'B' to the speaker. If switch 8 state variable value is 0, the high 'C' key is pressed, so output high 'C' to the speaker. Wait briefly to remove false signals generated by the switch. This process repeats infinitely. ⓞ Written sketch First, constants are predefined. The terminal connected to the speaker is set to pin 11 with the name SPEAKER, and the terminals connected to switches are set to pin numbers 1 through 8 with names btn1 to btn8. To output C, D, E, F, G, A, B, and C, the delay times for 1 and 0 for each note from 'C' to high 'C' are predefined as constant values. ⓞ Written sketch 2 To store the state of each switch when pressed, variables are declared from switch_button1 to switch_button8. ⓞ Written sketch 3 This is the setup() function. Set the SPEAKER terminal connected to the speaker to output mode. Set each switch terminal from switch1 to switch8 to input mode. ⓞ Written sketch 4 What is shown in the diagram is called pullup configuration. When the switch is not pressed, if the value is 0 (0V) or 1 (5V) is unclear, there is a risk of circuit malfunction. To clearly specify the initial value, it is set to 1 to ensure that the position is not pressed. ⓞ Written sketch 5 Check whether each key switch is pressed and store the value in variables from switch_read1 to switch_read8. When the switch is pressed, 0 is stored. ⓞ Written sketch 6 Read the value of each switch's state storage variable. If the value is 0, the switch is pressed, so play the sound of the corresponding pitch. When the switch is pressed, false signals may appear due to vibrations within the switch itself. To remove these false signals, the current state is delayed slightly. The delay(100) function waits for about 100 microseconds, and the entire process repeats infinitely. ⓞ Written sketch 7 A separate function called playNote() is created. It receives the delay time value of which note to play in a variable called NOTE as an argument and repeatedly outputs 1 and 0 to the speaker for that delay duration. In the for loop, with each vibration, the vibration period (NOTE*2) is added, so one note is sustained for 1 second. When i exceeds 1,000,000, it means 1 second has been exceeded. The variable i is specified as a long type. Integer can only hold numbers from -32,768 to 32,767, which is too small to accommodate 1,000,000, so the long type is used. Therefore, output 1 (5V) to the SPEAKER terminal, maintain the delay time for NOTE, output 0 (0V) to the SPEAKER terminal, maintain the delay time for NOTE, and repeat this for 1 second. ⓞ Completed photo < Error Cases > There are cases where the circuit is fully connected but still doesn't work properly. If there is no sound, check whether the piezo buzzer is properly connected. Connect the longer leg to the positive terminal. If sound comes out randomly without pressing any keys, it is because the pullup is not properly configured. When the switch is not pressed, the input value is unclear whether it is 0 or 1, which can cause malfunction. After setting the input mode, verify that the initial output value is set to 1 (switch not pressed).
4
댓글 0
등록된 댓글이 없습니다.