## Tutorial.PlayMelody History

March 10, 2011, at 11:50 PM by David Cuartielles -
Changed line 17 from:
``` [=
```
to:
Changed lines 165-166 from:

=]

to:
Deleted line 168:
``` [=
```
Deleted line 233:

=]

March 10, 2011, at 11:49 PM by David Cuartielles -
Changed line 29 from:
to:
Changed line 82 from:

int tone = 0;

to:

int tone_ = 0;

Changed line 90 from:
```  if (tone > 0) { // if this isn't a Rest beat, while the tone has
```
to:
```  if (tone_ > 0) { // if this isn't a Rest beat, while the tone has
```
Changed lines 95-96 from:
```      delayMicroseconds(tone / 2);
```
to:
```      delayMicroseconds(tone_ / 2);
```
Changed lines 99-100 from:
```      delayMicroseconds(tone / 2);
```
to:
```      delayMicroseconds(tone_ / 2);
```
Changed line 102 from:
```      elapsed_time += (tone);
```
to:
```      elapsed_time += (tone_);
```
Changed line 116 from:
```    tone = melody[i];
```
to:
```    tone_ = melody[i];
```
Changed line 130 from:
```      Serial.print(tone);
```
to:
```      Serial.print(tone_);
```
Changed line 168 from:
to:
Changed lines 235-236 from:

=]

to:
October 18, 2008, at 06:22 PM by David A. Mellis -
Changed lines 3-26 from:

This example uses a piezo speaker to play melodies. It sends a square wave of the appropriate frequency to the piezo, generating the corresponding tone.

The calculation of the tones is made following the mathematical operation:

```      timeHigh = period / 2 = 1 / (2 * toneFrequency)
```

where the different tones are described as in the table:

```note    frequency    period    timeHigh
c       261 Hz       3830       1915
d       294 Hz       3400       1700
e       329 Hz       3038       1519
f       349 Hz       2864       1432
g       392 Hz       2550       1275
a       440 Hz       2272       1136
b       493 Hz       2028       1014
C       523 Hz       1912        956

```

### Circuit

Piezos have polarity. Commercial devices are usually have a red (positive) and a black (negative). Connect the red wire digital pin 9 and the black wire to ground. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc.

to:

This example makes use of a Piezo Speaker in order to play melodies. We are taking advantage of the processors capability to produde PWM signals in order to play music. There is more information about how PWM works written by David Cuartielles here and even at K3's old course guide

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 9, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

The first example of the code will just send a square wave to the piezo, while the second one will make use of the PWM functionality to control the volume through changing the Pulse Width.

The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the output. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc.

Changed lines 13-31 from:

A piezo connected to pin 9.

### Code

