Description
A pot twisted to play a tone with a led indicated if there is an input
Components Used
10k Resistor
100 Resistor
1 Led
1 Speaker
1 Pot
Source code
/*
* Sound Serial for stick
* ------------
*
* Created 4 Oct. 2011
* copyleft 2011 Fan, Wai Pio
*
* One pot control to play tones
*
* modification of the following
*
* Sound Serial
* (cleft) 2005 D. Cuartielles for K3
*
* Updated by Tod E. Kurt <tod@todbot.com> to use new Serial. commands
* and have a longer cycle time.
*
*/
int potPin = 0; // select the input pin for the potentiometer
int speakerPin = 7; // select the pin for the speaker
int ledPin = 13;
int val = 0; // variable to store the value coming from the sensor
int oldVal = 0;
// note names and their corresponding half-periods
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int ledState = LOW;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
val = (val / 147.0 + 0.5);
Serial.println(val);
digitalWrite(speakerPin, LOW);
if(val != oldVal){
ledState = !ledState; // flip the LED state
digitalWrite(ledPin, ledState); // write to LED
}
oldVal = val;
count = val;
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
}
}