hi I'm Massimo Banzi and I like to make
stuff welcome to another tutorial taken
from our Arduino starter kit so today
we're going to be building a small
musical instrument you can see here 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 so each button is associated
with the node so every time I press the
button Arduino produces a sound through
the piezo speaker so how does this work
well 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 so in
the previous examples we've seen that
for every button we add a wire going to
the Arduino board we were using one
digital pin to read each individual
button in here we have four buttons
connected to just one wire because we
have built what is called a resistor
ladder so there's a combination of
resistors and buttons that when I press
one of the buttons I create a
combination of resistors they like the
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 button
produces a different voltage and you
know that voltage applied to an analog
input can be read from the Arduino using
analog read and then using a series of
if statements we can actually say okay
if the value is between this number and
this number it's I press the first
button if it is between another set of
numbers I've pressed the second button
and I can experimentally figure out
which one are the buttons that I've
pressed by looking at all the numbers
that come out when I press the different
buttons that correspond to the
combination
of resistors now the output of this
circuit the activator is this piezo
speaker the piezo speaker is a very
simple device made of piezo electric
material test 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 you the sequence 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 so 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 so 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 so
this is a variable called notes that
contains four different integer values
each one of these integral value
represents the frequency associated to a
certain node in particular these are the
middle C D E and F nodes 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 beginning to begin a
communication with the computer so 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
analog input zero that represents the
combination of keys that have been
pressed on the keyboard then we print
that value on the serial monitor so that
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 the value in different bands and
which figure out in which band the value
Falls and 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
piezo speaker connected to a certain pin
on the Arduino the only thing we need to
do with this we need to say this is the
pin where the speaker is connected to in
our case number 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 that
there's a number of else if statement
that divided 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 they
represent the middle F if none of this
combination is actually detected then
there's a final else statement and you
can see here this else statement calls
this function called no tone that stops
any sound being produced on a specific
pin so we say no tone 8 and this stops
the sound so let's try again the
instrument and see how our software is
actually working here in the same time I
will open my serial monitor so that we
can see the numbers while I press the
button so at the moment we see a value
which is probably very close to zero
which represents the fact that no button
has been pressed so we press a button
and we get 1023 the second button is
1002
the third button is about 512 or
something and the last one is kind of in
that 1520 sort of value so this is in a
very simple way a small musical
instrument that you can build very
quickly with your Arduino appear the
speaker a few resistor and a few buttons
so this is all for now
but remember build it hack it share it
because Arduino is you