What is the action that makes Timo go to sleep?
How does a piezo speaker work?
Wiring is Inspiring
Does it matter which one of the two legs of the tilt switch you connect to pin 2?
Give Life to the Creature
Why did you copy all that code to the tab called notes.h?
Select Sounds at Random
Open the serial monitor and see what numbers it prints when you turn the tilt switch.
What values are printed in the serial monitor when the tilt switch is open “(digitalRead(2) == HIGH)”?
Play the Sounds
Why do you need to copy the ready-made code and paste it outside the loop part?
In your code you now have several functions that you call in the loop part. Which of the following statements is true?
Links that you might find interesting:
- Check Kati Hyyppä´s projects and
get inspired! They are pretty awesome!
- Or the Rapid
workshop at the FAB LAB BERLIN!
- Also, give a look at Niklas
Roy's projects! "Household Robot" is
- If you want your creature to be made out of a paper model, check at
Free Paper Toys.
Using Local Libraries
For this project, you created a library file called
notes.h. This library file is a local one,
meaning that is not shared by the Arduino's library manager.
Using the command
#include "nameExample.h" is
a common thing to do in Arduino programming.
#include tells Arduino to use a specific library - which has a name that ends with
If you use
#include "nameExample.h" it
means that you're using a local library.
But, if you use instead
#include <nameExample.h>, this means that
you'll be using an external library instead, like the ones you'll find
the Arduino library
What is “notes.h”?
Arduino doesn't know how to send the names of the notes to the piezo,
it only understands numbers.
On the other hand, writing notes as numbers is hard for a programmer.
When you add the additional file
the Arduino program, Arduino can look for code inside it.
notes.h is just a list of variables that have
been given the names of notes.
The values of the variables are numerical values, which correspond to
certain notes if you send them to Arduino
You can add files like this to Arduino, but remember that you must save
them in the same file as your own program.
As you noticed, you can also add functions to your code. A function is a named block of commands which defines an action.
Like for example the function
setup(). The last two are used in every sketch that you create - they are called reserved functions.
laugh(), for example, is a custom created function, to produce a laughing sound!
Tip: You can write your own functions to organize your code as a way of making it more readable.
You used the function
random() to make your creature babble random sounds out of a list of sounds.
For that, you defined the number of sounds that you’ll use and then you decided which sound should equal each of those numbers. Think about it as the shuffle mode in your MP3 player, deciding randomly from a list of songs that you have!