Official website for Linux User & Developer
FOLLOW US ON:
Feb
11

Arduino programming guide – part 1

by Gareth Halfacree

Gareth Halfacree shows us how to get started with programming for Arduino and open hardware by create an electronic music box. The first of our three part guide assumes no prior experience in either programming or developing with Arduino…

9. The setup
Ordinarily, the setup section of a sketch contains the information needed to configure the Arduino for our commands in loop. However, because anything in setup is only executed once, it’s where the meat of our single-play music box can be found.

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

	// to calculate the note duration, take one second
	// divided by the note type.
	//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
	int noteDuration = 1000/noteDurations[thisNote];
	tone(8, melody[thisNote],noteDuration);

	// to distinguish the notes, set a minimum time between them.
	// the note’s duration + 30% seems to work well:
	int pauseBetweenNotes = noteDuration * 1.30;
	delay(pauseBetweenNotes);
  }
}

10. Programming the Arduino
Before we start playing with the sample code, let’s check that it runs by hitting the Verify button in the top-left of the IDE – the one that looks like a play symbol.  All being well, you should receive a message telling you the compiled size of the Sketch at the bottom of the IDE.  If not, you’ll see an error message; the most common problem at this point is an inability to find the pitches.h file.
Now we know that the program is valid, let’s put it into our Arduino. Connect your USB cable to the Arduino (Fig 5) and hit the penultimate Upload button. After a brief pause and a flashing light on the Arduino, you should be rewarded with an enchanting ditty from your very own music box.

Arduino programming guide - part 1

Troubleshooting
If you didn’t hear any music when you uploaded the Sketch to the Arduino, something’s gone wrong. Don’t panic, because there’s usually a simple explanation.  Let’s have a look at what you may have missed.

11. Error messages
The most common cause of a failed upload is having the wrong serial port configured in the IDE, which will generate an error in the status area at the bottom of the IDE.  Click the Tools>Serial Port menu and see what’s set.  If it’s currently configured to /dev/usb0, try changing it to /dev/usb1 and clicking Upload again.

12. The circuit
Make sure that the buzzer is connected to both Pin 8 and Ground on the Arduino.  If you’re using a breadboard, it’s easy to miss the right holes due to the size of the buzzer, so lift it and double-check it’s connected to the same rows as your positive and ground wires (Fig 6).

Arduino programming guide - part 1

13. Pitches.h
If you compiled the Arduino package yourself, or used an unofficial package, it’s possible you don’t have the file pitches.h in a location that the compiler can find it.  If so, you’ll see a ‘No such file or directory’ error in the status area.  Try downloading a newer build, or find the missing file:

$ locate pitches.h

14. Customisation
Now that we’ve got our music box playing, it’s time to try changing the Sketch to suit our own needs. Although the example is useful, the layout can be a bit strange and requires some careful handling if we want to play our
own music.

15. The pitch
To change the tune, find the melody section that defines what notes are played. Each note is defined as a tone from pitches.h.  If you want to change the pitch of a note, do so here – but if you want to add more notes, there are a few more steps.  A ‘0’ represents a rest.

int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};

16. The length
Each note in our tune has a length value associated with it, including the rests.  A value of 8 represents an eighth, 4 a quarter, 2 a half and 1 a full-length note.  If you add more notes to the tune, be sure to add their lengths to this list.

int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

17. All together now
The last stage of adding to our tune is to delve into the setup section and change the line that iterates through each note. If we leave it at its default value, we’ll only hear the first eight notes.  Simply change the number in bold to the number of notes in your new tune.

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

Once you’ve changed your tune, hit the Upload button again to replace the old version of the Sketch still stored in the Arduino’s memory with the new version. If you want to hear the tune again without re-uploading it, just hit the reset button on the Arduino’s body.

In part 2 we’ll look at tidying the example program up to make it easier to modify, and interfacing our music box with our Linux system for some interactive entertainment…

Linux User & Developer is the magazine for the GNU Generation
Click here to try 3 issues for £1

Return to the homepage
See what else features in issue 96

twitter follow us

Pages: 1 2
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    Trackbacks

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields