  
Hi I am Massimo Banzi 
and I like to make stuff.
Welcome to another tutorial
taken from our Arduino StarterKit.
 
Today we are going to build
a small musical instrument.
You can see, it’s again 
a simple circuit.
We have four buttons 
connected to the Arduino 
and a small piezo speaker
or “paizo” speaker 
depending on where you come from.
So let’s press the buttons 
and see what happens.
Each button 
is associated with a note.
Every time I press a button,
Arduino produces a sound 
through the piezo speaker.
So, how does this work? 
Let’s start from the circuit.
You can see here there are four buttons.
But the four buttons are connected to the
Arduino board with just one wire.
In the previous examples,
we’ve seen that 
for every button we had a wire 
going to the Arduino board
We were using one digital pin
to read each individual button
and here we have four buttons
connected to just one wire,
because we have built 
what is called “resistor ladder”
That's a combination 
of resistors and buttons.
When I press one of the buttons,
I create a combination
of resistors 
that let current flow through them.
But then the voltage
that the Arduino board measures
out of this resistor ladder
changes depending on which 
button I press.
So, each combination of buttons
produces a different voltage.
You know that 
voltages applied to an analog input
can be read from the Arduino
using analogRead().
Then using a 
series of “if” statements,
we can actually say
ok, if the value is
between this number and this number, 
it's because I pressed the first  button.
If it’s between
another set of numbers,
I pressed the second button.
And I could experimentally figure out
which one of the buttons
I pressed 
by looking at
all the numbers that come out
when I press different buttons
that correspond to the combinations
of resistors.
Now
the output of this circuit,
the actuator, is this piezo speaker. 
The piezo speaker
is a very simple device made of
piezo electric material 
that has this feature
that whenever you power it
with electricity, it will make 
a small click.
If you turn on and off
the power to the piezo speaker 
at a certain speed,
these sequences of clicks 
make a sound.
So, for example, 
if I turn on and off
the power 440 times per second,
I produce a sound, which is a very
well defined note.
In our code,
as we will see in a few minutes,
we have defined all the different
frequencies of each note.
And when you press the button,
the Arduino detects
which button has been pressed, 
and then plays that particular note
on the speaker.
Now let’s have a look at the code
and let’s read it line by line
to understand exactly
how to implement this behavior. 
Let’s start from the beginning.
You can see on this line 
that we are defining
a new type of variable,
called an array. 
This is a variable
called "notes" 
that contains four 
different integer values. 
Each one of these 
integer values represents 
the frequency associated
to a certain note. 
In particular 
these are the middle C, D, E,
and F notes.
This will be useful later
when we detect which 
button has been pressed,
we can then 
choose the right frequency
to play on the speaker. 
Then we have the usual setup()
where we have a Serial.begin()
to begin a communication 
with the computer. 
Let’s look at the loop now.
The first thing we do,
we create a variable called "sounder"
that contains the value read 
from the analogPin(0)
that represents the combination of
keys that have been pressed
on the keyboard. 
Then we print that 
value on the serial monitor, 
so we can 
look at it on the computer
and then we start to understand
which button
 has been pressed.
So we do this by looking at 
the "sounder" variable 
using a set of “if” 
and “else if” statements to
segment values in different bands,
and figure out in which band
 the value falls.
Each one of them corresponds
to a sound. 
So, at the beginning,
when the value is 1023,
then we know 
that we have to play the middle C. 
We are using a new function 
called tone()
Tone() is able to produce a sound 
on a small speaker or a piezo speaker
connected to a certain 
pin on the Arduino. 
The only thing we need to do is,
we need to say
 this is the pin where 
the speaker is connected to, 
in our case, 8.
And then we have to 
specify the note 
and that note will be played 
on that pin.
Then
if we continue down
the source code, you can see
there’s a number of “else if”
statements that divide 
the value of "sounder" in 
different bands. 
So
we have a band that goes 
between 920 and 1010
that corresponds to the middle D,
a band that goes between 505 and 515 
that corresponds to the middle E,
and finally 
a band that goes between 
5 and 10 that represents  
the middle F.
If none of these combinations 
is actually detected, 
then there’s a final “else”
statement that you can see here.
This “else” statement calls this
function called nonetone() that
stops any sound being 
produced on the specific pin. 
So we say notone(8)
and this stops
the sound.
Let’s try 
again the instrument and see 
how our software is actually
working here. 
At the same time, I will open
my serial monitor so that
we can see the numbers
while I press the button.
At the moment we see a value,
which is very
close to 0,
which represents the fact
that no button has been pressed.
So, we press the button
and we got 1023.
The second button is 1002.
The third button is 
about 512 
or something.
And the last one is
sort of between 15 and 20.
This
is in a very simple
way, a small 
music instrument that you can build 
very quickly with your Arduino,
a piezo speaker, 
a few resistors and a few buttons.
This is all for now
but remember: Build it, 
hack it, share it,
because Arduino is you.
