8
CMPEH 472 Lab Making Sounds 1. For this lab you will need: Arduino Uno USB cable +5v DC power supply Pushbuttons (5) Breadboard and wires o-scope, multimeter 8-ohm loudspeaker 10k-ohm resistor (5), 150-ohm resistor, and 10k-potentiometer 2. Useful links: a. Tone() reference http://arduino.cc/en/Reference/Tone b. Pentatonic scale http://en.wikipedia.org/wiki/Pentatonic_scale c. While loop reference http://arduino.cc/en/Reference/While d. Define reference http://arduino.cc/en/Reference/Define e. Include reference http://arduino.cc/en/Reference/Include f. Array reference http://arduino.cc/en/Reference/Array 3. Re-read pages 95-105 in your textbook. 4. NOTE: After you’ve created your program sketch, you will need to download the definition file “pitches.h” and place it in your sketch directory. (See page 95 in your textbook for help).

CMPEH_472__Making_Sounds_rev2.docx

Embed Size (px)

Citation preview

CMPEH 472 Lab Making Sounds

1. For this lab you will need:

Arduino Uno

USB cable

+5v DC power supply

Pushbuttons (5)

Breadboard and wires

o-scope, multimeter

8-ohm loudspeaker

10k-ohm resistor (5), 150-ohm resistor, and 10k-potentiometer

2. Useful links:

a. Tone() reference http://arduino.cc/en/Reference/Toneb. Pentatonic scale http://en.wikipedia.org/wiki/Pentatonic_scalec. While loop reference http://arduino.cc/en/Reference/Whiled. Define reference http://arduino.cc/en/Reference/Definee. Include reference http://arduino.cc/en/Reference/Includef. Array reference http://arduino.cc/en/Reference/Array

3. Re-read pages 95-105 in your textbook.

4. NOTE: After you’ve created your program sketch, you will need to download the definition file “pitches.h” and place it in your sketch directory. (See page 95 in your textbook for help).

5. Be sure to familiarize yourself with the details of the following code shown below (listing 5-1) and the schematic, (Figure 5-5 from your textbook). Build this system to make sound sequences.

/*Exploring Arduino - Code Listing 5-1: Arduino Music PlayerBy Jeremy Blum, Exploring Arduino, Wiley and Sons, 2013*/

//Plays a song on a speaker

#include "pitches.h" //Header file with pitch definitions

const int SPEAKER=9; //Speaker Pin

//Note Arrayint notes[] = { NOTE_A4, NOTE_E3, NOTE_A4, 0, NOTE_A4, NOTE_E3, NOTE_A4, 0, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E3, NOTE_A4, 0};

//The Duration of each note (in ms)int times[] = { 250, 250, 250, 250, 250, 250, 250, 250, 125, 125, 125, 125, 125, 125, 125, 125, 250, 250, 250, 250 };

void setup(){ //Play each note for the right duration for (int i = 0; i < 20; i++) { tone(SPEAKER, notes[i], times[i]); delay(times[i]); }}

void loop(){ //Press the reset button to play again.}

Figure 5-5, from your textbook

6. Build and code a pentatonic micro piano system. Use the following code and schematic (Listing 5-2 and Figure 5-6) from your textbook:

/*Exploring Arduino - Code Listing 5-2: Pentatonic Micro PianoBy Jeremy Blum, Exploring Arduino, Wiley and Sons, 2013.*/

//Pentatonic Piano//C D E G A #define NOTE_C 262 //Hz#define NOTE_D 294 //Hz#define NOTE_E 330 //Hz#define NOTE_G 392 //Hz#define NOTE_A 440 //Hz

const int SPEAKER=9; //Speaker on Pin 9

const int BUTTON_C=7; //Button Pinconst int BUTTON_D=6; //Button Pinconst int BUTTON_E=5; //Button Pinconst int BUTTON_G=4; //Button Pinconst int BUTTON_A=3; //Button Pin

void setup(){ //No setup needed //Tone function sets outputs}

void loop(){ while (digitalRead(BUTTON_C)) tone(SPEAKER, NOTE_C); while(digitalRead(BUTTON_D)) tone(SPEAKER, NOTE_D); while(digitalRead(BUTTON_E)) tone(SPEAKER, NOTE_E); while(digitalRead(BUTTON_G)) tone(SPEAKER, NOTE_G); while(digitalRead(BUTTON_A)) tone(SPEAKER, NOTE_A); //Stop playing if all buttons have been released noTone(SPEAKER);}

Figure 5-6, from your textbook

7. Questions to answer:

a. Explain the tone() function and give an example.b. Explain how this line of code works: #define NOTE_C 262 //Hzc. Explain how this line of code works in your program: #include "pitches.h"d. Create a flow chart that matches up with code listing 5-1, page 101 in your textbook.e. Create a flow chart that matches up with code listing 5-2, page 104 in your textbook. f. Explain in detail how the following for-loop works in your program:

for (int i = 0; i < 20; i++) { tone(SPEAKER, notes[i], times[i]); delay(times[i]); }}

g. What is a pentatonic music scale?h. At home: create a sound or song to share during a Thursday class; submit your code in the

lab report.