Tutorial.Tone2 History

Hide minor edits - Show changes to output

June 12, 2012, at 04:22 PM by Tom Igoe -
Changed lines 32-37 from:
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 100 - 1000 usually works pretty well for this sketch.

You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1000. Change the values in the map() comand to match the range for your sensor.


to:
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 120 - 1500 usually works pretty well for this sketch.

You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1000. Change the values in the map() command to match the range for your sensor.


May 02, 2012, at 03:36 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/02.Digital/tonePitchFollower/tonePitchFollower.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:08 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:04 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.ino lang=arduino tabwidth=4:)
January 12, 2011, at 12:48 PM by Davide Gomba -
January 12, 2011, at 12:48 PM by Davide Gomba -
Changed lines 22-23 from:
Connect one terminal of your speaker to digital pin 8 through a 100 ohm resistor, and its other terminal to ground. Power your photoresistor with 5V, and connect it to analog 0 with the addition of a 4.7K resistor to ground.
to:
Connect one terminal of your speaker to digital pin 9 through a 100 ohm resistor, and its other terminal to ground. Power your photoresistor with 5V, and connect it to analog 0 with the addition of a 4.7K resistor to ground.
September 24, 2010, at 06:05 AM by Tom Igoe -
Changed lines 50-51 from:
* [[(Reference/)map]]0
to:
* [[(Reference/)map]]()
September 23, 2010, at 10:31 PM by Christian Cerrito -
Changed lines 3-4 from:
!!Pitch follower using the tone() function
to:
!!!Pitch follower using the tone() function
September 17, 2010, at 05:44 PM by Tom Igoe -
Changed line 52 from:
* [[Tone1 | Tone]] - play a melody with a piezo speaker
to:
* [[Tone | Tone]] - play a melody with a piezo speaker
September 16, 2010, at 10:07 PM by Tom Igoe -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
August 25, 2010, at 05:14 AM by Christian Cerrito -
Changed lines 7-8 from:
''Hardware''
to:
!!!Hardware Required
August 25, 2010, at 05:13 AM by Christian Cerrito -
Added lines 7-15:
''Hardware''

* 8-ohm speaker
* 1 photocell
* 4.7K ohm resistor
* 100 ohm resistor
* breadboard
* hook up wire
Changed lines 22-23 from:
to:
Connect one terminal of your speaker to digital pin 8 through a 100 ohm resistor, and its other terminal to ground. Power your photoresistor with 5V, and connect it to analog 0 with the addition of a 4.7K resistor to ground.
Changed lines 43-54 from:
(:divend:)
to:
(:divend:)


!!!See Also:
* [[(Reference/)Array]]()
* [[(Reference/)for]]()
* [[(Reference/)tone]]()
* [[(Reference/)map]]0

* [[Tone1 | Tone]] - play a melody with a piezo speaker
* [[Tone3 | SimpleKeyboard]] - a three-key musical keyboard using force sensors and a piezo speaker.
* [[Tone 4]] - play tones on multiple speakers sequentially
August 24, 2010, at 08:10 AM by Christian Cerrito -
Changed line 31 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
January 22, 2010, at 12:05 AM by Tom Igoe -
Changed lines 31-46 from:
%color=#7e7e7e%/*
%color=#7e7e7e%  Pitch follower
%color=#7e7e7e% 
%color=#7e7e7e% Plays a pitch that changes based on a changing analog input
%color=#7e7e7e% 
%color=#7e7e7e% circuit:
%color=#7e7e7e% * 8-ohm speaker on digital pin 8
%color=#7e7e7e% * photoresistor on analog 0 to 5V
%color=#7e7e7e% * 4.7K resistor on analog 0 to ground
%color=#7e7e7e% 
%color=#7e7e7e% created 21 Jan 2010
%color=#7e7e7e% by Tom Igoe 
%color=#7e7e7e% 
%color=#7e7e7e% http://arduino.cc/en/Tutorial/Tone2
%color=#7e7e7e% 
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/tonePitchFollower/tonePitchFollower.pde lang=arduino tabwidth=4:)
Deleted lines 32-53:

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%// initialize serial communications (for debugging only):
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%// read the sensor:
  %color=#cc6600%int%% sensorReading = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%// print the sensor reading so you know its range
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%println%%(sensorReading);
  %color=#7e7e7e%// map the pitch to the range of the analog input.
  %color=#7e7e7e%// change the minimum and maximum input numbers below
  %color=#7e7e7e%// depending on the range your sensor's giving:
  %color=#cc6600%int%% thisPitch = %color=#cc6600%map%%(sensorReading, 400, 1000, 100, 1000);

  %color=#7e7e7e%// play the pitch:
  %color=#cc6600%tone%%(8, thisPitch, 10);

}

January 21, 2010, at 08:06 PM by Tom Igoe -
Added lines 1-70:
''Examples > Digital I/O''

!!Pitch follower using the tone() function

This example shows how to use the tone() command to generate a pitch that follows the values of an analog input

!!!Circuit

%height=300px%[[Attach:arduino_speaker_photocell_bb.png | Attach:arduino_speaker_photocell_bb.png]]

[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]


!!!Schematic

[-click the image to enlarge-]

%height=300px%[[Attach:arduino_speaker_photocell_schem.png | Attach:arduino_speaker_photocell_schem.png]]


!!! Code
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 100 - 1000 usually works pretty well for this sketch.

You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1000. Change the values in the map() comand to match the range for your sensor.



The sketch is as follows:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e%  Pitch follower
%color=#7e7e7e% 
%color=#7e7e7e% Plays a pitch that changes based on a changing analog input
%color=#7e7e7e% 
%color=#7e7e7e% circuit:
%color=#7e7e7e% * 8-ohm speaker on digital pin 8
%color=#7e7e7e% * photoresistor on analog 0 to 5V
%color=#7e7e7e% * 4.7K resistor on analog 0 to ground
%color=#7e7e7e% 
%color=#7e7e7e% created 21 Jan 2010
%color=#7e7e7e% by Tom Igoe 
%color=#7e7e7e% 
%color=#7e7e7e% http://arduino.cc/en/Tutorial/Tone2
%color=#7e7e7e% 
%color=#7e7e7e% */


%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%// initialize serial communications (for debugging only):
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%// read the sensor:
  %color=#cc6600%int%% sensorReading = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%// print the sensor reading so you know its range
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%println%%(sensorReading);
  %color=#7e7e7e%// map the pitch to the range of the analog input.
  %color=#7e7e7e%// change the minimum and maximum input numbers below
  %color=#7e7e7e%// depending on the range your sensor's giving:
  %color=#cc6600%int%% thisPitch = %color=#cc6600%map%%(sensorReading, 400, 1000, 100, 1000);

  %color=#7e7e7e%// play the pitch:
  %color=#cc6600%tone%%(8, thisPitch, 10);

}


(:divend:)

Share