Welcome to the Marston Science Library Guide to Arduinos.
Today, we will be working on the piezo tone tutorial.
Our goals: Learn how to translate from notes to frequency in Hertz, Learn to translate
simple sheet music to letters and durations, Learn how to include other files in your sketch,
and learn how to use the tone function to create simple melodies out of piezo.
Let's get started.
In this step, we will learn how to translate from notes to frequency in Hertz.
The basic formula for the frequencies of the notes of the equal tempered scale is given
by f sub n equals f sub zero times a to the nth power where f sub zero equals the frequency
of one fixed note which must be defined.
where n equals the number of half steps away from the fixed note you are, where f sub n
equals the frequency of the note n half steps away, and a equals 2 to the 1/12 power, the
twelfth root of 2 equals the number which when multiplied by itself 12 times equals 2
A common choice when translating notes to frequency is setting the A above middle C
or A4 at f sub zero equals 440 Hertz.
Using a scientific calculator, you have A equals 2 to the 1/12th power, which can be
calculated using the x root y function.
n is the number of notes away from A4, higher note: positive, lower note: negative.
So A to the n power can use the x to the y power function and f sub zero we will keep
constant at 440 Hertz.
For n, you have to include white and black keys.
Knowing that there are 12 steps per octave, and A4 equals 440, what should A3 equal?
Keep in mind that if you are at a higher note, n is positive; if you are on a lower note,
n is negative.
If f sub zero equals 440 times 2 to the 1/12th power, times n is -12, we have 2 to the negative
1st power, times 440, so we have 440 times 1/2 equals 220 Hertz.
So A3 would equal 220 Hertz.
What about for A5?
If we know that A5 is 12 steps away in the octave, we again know that the higher note
n is positive, so 440 times 2 to the 1/12th power to the 12th power equals 2 times 440,
equals 880 Hertz.
So A5 would equal 880 Hertz.
Now that you know how to calculate frequency, you want to make sure you use numbers the
Arduino can use, and round to the nearest whole number.
Luckily Arduino has created a library of all of the frequencies needed.
We will use this as our pitches.h library in our Arduino code.
To include libraries in your sketch, use #include.
Library files use the .h file type for the headers of library folders.
Navigate to the drop down arrow and add a new tab in your sketch in your Arduino integrated design
environment.
Name it pitches.h and hit OK.
User-generated libraries like pitches.h will go in the subdirectory with your sketch.
We're now going to paste the code we found before of all of the note frequencies
I will select all, and copy, and I will paste it here to our code
In this step, we will learn how to translate simple sheet music into notes and durations
We are reading to find which note we need on a scale of C1 to C8, including sharps and the duration of the note
For note type, 1/16th, our duration for the Arduino will be 16
For 1/4th, it will be 4
For 1/2, it will be 2 and for 1 it will be 1
In order to read sheet music, we must be able to read the notes.
On the treble clef, we can see the range goes from C4 to G5
C4 is on the line below the bottom most line of the 5 lines of our treble clef.
We have E4, G4, B4, D5, and F5 as our five lines
Our spaces include F4, A4, C5, and E5
In terms of note duration, the most commonly encountered note durations include
the whole note, the half note, the quarter note, the eighth note, and the sixteenth note.
The whole note is an open dot, the half note is an open dot with a stem
the quarter note is a closed dot with a stem
the eighth note is a closed dot with a stem and a flag
and the sixteenth note is a closed dot with a stem and two flags
In your code, be sure to first include your pitches.h library
#include "pitches.h"
Then declare your variables for your integer sensorReading to equal zero
Next, create and declare an integer array of your notes using the names referenced in pitches.h
Use empty straight brackets after an array name to indicate that data is to be added but no size is given
and curly brackets to fill in your data
Your for loop will access your arrays
Repeat step 3 by creating an array of your tempo or note durations
In your void loop, use a for statement
There are 3 parts to the for loop header
for and then in the parentheses your initialization
your condition, and your increment.
For initialization, declare an integer for the first note in your melody.
For condition, count how many notes are in your melody array
and have integer less than that number
For increment, use the compound operator ++ to increment from zero to your last note
This for loop plays the notes in succession
In order to determine how long each note should be played, declare an integer to store the duration of the note
Use the assignment operator, =, to set this integer equal to the length of a whole note
I used 1200 but you can use 1000 milliseconds
divided by your second array crossed with your first array
So we have note durations multiplied by our array thisNote
This aligns the order of the notes with the order of durations
Next, use the tone() function
There are three parts to the tone() function:
tone, and then in parentheses, your pin number, your frequency, and your duration.
First set your pin to digital 9 where piezo 2 is connected.
Second, set the frequency to your first array melody by the declared incremented thisNote
Third, set your duration to your newly declared integer, noteDuration.
Next, declare an integer for delay between notes and set it equal to the noteDuration multiplied by 1.3
Use the delay() function to set the time of delay between notes in milliseconds
Use the noTone() function to stop the tone from playing at the end
Since it is in the void loop() function
Check all semicolons and brackets
In order to run your code, you must first save the file File>Save
You will then click the checkmark to verify your code
If there are no errors in your code,
the sketch will compile using the loading bar
until it is done compiling
You can then plug in your Arduino into your computer using the USB cable
You will then want to go to Tools>
You will want to make sure that Board is selected at Arduino Uno
and Serial Port> has a connection
Mine is usbmodem411, yours may be something different
You will hit the right arrow > Upload
and watch it upload to your Arduino
In order to make the piezo tone, you will need to plug in the piezo
with the red wire to digital pin 8 and the black wire to ground
You will then need to upload your code to the board
[Gravity Falls Theme Song in digital sound]
Không có nhận xét nào:
Đăng nhận xét