Victor Espinoza – Update 5

Research for programming sound output in Arduino:

*Note: this is the fundamental research that is required to generate sound using the Arduino Uno. It is not the actual implementation of the sound output (this task belongs to another team member). This research is intended to be used to aid Jose in generating the sound output.

Outputting sound to a speaker using Arduino is actually pretty simple. It consists of a couple of key steps that are divided into two sections (Initialization and Implementation):

Initialization:

  1. Defining a relationship between the note, period, and frequency.
    • We achieve this by using define statements for example, defining a c note with a frequency of 261Hz would look like this:
      •            note, period, &  frequency.
        #define  c     3830    // 261 Hz
  2. Store all of the defined notes that are in the tune into an array of integers.
  3.  Store all of the note lengths into another array of integers (this array will be directly related to the note array).
  4. Determine and set the tempo of the melody as well as how long you are going to pause between notes. (You must pause in between notes otherwise it will just be one long continuous sound).
  5. Initialize the tone, beat, and duration values to known states (0).
  6. Connect the speaker to a PWM capable pin.

Implementation:

  1. Use a for loop to easily access the values in the note and note length arrays.
    • The for loop should continue to iterate until the tune has finished.
  2. Assign the tone  value to the current note and the beat value to the current note length.
  3. Determine the appropriate timing for the note (current note length * tempo)
  4. Play the desired note.
  5. Wait for a short amount of time until you play the next note
  6. Repeate the process until the tune has finished playing.

Comments on Playing the Desired Note:

Playing the desired note actually consists of a couple of steps itself. Here is the following logic required to play the desired note:

  1. Create a long variable to keep track of the elapsed time and initialize it to zero.
  2. Make sure that the tone value is greater than zero (meaning that the tone value is an actual note that needs to be played).
    • If this is the case, then we proceed to play the tone
    • If this is not the case, this means that the current tone is a rest beat (so we wait for the rest beat to finish)
  3. Assuming that case #2 was fullfilled, you then need to enter a while loop that executes until the appropriate timing for the note has been fulfilled.
  4. Check to see if the timing for the note has been fulfilled. Exit the loop if this is the case.
  5. Output a high pulse to the speaker for half of the tone duration.
  6. Output a low pulse to the speaker for the other half of the tone duration.
  7. Update the elapsed time.
  8. Go back to step 4 until timing has been met.

This is the general layout for how to generate sound using the Arduino. For a helpful example on how to do this, check out the Arduino Play Melody example here.

 

*Note: Apart from the above research I also spent time becoming more familiar with the 3-D printing tools that we will be using so that I can help 3-D print parts. This way,  we can speed up the rate at which our parts are being printed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s