Analog Ouput Lab, or “Remember how fun it was to annoy people with noisemakers?”
It’s my now very much belated entry detailing the Analog Output lab! Hooray!
First, all the ingredients for a successful lab:
Following the instructions from the first section of the lap, I wired up the servo motor like so:
After loading the code into the Arduino IDE, turning the potentiometer would cause the servo motor to turn to a specific position as well. Because we map the full range of values from the potentiometer (0-1023) to the full range of values that the servo motor can receive (0-179), one full turn of the potentiometer moves the motor between its two extremes. This is expressed in the code as (val = map(val, 0, 1023, 0, 179);
I noticed that if one turned the potentiometer knob to its very highest setting, the motor would act as if it were struggling to get beyond its furthest position. This is normal and just means that the cheaply made servo motors are not all calibrated precisely.
Changing the line to (val = map(val, 0, 1023, 0, 50)
allows a full turn of the potentiometer knob to control only a portion of the range of the servo motor.
Next, I swapped the servo motor for a piezo speaker. I also had to add a resistor to keep from blowing out the little speaker.
After loading the Tone library and the appropriate code into the IDE, I made some annoying sounds as directed:
The final portion of the lab was to invent some kind of musical instrument or moving device. I opted to create a musical device that one could play using light. By varying the intensity of light hitting a photoresistor, one could potentially play different kinds of notes.
To vary the intensity of light in a predictable way, I printed strips of inkjet overhead transparency with blocks of various shades of gray. I also used a constant source of light: a white iPhone 3G screen set to the brightest setting. By slide this strip of paper directly between the photoresistor and the light source, I found that I could get fairly exact, discrete readings from the photoresistor. These I could then map to certain tonal frequencies to play the C major scale.
Here is a circuit diagram of how I set this whole thing up:
[insert circuit diagram after scanner gets fixed]
Here is a diagram of how I put the foam-core housing of the music box together:
[insert drawing]
And here is the code that drives it:
#include <Tone.h>
Tone mytone; // declare a tone name
void setup() {
// initialize the tone
mytone.begin(8);
Serial.begin(300); // for debugging
}
void loop() {
// read the analog input
int inVar = analogRead(0);
// output to serial monitor so I know what numbers to aim for
Serial.println(inVar, DEC);
// map an input numbher to a note frequency
int note;
if (inVar<=270) {
// it's dark! play nothing!
// mytone.stop();
}
else if(inVar>270 && inVar<=300) { // center around 280
mytone.play(523); //C
}
else if(inVar>300 && inVar<=467) { // 422
mytone.play(587); //D
}
else if(inVar>467 && inVar<=543) { // 513
mytone.play(659); //E
}
else if(inVar>543 && inVar<=597) { // 573
mytone.play(698); //F
}
else if(inVar>597 && inVar<=629) { // 615
mytone.play(784); //G
}
else if(inVar>629 && inVar<=655) { // 643
mytone.play(880); //A
}
else if(inVar>655 && inVar<=676) { // 668
mytone.play(988); //B
}
else if(inVar>676 && inVar<=695) { // 685
mytone.play(1046); //C
}
else { // it's bright! play nothing.
mytone.stop();
}
}
The toughest part in setting this up was determining which range of values from the potentiometer would map to which musical notes. I recorded the range of numbers outputted by Serial.println(inVar, DEC)
while sliding the strip of transparency over the photoresistor, pausing at each block of gray. After a few rounds of this I was finally able to determine which numbers matched up with which gray block—they can sometimes fluctuate a bit, so I padded each range with +/- 5. Then I wrote a really long series of if-statements… (see above)
Here are some videos of the final product in action: