How To Use: Tone Function - Arduino Playing The James Bond Theme

On this tutorial you will see how to use the tone function to play monophonic music.

Materials List: 

1 x Arduino Uno R3

1 x Breadboard

1 x Speaker 8 Ω / 0,25 W

1 x Rotary Potentiometer 10 KΩ

1 x Capacitor 100μF / 25 V

Jumpers

How it works:

The syntax of tone function is:

tone (pin, frequency, duration);

The parameters are:

 

pin: the pin on which to generate the tone

frequency: the frequency of the tone in hertz - unsigned int

duration: the duration of the tone in milliseconds (optional) - unsigned long

The tone function generates a square wave of the specified frequency on a pin during the time of duration field (optional).

Each musical note is determined by a frequency and you can see on table below the frequencies:

Based on this table and pentagram above the archive pitches.h (of toneMelody example) was created.

The array melody[] is used to store the notes, using names to correspond to the note and frequency.

For example, the frequency of note A4 (A for "La" and 4 for the 4th octave) is 440 Hz in the array it will be called NOTE_A4.

The musical score represents the notes and the duration by symbols on the pentagram.

Below you see clef-G and clef-F.

The duration is determined by the following symbols:

In array noteDurations[], the durations were represented by numbers.See table below.

Symbol 

Value in array

whole note

1

half note

2

quarter note

4

eighth note

8

sixteenth note

16

           thirty-second note

32

            sixty-fourth note

64 

 

The potentiometer was used to control volume and the capacitor as coupling.

Below you see the circuit on Fritzing.

 

The Sketch:

Verify circuit, and upload Sketch to Arduino:


#include "pitches.h"
#define NO_SOUND 0 // make the rests in music

//array of notes
int melody[] = {
  /*NOTE_G4,NOTE_G4,NO_SOUND,NOTE_G4,NOTE_G4,NO_SOUND,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,
   NOTE_B3,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_CS4,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_B3,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_CS4,NOTE_G3,NOTE_C4,NOTE_G3,
   NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
   NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,*/
   //Introduction
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
  NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
  NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
  NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6,NO_SOUND
};

// note duration: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
  /*8,8,2,8,8,2,16,8,16,8,8,
   2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,
   8,16,16,8,4,8,8,8,
   8,16,16,8,4,8,8,8,*/
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,
  8,4,8,4,8,8,
  8,8,4,8,4,8,
  4,8,4,8,3
};

int pace = 1450; // change pace of music("speedy")
void setup() {
  for (int Note = 0; Note <54; Note++) {//counter of Notes (54 limit the array)
    int duration = pace/noteDurations[Note];//Adjust duration with the pace of music
    tone(8, melody[Note],duration); //Play note

// to distinguish the notes, set a minimum time between them.
    delay(duration*1.2);
  }
}

void loop() {
  //to repeat song, push reset button.
}
//End of Sketch

Let's listen James Bond!!!

Reference:

http://arduino.cc/en/Tutorial/tone

http://en.wikipedia.org/wiki/Note_value

http://en.wikipedia.org/wiki/Clef

 

Views: 2265

Tags: Arduino, Bond, James, Library, Music, Tone

Comment by نهاد عبدالله حسين السلمان on January 14, 2013 at 4:31am
Hi - this truly stunning what spatial, which could create such programs audio Effects
Comment by نهاد عبدالله حسين السلمان on January 14, 2013 at 4:43am

You could get this project file

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Jonathan Dean replied to abhishek yadav's discussion how to record time between two events using arduion
"Just remember that millis() will overflow after about 50 days, so there's the potential for…"
2 hours ago
Jonathan Dean replied to Jonathan Dean's discussion Two LCDs in Parallel?
"Well, I finally got my 16x2 LCDs and a fresh new Nano 3.0. Both LCDs are wired up in parallel (same…"
2 hours ago
Christian Rheinnecker replied to abhishek yadav's discussion how to record time between two events using arduion
"float preTime = millis(); float afterTime; .... Do your Stuff afterTime = millis(); float YouTime…"
yesterday
Christian Rheinnecker posted a status
"New Stuff @arduinoprx.de http://www.arduinoprx.de/"
yesterday
Profile IconWill Fleming, Elijah Patric, Paul R Avery and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
GarageLab posted blog posts
yesterday
Christian Rheinnecker posted a video

Arduino PrX : Sequencer AM808 VX3 - Recording a Beat to Atari Mega ST 2 in Cubase 3

In this video you see my AM808 VX3 Midi Sequencer Play a Beat and my Atari Mega ST 2 with Satan Disk NVDI 5 and Cubase 3 is recording a Beat in one Part. It ...
Wednesday
Doug Hyde updated their profile
Wednesday
Profile IconDoug Hyde, Stephen Gray, Mark Evans and 5 more joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
GarageLab posted a blog post

Airburr Flying Robot Attaches to Walls

Airburr was developed at the Laboratory of Intelligent Systems, a branch of the European École…See More
Tuesday
GarageLab posted a blog post

Arduino robotic hand under $200

Robotic hand is one of most important parts in robot equipment. This is main manipulator that…See More
Monday
Profile IconDavid M Patterson, Jordan Hanger , Jason and 3 more joined GarageLab (arduino, electronics, robotics, hacking)
Monday
abhishek yadav posted a discussion

how to record time between two events using arduion

i have a disk rotating at variable speed(max rpm 600) . i want to know how much time it takes to…See More
May 19
Danny Vigo updated their profile
May 19
Danny Vigo commented on Fernando Gil's blog post Tutorial: Servomotor + Arduino
"Why would you want to connect them in parallel? Are you trying to increase the current? Perhaps to…"
May 18
Danny Vigo replied to SATYA NARAYANA REDDY NIMMAKAYALA's discussion 5 stepper motor control code
"Well if you look in the examples that come included with the Arduino IDE you can find some code…"
May 18
Profile IconEvaristo Ramos, Jr. and Dayan Martinez joined GarageLab (arduino, electronics, robotics, hacking)
May 18
Christian Rheinnecker posted a status
"New Video Cypress Hill - Rap Superstar https://www.youtube.com/watch?v=VZSFgdrhzQQ"
May 18
Christian Rheinnecker posted a video

Arduino PrX : Sequencer AM808 VX3 - Demo Cypress Hill - Rap Superstar Beat Play

Demonstration eines Beats auf dem AM808 VX3 Arduino Midi Sequencer Cypress Hill - Rap Superstar Weiter Infos auf http://arduinoprx.de
May 18
Christian Rheinnecker posted a status
May 18

© 2013   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service