Comp 2303 Elements of Computer Science: Embedded Systems

A Musical Instrument

Portfolio

The activities in this tasksheet contribute to ILO1 and ILO2

Activities

1 / Here we shall use light level to change the pitch (frequency) of a sound.
(a) Connect the circuit as shown here with the usual 10k resistor in series with the LDR. The LED is replaced by a small loudspeaker which does not need a series resistor, it already has enough resistance. Connect the output of the LDR to Arduino analog input A0 and connect the loudspeaker to digital output 11.

Now let’s write some code to grab the voltage from the LDR and convert this to a tone.
(b) Open up your streetlight sketch and immediately save it with a name of your choice. Remove all code except the lines which grab the LDR voltage and send this to the serial port.
There is a built in Arduino function which outputs a tone of a certain “pitch” (integer) to a pin. It looks like this, tone(pinNumber,pitch);You can google the pitches of sensible notes, middle-A is 440Hz and the A above is 880Hz. Since the voltage is in the range of 0-5 then we need to scale up this value to make the pitch
(c) Declare an integer variable “pitch” and then write a line of code to calculate the pitch like this
pitch = number*sense; where you choose a reasonable “number”.
(d) Now add a line of code to output a tone, follow this with a second delay, then follow this with a line noTone(pinNumber); to temporarily switch the sound off.
(e) Compile, debug, upload and test. The pitch should change as you allow more or less light into the LDR.
2. / A better way to convert sensed voltage to pitch. In the above activity we converted sense to pitch by a simple multiplication. Let’s do something more sophisticated which is very useful in general.
If we want to scale a variable then we can use the map(…); function. Let’s say “sense” has a range of A to B, and we want to scale this to a “pitch” in the corresponding range P to Q. The following function will do this
pitch = map(sense,A,B,P,Q);
so we must decideon the values of A,B,P,Q.
(a) Save your previous sketch with a new name. Now remove the line which converts the raw value from analogRead (range 0 to 1023) to 0 to 5.
(b) Run your sketch and record the largest and smallest raw values from the analog pin using the serial monitor. Use the smallest for the value of A, and the largest for B.
The values of P and Q are the lowest and highest pitches you want. Google values for these. I chose P = 440 and Q = 1760
(c) Now write your map(…); line of code using the values A,B,P,Q you have chosen.
(d) Run your sketch which should now create a larger range of sounds.
3. / Let’s program the Arduino to play a simple melody. Notes in a melody must have (i) the correct pitch, (ii) the correct length (in seconds or milliseconds) and (iii) must have a gap between the previous note.
(a) Choose a tune, google the notes and look up the pitch of each note. Find also their relative lengths (crotchet, minim, …).
(b) Declare two arrays to hold the pitch and the length. Here’s how to declare and assign values. I’ve chosen the first few notes of Twinkle Twinkle. The first four notes are of equal length, so I’ve assigned a value “1” (we’ll convert this to milliseconds later). Use “1” for the shortest note.
int pitch[] = {262,262,392,392};
int length[] = {1,1,1,1};
(c) Count the notes and assign this to a variable nrNotes once you have declared it.
(d) Now write a for loop to grab the values from each array and use these to create a tone. Here’s a start, but you’ll need to add in some more lines,
for(inti=0; inrNotes; i++) {
p = pitch[i];
..
tone(11,p,l);
..
}
where variable “p” is the i’th pitch and “l” is the I’th length. You’ll need to declare these variables somewhere.
(e) Now grab the length of the i’th note and assign this to “l”, but also multiply by a number such as 250 or 500 to scale it to a decent number of milliseconds.
(f) Write a line of code to delay by this amount.
(g) End your code by calling noTone(11); to turn off the sound ready for the next note.
Great work to add to your portfolio.