Monday, December 1, 2014

ETEC 597: Maker Movement - Week 5

This week's build allowed me to get my family involved in the process.  Typically, my wife walks in the office, sees me working on the electronics "thingy," turns around, and leaves.  With the addition of the piezo speaker, though, she could actually get some sensory feedback (besides flashy lights) that she could connect to electronics in her life.

It was enjoyable playing around with the code and making the piezo speaker play certain notes and tones.  I am not (nor have I ever been) a music person, so I enlisted the help of some of my band students to help me extrapolate some music to have my speaker output.  I ultimately decided on a Christmas classic, Jingle Bells, as the tune for my Arduino.  The addition of the push button was a little more challenging but definitely doable since we started working with control structures in the programming language.

Code

const int buttonPin = 2;     // pushbutton
const int ledPin =  13;      // on-board LED
const int buzzerPin = 12;    // buzzer

const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 100;

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  pinMode(ledPin, OUTPUT); 
  pinMode(buzzerPin, OUTPUT);  

  pinMode(buttonPin, INPUT);    
}

void loop(){

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {    
    digitalWrite(ledPin, HIGH);

    int i, duration;
  
    for (i = 0; i < songLength; i++) // step through the song arrays
    {
        duration = beats[i] * tempo;  // length of note/rest in ms
        if (notes[i] == ' ')          // is this a rest? 
        {
           delay(duration);            // then pause for a moment
        }
        else                          // otherwise, play the note
        {
           tone(buzzerPin, frequency(notes[i]), duration);
           delay(duration);            // wait for tone to finish
        }
        delay(tempo/10);              // brief pause between notes
     }
  
     // We only want to play the song once, so we'll pause forever:
     while(true){}

  }   
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    noTone(12);
  }
}

int frequency(char note) 
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.
  
  int i;
  const int numNotes = 8;  // number of notes we're storing
  
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  
  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.
  
  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.

}

Code Snippet of Song Change

The notes and beats arrays were the biggest changes when going from the original song to Jingle Bells.

const int songLength = 26;
char notes[] = "eeeeeeegcde fffffeeeeddedg";     //Jingle Bells
int beats[] = {1,1,2,1,1,2,1,1,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2};

int tempo = 300;

Circuit Picture



Circuit Schematics


Sorry for the orientation; I could not get this particular picture to flip.

Videos


Original Tune


Jingle Bells

Extension

I remember my fascination with computer-generated sounds from the time that I started with my first computer (a Tandy RL-1000 at a whopping 8MHz!)  I didn't know exactly how they worked but I was very curious and spent hours upon hours playing around with the MIDI editor that I had installed.  Although sounds are now much more complex (with many more channels and higher bit rates,) the principle is the same.  Sounds are composed of bits of data that control the vibrations of the speaker.  I would like to try this same experiment in the future with a full-size speaker rather than the piezo to see if the vibrations of the speaker can be seen.

No comments:

Post a Comment