Arduino Music Starter

by Computothought in Circuits > Arduino

2361 Views, 9 Favorites, 0 Comments

Arduino Music Starter

Screenshot from 2015-07-04 23:04:04.png
SUNP0020.JPG

The Arduino is one of the neatest microcontrollers. For something so
simple, it can do an amazing amount of projects. One such project that interested me was the Digital melody project. If you have one the the Arduinos where the Atmel chip can be removed, you can make your own project without the board once the programming is done.

What Is Needed.

speaker.jpg
tut7-connect-resistor.jpg

For this project, all you will need is the Arduino, usb cable (for programming), computer, some wires, optional 100 ohm resistor, and an old eight ohm speaker. Usually the back of speakers on the magnet have what resistance they are. So grab the old dead radio or the like that has a speaker and see what it has. Old personal computers are likely to have one too that you can borrow for a minute.

The Circuit

speaker_schem.png

Now you need to build the circuit. It is minimal and you should be able to set it up quickly. Resistor adds safety for protecting the Arduino. Uses digital pin 8 and ground.

Original Code.

Screenshot from 2015-01-15 03:31:11.png
Screenshot from 2015-01-15 03:38:52.png

Set up up your Arduino for programming and load the Arduino IDE program into your computer. Then load in the Digital melody sample from the menu. The instructions for your Arduino should tell you how to set up your board. Assuming your tools are set up properly, you can now compile and up load the sketch to your Arduino. You should now hear a set of tones that sounds like it came from a cartoon.

Your Own Tune.

Screenshot - 01152015 - 02:41:49 AM.png
Screenshot - 01152015 - 03:57:11 AM.png

But what if you wanted to do your own tune. No problem. All you have to do is translate the notes and timing into your Arduino sketch. If you are unfamiliar with musical notation, you might want to get some help on this. The notation for a note might look something like the picture. Then you need to have the note durations.

silentnight.png

We decided to try doing Silent Night. Of course you need the sheet music to get the notes and the timing. The sketch will provide you with the information of how to translate the notes. So we began doing our own translation using vim to record our results. Since this is 3/4 timing, things will be tricky.

The Translation.

Screenshot from 2015-01-15 04:02:48.png
Screenshot from 2015-01-15 04:07:13.png

So we began doing our own translation using vim to record our results. Since this is 3/4 timing, things will be tricky. Oops every thing is in lower case and we need to add the NOTE_ to the beginning of the notes. No problem. a g~~ will convert the letters to uppercase and a :%s!^!NOTE_! will the preface to the notes. So far so good. Now we need to separate the notes from the timings. So save the file.

$ cut -c-7 silentnight > notes-sn

and that gives us:

$ cat notes-sn

NOTE_G3

NOTE_A3

NOTE_G3

NOTE_E3

NOTE_G3

NOTE_A3

NOTE_G3

NOTE_E3

NOTE_D4

NOTE_D4

...

...

...

$ tr '\n' ', ' < notes-sn > notes-sn.txt

$ sed -i 's/,/, /g; s/,\s\+/, /g' notes-sn.txt

$ cat notes-sn.txt

NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_D4, NOTE_D4, NOTE_B3, NOTE_C4, NOTE_C4, NOTE_G3, NOTE_A3, NOTE_A3, NOTE_C4, ... ... ...

Which can be easily cut and pasted into the sketch.

$ cut -d'-' -f2 silentnight > notes-st

And that gives us

$ cat notes-st

3

8

4

1

3

8

4

1

1

4

...

...

...

Now to add the commas:

$ tr '\n' ',' < notes-st > notes-cs.csv

and that gives us which you can easily cut and paste into the sketch.

$ cat notes-cs.csv

3,8,4,1,3,8,4,1,1,4,1,1,4,1,1,...

To add spaces:

$ sed -i 's/,/, /g; s/,\s\+/, /g' notes-cs.csv

cat notes-cs.csv

3, 8, 4, 1, 3, 8, 4, 1, 1, 4, 1, 1, 4, 1, ... ... ...

You will also need to change this line for the total number of notes.

for (int thisNote = 0; thisNote < 8; thisNote++) {

More music!

Using Basic Instead of Bash.

Screenshot from 2015-05-08 18:55:21.png

Freebasic is free and is available for most platforms.

compile with

fbc -lang qb filerdr.bas

filerdr.bas

[code]

open "file" for input as #1

open "notes" for output as #2

open "notes-cs" for output as #3

do while not eof(1)

input #1, a$

b$ = left$(a$,2)

c$ = mid$(a$,4,2)

print #2,"NOTE_";b$;", ";

print #3, c$;", ";

loop

close #1

close #2

close #3

[/code]

file
G3-3

F4-23

notes
NOTE_G3, NOTE_F4,

notes-cs

3 , 23,

Standalone Version.

Screenshot from 2015-05-22 08:30:24.png

If you have the Arduino with the removable chip, you can build a standalone version so it can be put in a music box or whatever. You may have to adjust your code depending on what digital pin you use. Add a switch to the battery and you have a neat gift.