[@ int speakerPin = 9;

int length = 15; // the number of notes char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300;

void playTone(int tone, int duration) {

```  for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
```
to:

Example of connection of a Piezo to pin 9

### Example 1: Play Melody

``` [=
```

/* Play Melody

``` * -----------
*
* Program to play a simple melody
*
* Tones are created by quickly pulsing a speaker on and off
*   using PWM, to create signature frequencies.
*
* Each note has a frequency, created by varying the period of
*  vibration, measured in microseconds. We'll use pulse-width
*  modulation (PWM) to create that vibration.
```

``` * We calculate the pulse-width to be half the period; we pulse
*  the speaker HIGH for 'pulse-width' microseconds, then LOW
*  for 'pulse-width' microseconds.
*  This pulsing creates a vibration of the desired frequency.
*
* (cleft) 2005 D. Cuartielles for K3
* Refactoring and comments 2006 clay.shirky@nyu.edu
* See NOTES in comments at end for possible improvements
*/
```

// TONES ========================================== // Start by defining the relationship between // note, period, & frequency.

1. define c 3830 // 261 Hz
2. define d 3400 // 294 Hz
3. define e 3038 // 329 Hz
4. define f 2864 // 349 Hz
5. define g 2550 // 392 Hz
6. define a 2272 // 440 Hz
7. define b 2028 // 493 Hz
8. define C 1912 // 523 Hz

// Define a special note, 'R', to represent a rest

1. define R 0

// SETUP ============================================ // Set up speaker on a PWM pin (digital 9, 10 or 11) int speakerOut = 9; // Do we want debugging on serial out? 1 for yes, 0 for no int DEBUG = 1;

void setup() {

```  pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
```
Changed lines 67-75 from:

void playNote(char note, int duration) {

```  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
```

```  // play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
```
to:

// MELODY and TIMING ======================================= // melody[] is an array of notes, accompanied by beats[], // which sets each note's relative length (higher #, longer note) int melody[] = { C, b, g, C, b, e, R, C, c, g, a, C }; int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 }; int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo long tempo = 10000; // Set length of pause between notes int pause = 1000; // Loop variable to increase Rest length int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables int tone = 0; int beat = 0; long duration = 0;

// PLAY TONE ============================================== // Pulse the speaker to play a tone for a particular duration void playTone() {

```  long elapsed_time = 0;
if (tone > 0) { // if this isn't a Rest beat, while the tone has
//  played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
```

```      digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
```

```      // DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);
```

```      // Keep track of how long we pulsed
elapsed_time += (tone);
}
```
```  else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
```
Changed lines 112-115 from:

void setup() {

```  pinMode(speakerPin, OUTPUT);
```

}

to:

// LET THE WILD RUMPUS BEGIN =============================

Changed lines 114-118 from:
```  for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
```
to:
```  // Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone = melody[i];
beat = beats[i];
```

```    duration = beat * tempo; // Set up timing
```

```    playTone();
// A pause between notes...
delayMicroseconds(pause);
```

```    if (DEBUG) { // If debugging, report loop, tone, beat, and duration
Serial.print(i);
Serial.print(":");
Serial.print(beat);
Serial.print(" ");
Serial.print(tone);
Serial.print(" ");
Serial.println(duration);
```
Deleted lines 133-135:
```    // pause between notes
delay(tempo / 2);
```
Changed lines 136-235 from:

@]

to:

/*

``` * NOTES
* The program purports to hold a tone for 'duration' microseconds.
*  Lies lies lies! It holds for at least 'duration' microseconds, _plus_
*  any overhead created by incremeting elapsed_time (could be in excess of
*  3K microseconds) _plus_ overhead of looping and two digitalWrites()
*
* As a result, a tone of 'duration' plays much more slowly than a rest
*  of 'duration.' rest_count creates a loop variable to bring 'rest' beats
*  in line with 'tone' beats of the same length.
*
* rest_count will be affected by chip architecture and speed, as well as
*  overhead from any program mods. Past behavior is no guarantee of future
*  performance. Your mileage may vary. Light fuse and get away.
*
* This could use a number of enhancements:
* ADD code to let the programmer specify how many times the melody should
*     loop before stopping
* MOVE tempo, pause, and rest_count to #define statements
* RE-WRITE to include volume, using analogWrite, as with the second program at
*          http://www.arduino.cc/en/Tutorial/PlayMelody
* ADD code to make the tempo settable by pot or other input device
* ADD code to take tempo or volume settable by serial communication
*          (Requires 0005 or higher.)
* ADD code to create a tone offset (higer or lower) through pot etc
* REPLACE random melody with opening bars to 'Smoke on the Water'
*/
```

=]

Second version, with volume control set using analogWrite()

```
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

```
October 18, 2008, at 06:16 PM by David A. Mellis -
Changed lines 12-20 from:

note frequency period timeHigh c 261 Hz 3830 1915 d 294 Hz 3400 1700 e 329 Hz 3038 1519 f 349 Hz 2864 1432 g 392 Hz 2550 1275 a 440 Hz 2272 1136 b 493 Hz 2028 1014 C 523 Hz 1912 956

to:

note frequency period timeHigh c 261 Hz 3830 1915 d 294 Hz 3400 1700 e 329 Hz 3038 1519 f 349 Hz 2864 1432 g 392 Hz 2550 1275 a 440 Hz 2272 1136 b 493 Hz 2028 1014 C 523 Hz 1912 956

October 18, 2008, at 06:14 PM by David A. Mellis -
October 18, 2008, at 06:13 PM by David A. Mellis -
Changed lines 3-10 from:

This example makes use of a Piezo Speaker in order to play melodies. We are taking advantage of the processors capability to produde PWM signals in order to play music. There is more information about how PWM works written by David Cuartielles here and even at K3's old course guide

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 9, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

The first example of the code will just send a square wave to the piezo, while the second one will make use of the PWM functionality to control the volume through changing the Pulse Width.

The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the output. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc.

to:

This example uses a piezo speaker to play melodies. It sends a square wave of the appropriate frequency to the piezo, generating the corresponding tone.

The calculation of the tones is made following the mathematical operation:

```      timeHigh = period / 2 = 1 / (2 * toneFrequency)
```

where the different tones are described as in the table:

```note 	frequency 	period 	timeHigh
c 	        261 Hz 	        3830 	1915
d 	        294 Hz 	        3400 	1700
e 	        329 Hz 	        3038 	1519
f 	        349 Hz 	        2864 	1432
g 	        392 Hz 	        2550 	1275
a 	        440 Hz 	        2272 	1136
b 	        493 Hz 	        2028	1014
C	        523 Hz	        1912 	956

```

### Circuit

Piezos have polarity. Commercial devices are usually have a red (positive) and a black (negative). Connect the red wire digital pin 9 and the black wire to ground. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc.

Changed lines 29-80 from:

Example of connection of a Piezo to pin 9

### Example 1: Play Melody

``` [=
```

/* Play Melody

``` * -----------
*
* Program to play a simple melody
*
* Tones are created by quickly pulsing a speaker on and off
*   using PWM, to create signature frequencies.
*
* Each note has a frequency, created by varying the period of
*  vibration, measured in microseconds. We'll use pulse-width
*  modulation (PWM) to create that vibration.
```

``` * We calculate the pulse-width to be half the period; we pulse
*  the speaker HIGH for 'pulse-width' microseconds, then LOW
*  for 'pulse-width' microseconds.
*  This pulsing creates a vibration of the desired frequency.
*
* (cleft) 2005 D. Cuartielles for K3
* Refactoring and comments 2006 clay.shirky@nyu.edu
* See NOTES in comments at end for possible improvements
*/
```

// TONES ========================================== // Start by defining the relationship between // note, period, & frequency.

1. define c 3830 // 261 Hz
2. define d 3400 // 294 Hz
3. define e 3038 // 329 Hz
4. define f 2864 // 349 Hz
5. define g 2550 // 392 Hz
6. define a 2272 // 440 Hz
7. define b 2028 // 493 Hz
8. define C 1912 // 523 Hz

// Define a special note, 'R', to represent a rest

1. define R 0

// SETUP ============================================ // Set up speaker on a PWM pin (digital 9, 10 or 11) int speakerOut = 9; // Do we want debugging on serial out? 1 for yes, 0 for no int DEBUG = 1;

void setup() {

```  pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
```
to:

A piezo connected to pin 9.

### Code

[@ int speakerPin = 9;

int length = 15; // the number of notes char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300;

void playTone(int tone, int duration) {

```  for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
```
Changed lines 50-86 from:

// MELODY and TIMING ======================================= // melody[] is an array of notes, accompanied by beats[], // which sets each note's relative length (higher #, longer note) int melody[] = { C, b, g, C, b, e, R, C, c, g, a, C }; int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 }; int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo long tempo = 10000; // Set length of pause between notes int pause = 1000; // Loop variable to increase Rest length int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables int tone = 0; int beat = 0; long duration = 0;

// PLAY TONE ============================================== // Pulse the speaker to play a tone for a particular duration void playTone() {

```  long elapsed_time = 0;
if (tone > 0) { // if this isn't a Rest beat, while the tone has
//  played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
```

```      digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
```

```      // DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);
```

```      // Keep track of how long we pulsed
elapsed_time += (tone);
}
```
to:

void playNote(char note, int duration) {

```  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
```

```  // play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
```
Deleted lines 59-63:
```  else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
```
Changed lines 62-65 from:

// LET THE WILD RUMPUS BEGIN =============================

to:

void setup() {

```  pinMode(speakerPin, OUTPUT);
```

}

Changed lines 67-85 from:
```  // Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone = melody[i];
beat = beats[i];
```

```    duration = beat * tempo; // Set up timing
```

```    playTone();
// A pause between notes...
delayMicroseconds(pause);
```

```    if (DEBUG) { // If debugging, report loop, tone, beat, and duration
Serial.print(i);
Serial.print(":");
Serial.print(beat);
Serial.print(" ");
Serial.print(tone);
Serial.print(" ");
Serial.println(duration);
```
to:
```  for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
```
```    // pause between notes
delay(tempo / 2);
```
Changed lines 78-177 from:

/*

``` * NOTES
* The program purports to hold a tone for 'duration' microseconds.
*  Lies lies lies! It holds for at least 'duration' microseconds, _plus_
*  any overhead created by incremeting elapsed_time (could be in excess of
*  3K microseconds) _plus_ overhead of looping and two digitalWrites()
*
* As a result, a tone of 'duration' plays much more slowly than a rest
*  of 'duration.' rest_count creates a loop variable to bring 'rest' beats
*  in line with 'tone' beats of the same length.
*
* rest_count will be affected by chip architecture and speed, as well as
*  overhead from any program mods. Past behavior is no guarantee of future
*  performance. Your mileage may vary. Light fuse and get away.
*
* This could use a number of enhancements:
* ADD code to let the programmer specify how many times the melody should
*     loop before stopping
* MOVE tempo, pause, and rest_count to #define statements
* RE-WRITE to include volume, using analogWrite, as with the second program at
*          http://www.arduino.cc/en/Tutorial/PlayMelody
* ADD code to make the tempo settable by pot or other input device
* ADD code to take tempo or volume settable by serial communication
*          (Requires 0005 or higher.)
* ADD code to create a tone offset (higer or lower) through pot etc
* REPLACE random melody with opening bars to 'Smoke on the Water'
*/
```

=]

Second version, with volume control set using analogWrite()

```
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

```
to:

@]

October 02, 2006, at 11:15 PM by Clay Shirky -
Changed line 169 from:

[=

to:
``` [=
```
October 02, 2006, at 11:15 PM by Clay Shirky -

=]

Second version, with volume control set using analogWrite()

[= /* Play Melody

``` * -----------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* (cleft) 2005 D. Cuartielles for K3
*/
```

int ledPin = 13; int speakerOut = 9; byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p"; // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // 10 20 30 int count = 0; int count2 = 0; int count3 = 0; int MAX_COUNT = 24; int statePin = LOW;

void setup() {

``` pinMode(ledPin, OUTPUT);
```

}

void loop() {

```  analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
```

}

October 02, 2006, at 11:10 PM by Clay Shirky - Re-wrote Example #1
Changed lines 21-22 from:
``` * Program to play melodies stored in an array, it requires to know
```
to:
``` * Program to play a simple melody
```
Changed lines 23-24 from:
``` * The calculation of the tones is made following the mathematical
* operation:
```
to:
``` * Tones are created by quickly pulsing a speaker on and off
*   using PWM, to create signature frequencies.
```
Changed lines 26-33 from:
``` *       timeHigh = 1/(2 * toneFrequency) = period / 2
```
to:
``` * Each note has a frequency, created by varying the period of
*  vibration, measured in microseconds. We'll use pulse-width
*  modulation (PWM) to create that vibration.
```

``` * We calculate the pulse-width to be half the period; we pulse
*  the speaker HIGH for 'pulse-width' microseconds, then LOW
*  for 'pulse-width' microseconds.
*  This pulsing creates a vibration of the desired frequency.
```
Deleted lines 34-45:
``` * where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
```
``` * Refactoring and comments 2006 clay.shirky@nyu.edu
* See NOTES in comments at end for possible improvements
```
Changed lines 40-55 from:

int ledPin = 13; int speakerOut = 9; byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p"; // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // 10 20 30 int count = 0; int count2 = 0; int count3 = 0; int MAX_COUNT = 24; int statePin = LOW;

void setup() {

``` pinMode(ledPin, OUTPUT);
pinMode(speakerOut, OUTPUT);
```
to:

// TONES ========================================== // Start by defining the relationship between // note, period, & frequency.

1. define c 3830 // 261 Hz
2. define d 3400 // 294 Hz
3. define e 3038 // 329 Hz
4. define f 2864 // 349 Hz
5. define g 2550 // 392 Hz
6. define a 2272 // 440 Hz
7. define b 2028 // 493 Hz
8. define C 1912 // 523 Hz

// Define a special note, 'R', to represent a rest

1. define R 0

// SETUP ============================================ // Set up speaker on a PWM pin (digital 9, 10 or 11) int speakerOut = 9; // Do we want debugging on serial out? 1 for yes, 0 for no int DEBUG = 1;

void setup() {

```  pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
```

// MELODY and TIMING ======================================= // melody[] is an array of notes, accompanied by beats[], // which sets each note's relative length (higher #, longer note) int melody[] = { C, b, g, C, b, e, R, C, c, g, a, C }; int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 }; int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo long tempo = 10000; // Set length of pause between notes int pause = 1000; // Loop variable to increase Rest length int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables int tone = 0; int beat = 0; long duration = 0;

// PLAY TONE ============================================== // Pulse the speaker to play a tone for a particular duration void playTone() {

```  long elapsed_time = 0;
if (tone > 0) { // if this isn't a Rest beat, while the tone has
//  played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
```

```      digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);
```

```      // DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);
```

```      // Keep track of how long we pulsed
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
```

}

// LET THE WILD RUMPUS BEGIN =============================

Changed lines 114-131 from:
```  digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
```
to:
```  // Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone = melody[i];
beat = beats[i];
```

```    duration = beat * tempo; // Set up timing
```

```    playTone();
// A pause between notes...
delayMicroseconds(pause);
```

```    if (DEBUG) { // If debugging, report loop, tone, beat, and duration
Serial.print(i);
Serial.print(":");
Serial.print(beat);
Serial.print(" ");
Serial.print(tone);
Serial.print(" ");
Serial.println(duration);
```
Changed lines 137-166 from:

=]

### Example 2: Play Melody _ faded volume

``` [=
```

/* Play Melody - FV

``` * ----------------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* We use the Pulse Width feature with analogWrite to change volume
*
* (cleft) 2005 D. Cuartielles for K3
```
to:

/*

``` * NOTES
* The program purports to hold a tone for 'duration' microseconds.
*  Lies lies lies! It holds for at least 'duration' microseconds, _plus_
*  any overhead created by incremeting elapsed_time (could be in excess of
*  3K microseconds) _plus_ overhead of looping and two digitalWrites()
*
* As a result, a tone of 'duration' plays much more slowly than a rest
*  of 'duration.' rest_count creates a loop variable to bring 'rest' beats
*  in line with 'tone' beats of the same length.
*
* rest_count will be affected by chip architecture and speed, as well as
*  overhead from any program mods. Past behavior is no guarantee of future
*  performance. Your mileage may vary. Light fuse and get away.
*
* This could use a number of enhancements:
* ADD code to let the programmer specify how many times the melody should
*     loop before stopping
* MOVE tempo, pause, and rest_count to #define statements
* RE-WRITE to include volume, using analogWrite, as with the second program at
*          http://www.arduino.cc/en/Tutorial/PlayMelody
* ADD code to make the tempo settable by pot or other input device
* ADD code to take tempo or volume settable by serial communication
*          (Requires 0005 or higher.)
* ADD code to create a tone offset (higer or lower) through pot etc
* REPLACE random melody with opening bars to 'Smoke on the Water'
```
Deleted lines 164-205:

int ledPin = 13; int speakerOut = 9; int volume = 300; // maximum volume is 1000 byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p"; // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // 10 20 30 int count = 0; int count2 = 0; int count3 = 0; int MAX_COUNT = 24; int statePin = LOW;

void setup() {

``` pinMode(ledPin, OUTPUT);
```

}

void loop() {

```  analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,volume);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
```

}

October 18, 2005, at 08:50 AM by 195.178.229.25 -
Changed lines 5-6 from:

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 10, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

to:

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 9, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

The first example of the code will just send a square wave to the piezo, while the second one will make use of the PWM functionality to control the volume through changing the Pulse Width.

Changed lines 13-14 from:

Example of connection of a Piezo to pin 10

to:

Example of connection of a Piezo to pin 9

### Example 1: Play Melody

``` pinMode(speakerOut, OUTPUT);
```
Changed line 63 from:
```  analogWrite(speakerOut, 0);
```
to:
```  digitalWrite(speakerOut, LOW);
```
Changed line 70 from:
```          analogWrite(speakerOut,500);
```
to:
```          digitalWrite(speakerOut,HIGH);
```
Changed line 72 from:
```          analogWrite(speakerOut, 0);
```
to:
```          digitalWrite(speakerOut, LOW);
```
Changed line 77 from:
```          analogWrite(speakerOut, 0);
```
to:
```          digitalWrite(speakerOut, 0);
```

=]

### Example 2: Play Melody _ faded volume

``` [=
```

/* Play Melody - FV

``` * ----------------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* We use the Pulse Width feature with analogWrite to change volume
*
* (cleft) 2005 D. Cuartielles for K3
*/
```

int ledPin = 13; int speakerOut = 9; int volume = 300; // maximum volume is 1000 byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956}; byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p"; // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 // 10 20 30 int count = 0; int count2 = 0; int count3 = 0; int MAX_COUNT = 24; int statePin = LOW;

void setup() {

``` pinMode(ledPin, OUTPUT);
```

}

void loop() {

```  analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,volume);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
```

}

October 18, 2005, at 12:32 AM by 195.178.229.25 -

The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the output. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc.

October 18, 2005, at 12:29 AM by 195.178.229.25 -
Changed lines 7-8 from:
to:

http://static.flickr.com/31/53523608_3d4268ba68.jpg

Example of connection of a Piezo to pin 10

October 18, 2005, at 12:21 AM by 195.178.229.25 -

## Play Melody

This example makes use of a Piezo Speaker in order to play melodies. We are taking advantage of the processors capability to produde PWM signals in order to play music. There is more information about how PWM works written by David Cuartielles here and even at K3's old course guide

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 10, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

```
/* Play Melody
* -----------
*
* Program to play melodies stored in an array, it requires to know
*
* The calculation of the tones is made following the mathematical
* operation:
*
*       timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note 	frequency 	period 	PW (timeHigh)
* c 	        261 Hz 	        3830 	1915
* d 	        294 Hz 	        3400 	1700
* e 	        329 Hz 	        3038 	1519
* f 	        349 Hz 	        2864 	1432
* g 	        392 Hz 	        2550 	1275
* a 	        440 Hz 	        2272 	1136
* b 	        493 Hz 	        2028	1014
* C	        523 Hz	        1912 	956
*
* (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

```