Advertisement
mem1889

Práctica 11

Feb 16th, 2015
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /* Arduino + Buzzer + Photoresistor = Home-made Theremin
  2. http://antoniolopes.info/wp/blog/2012/06/28/arduino-buzzer-photoresistor-home-made-theremin/
  3. */
  4.  
  5. int prPin = 0; // Pin where the photo resistor is connected to
  6.  
  7. int prReading; // The analog reading from the photoresistor
  8.  
  9. int buzzerPin = 4; // Connect Buzzer to Pin 4
  10.  
  11. long buzzerFreq; // The frequency to buzz the buzzer
  12.  
  13. // You can experiment with these values:
  14. long BUZZ_FREQ_MAX = 2500; // Maximum frequency for the buzzer
  15.  
  16. long PR_MAX = 1023; // Maximum value for the photoresistor
  17.  
  18.  
  19.  
  20. void setup() {
  21. pinMode(buzzerPin, OUTPUT); // set a pin for buzzer output
  22.  
  23. }
  24.  
  25.  
  26.  
  27. void loop() {
  28.  
  29. prReading = analogRead(prPin); // Values 0-1023
  30.  
  31. buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;
  32.  
  33. buzz(buzzerPin, buzzerFreq, 10);
  34.  
  35. }
  36.  
  37.  
  38.  
  39. void buzz(int targetPin, long frequency, long length) {
  40.  
  41. long delayValue = 1000000/frequency/2;
  42.  
  43. long numCycles = frequency * length/ 1000;
  44.  
  45. for (long i=0; i < numCycles; i++){
  46.  
  47. digitalWrite(targetPin,HIGH);
  48.  
  49. delayMicroseconds(delayValue);
  50.  
  51. digitalWrite(targetPin,LOW);
  52.  
  53. delayMicroseconds(delayValue);
  54.  
  55. }
  56.  
  57. }
  58.  
  59. /* fin del codigo */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement