Project 5.02 Light and Sound
The code is simple but it produces an interesting effect. Upload the sketch and then wave a hand over the Arno or hold it up to a light source. It’s fun to ask someone to find the phototransistor on the board. Most people I’ve asked figure it out pretty quickly by listening to the tone that’s generated as they hold their hand over different parts of the board.
Concepts: analogRead, frequencies, map function
Circuits:
Concepts: analogRead, frequencies, map function
Circuits:
The phototransistor on the Arno board has two pins. One is connected to the 5V power supply and the other to a resistor that is connected to ground. The phototransistor and resistor act as a voltage divider. The Arno pin photoTran reads the voltage drop between the phototransistor and resistor:
reading = analogRead(photoTran);
We set the frequency of the piezo with the map function. We map the range of the reading, o to 1023, to a range of 1000 to 10,000 hertz:
frequency = map(reading,0,1023,1000,10000);
Finally, we call the piezoTone function that we developed in Projects 4. The frequency used in the call to piezoTone is set by the phototransistor:
piezoTone(frequency,10);
The tone lasts for 10 milliseconds. The frequency can then be updated as the program moves back to the top of the loop() block and takes a new measurement of photoTran.
reading = analogRead(photoTran);
We set the frequency of the piezo with the map function. We map the range of the reading, o to 1023, to a range of 1000 to 10,000 hertz:
frequency = map(reading,0,1023,1000,10000);
Finally, we call the piezoTone function that we developed in Projects 4. The frequency used in the call to piezoTone is set by the phototransistor:
piezoTone(frequency,10);
The tone lasts for 10 milliseconds. The frequency can then be updated as the program moves back to the top of the loop() block and takes a new measurement of photoTran.