HackSpace – September 2019

(Jacob Rumans) #1

Robotic Music Glockenspiel


FEATURE


the board, and you should see one of the servos move
back and forth. Take a look at the code, and try editing
it. See if you can change the speed of the sweep, or
move both servos at once – the code that we will use
to play the glockenspiel is similar to this example.
Although it seems a little counter-intuitive at this
early stage of the build, now is a good time to
upload the main sketch, since we can see if the
circuit is working properly before we start attaching

long, ungainly parts. The sketch plays Beethoven’s
Ode to Joy when the button is pushed once, and
stops playing when the button is pushed again. The
LED is lit while the melody is playing.

UNDER THE HOOD
Go to hsmag.cc/issue22 and open the glock_main
sketch. The code starts by including two libraries: one
called ‘Servo’ to control the servos (obviously), and
one called ‘Bounce2’, which makes it easier to handle
button presses. Bounce2 is not included by default in
the Arduino software, so go to Tools > Manage
Libraries, search for ‘Bounce2’ and install it – if you
don’t do this, the compiler will complain and won’t let
you upload the sketch.
We then declare objects to represent the two
servos and the button, before defining various
variables which will be used in the code, such as the
possible angles of the beater, the amount of time to
wait for the arm to reach its position, and the
sequence of notes that you want to be played. In the
setup function, we tell the Arduino which pins
correspond to the servos, button, and LED, and we
set the delay between notes based on the tempo.
In the main loop, we continuously check whether
the button has been pushed. The updateButton
function listens for a button press and sets
sequencePlaying to either true or false. When
sequencePlaying is active, notes are played in order.
The playNote function (see code extract) handles the
process of moving the arm to the correct note,

Right
:hieSds staJR on top
oM the (rduino to
add MunJtionaSit`


Below
reatinN a IasiJ
TodeS oM the ser]o
itseSM in -ree
(+
was heSpMuS when
desiNninN a
ser]o Tount


The arm takes a finite time to reach a note before the beater can hit it, which needs
to be taken into consideration in the code. After some trial and error, we found that a
preHitDelay of 150 ms was enough time for the arm to reliably reach its destination
before triggering the beater, but feel free to adjust this value up (for increased reliability)
or down (for a faster maximum speed). There is also a postHitDelay value of 50 ms, which
can be tweaked to give a nice clean hit of the beater – if this value is too short, the beater
won’t reach the keys; too long and it will dampen the note, rather than letting it ring.


Timing troubles


Quick Tip


Some useful pedantic
knowledge: many people
will mistakenly call this
instrument a xylophone,
but a xylophone’s bars
are made of wood,
not metal.

Free download pdf