Lab 5 : Piezo Speaker by Victor Tjhia
Playing piezo with potentiometer
Piezo speaker will produce different melody as you spin the potentiometer
components:
- arduino
- piezo speaker
- potentiometer
/* Theremin
* --------
* Modified by Victor Tjhia
*
* Created 24 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
int potPin = 0; // select the input pin for the potentiometer
int speakerPin = 7;
int val = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerPin, LOW);
val = analogRead(potPin); // read value from the sensor
val = val/2; // process the value a little
for( int i=0; i<100; i++ ) { // play it for 10 cycles
Serial.println(val);
digitalWrite(speakerPin, LOW);
delayMicroseconds(val);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(val);
}
}