Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Arduino + Buzzer + Photoresistor = Home-made Theremin
- http://antoniolopes.info/wp/blog/2012/06/28/arduino-buzzer-photoresistor-home-made-theremin/
- */
- int prPin = 0; // Pin where the photo resistor is connected to
- int prReading; // The analog reading from the photoresistor
- int buzzerPin = 4; // Connect Buzzer to Pin 4
- long buzzerFreq; // The frequency to buzz the buzzer
- // You can experiment with these values:
- long BUZZ_FREQ_MAX = 2500; // Maximum frequency for the buzzer
- long PR_MAX = 1023; // Maximum value for the photoresistor
- void setup() {
- pinMode(buzzerPin, OUTPUT); // set a pin for buzzer output
- }
- void loop() {
- prReading = analogRead(prPin); // Values 0-1023
- buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;
- buzz(buzzerPin, buzzerFreq, 10);
- }
- void buzz(int targetPin, long frequency, long length) {
- long delayValue = 1000000/frequency/2;
- long numCycles = frequency * length/ 1000;
- for (long i=0; i < numCycles; i++){
- digitalWrite(targetPin,HIGH);
- delayMicroseconds(delayValue);
- digitalWrite(targetPin,LOW);
- delayMicroseconds(delayValue);
- }
- }
- /* fin del codigo */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement