Starting from:
$30

$24

LAB #07 SOlution

Objective:




To become familiar with the use of timers in computer applications. Specifically, to implement a piano like keyboard program using the ATmega2560 timer1 in Normal mode and an external speaker.




Procedure:




1. Starting with the file Echo1.ino using the Arduino serial port library functions copy/pasted from:




/* Simple Serial ECHO script : Written by ScottC 03/07/2012 */ http://arduinobasics.blogspot.com/2012/07/arduino-basics-simple-arduino-serial.html




write a C program for the Arduino Mega SBC which will monitor the keyboard for inputs of A, B, C, D, E, F, and G. Depending upon which key was pressed use the timer to generate a square wave on PortB.6 of the ATmega2560 (Arduino Digital pin 12) of the appropriate frequency for the middle octave of that musical note (see Table). The note should last until another key is pressed, at which time the new note will be played. If the escape key is pressed the note will be turned off.




2. Connect the lab speakers to PortB.6 and ground to actually play the notes so you can hear them.




BE SURE TO ALWAYS TURN OFF THE SBC POWER BEFORE CONNECTING OR DISCONNECTING ANYTHING TO IT.




Connect a sine wave signal generator to one of the lab speakers, set it to one of the musical note frequencies, and listen to it. Compare the sound of this note with the same frequency note generated from the SBC speaker. Explain why they sound different.



NOTE
FREQUENCY
A
440 Hz
A#
466 Hz
B
494 Hz
middle C
523 Hz
C#
554 Hz
D
587 Hz
D#
624 Hz
E
659 Hz
F
698 Hz
F#
740 Hz
G
784 Hz
G#
831 Hz






The Sharp notes are included in the Table for completeness. If you choose to implement all of the notes including sharps you will need to decide on an interface protocol which will distinguish between regular and sharp notes. For example, you might define regular notes as upper or lower case and sharps as the opposite.




If you choose to implement all of the notes you will receive up to 50 points (on a scale of 100) extra credit on this lab.







HINT: You can probably write a single subroutine which outputs the notes and checks for input. Then, you can call this single subroutine with different timer "load" values for the different notes.

More products