3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Play the French Can Can Using an Arduino and Buzzer

Step 9Play the Can Can



It's time to make some music! Using a grand piano (or Garage Band in my case), listen to the different notes you want to create. Play the song by ear, or look it up on sheet music. For each note you wish to create, count from the first note in the array, F, to figure out which note integer in the array matches the note being played on the piano.

NoteInt Example: 

G = ??: We'll figure this out.
F = 0
Add an octave (12) to make the pitch more pleasing on this buzzer = 12
G is 2 keys higher than F (black and white key) on the keyboard, so add 2 = 14
G = 14

For play length in this example:

a sixteenth note = 250 miliseconds
an eighth note = 500 miliseconds
a quarter note = 1000 miliseconds
a half note = 2000 miliseconds
a whole note = 4000 miliseconds

Also, the default 'breath' length in the playNote() function has been changed to 20, to give a slight pause between each note to give a 'tee-tee-tee-tee' effect when playing. Here are the results:

----------------copy after this line----------------


// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com

// Additions by Christopher Stevens
// http://www.christopherstevens.cc

//referenced from http://www.phy.mtu.edu/~suits/notefreqs.html
//starting with F noteFreqArr[1]
int noteFreqArr[] = {
49.4, 52.3, 55.4, 58.7, 62.2, 65.9, 69.9, 74, 78.4, 83.1, 88, 93.2,
98.8, 105, 111, 117, 124, 132, 140, 148, 157, 166, 176, 186,
198, 209, 222, 235, 249, 264, 279, 296, 314, 332, 352, 373,
395, 419, 444, 470, 498, 527, 559, 592, 627, 665, 704, 746,
790, 837, 887, 940, 996, 1050, 1110, 1180, 1250, 1320, 1400, 1490,
1580, 1670, 1770, 1870, 1990, 2100};

void setup() {
  pinMode(4, OUTPUT); // set a pin for buzzer output
}

void playNote(int noteInt, long length, long breath = 20) {
  length = length - breath;
  buzz(4, noteFreqArr[noteInt], length);
  if(breath > 0) { //take a short pause or 'breath' if specified
    delay(breath);
  }
}

void loop() {
  //main course
  playNote(24,500);
 
  playNote(17,1000);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(24,500);
  playNote(24,500);
  playNote(24,250);
  playNote(26,250);
  playNote(21,250);
  playNote(22,250);
  playNote(19,500);
  playNote(19,500);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(17,250);
  playNote(29,250);
  playNote(28,250);
  playNote(26,250);
  playNote(24,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
 
  playNote(17,1000);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(24,500);
  playNote(24,500);
  playNote(24,250);
  playNote(26,250);
  playNote(21,250);
  playNote(22,250);
  playNote(19,500);
  playNote(19,500);
  playNote(19,250);
  playNote(22,250);
  playNote(21,250);
  playNote(19,250);
  playNote(17,250);
  playNote(24,250);
  playNote(19,250);
  playNote(21,250);
  playNote(17,250);
  delay(250);
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}

----------------copy before this line----------------


The Arduino is now playing the Can Can, but it is still... blah blah. The next step gives the notes a bit more texture.

« Previous StepDownload PDFView All StepsNext Step »
1 comment
Mar 31, 2011. 8:16 PMemihackr97 says:
Men this is exactly what i was looking for, 5 stars!!!!

Great 'ible!!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
2
Followers
1
Author:Ultrus(Christopher Stevens)
Web developer and designer who loves renovating and empowering over-sized web projects. Experience in developing and integrating ground breaking creative solutions online. Enjoys solving complex devel...
more »