p16_Buzzer.ino 736 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. Demo code for Project 16 - Buzzer
  3. Make some noise by buzzer
  4. by Maker Studio
  5. http://makerstudio.cc
  6. */
  7. const int buzzerPin = 3; // connect buzzer + to D3, - to GND
  8. int volume = 0; // volume of the buzzering
  9. void setup()
  10. {
  11. pinMode(buzzerPin,OUTPUT); // set as OUTPUT
  12. }
  13. void loop()
  14. {
  15. //make some Di Di Di Di sound
  16. for(int i = 0; i < 10; i++){
  17. digitalWrite(buzzerPin,HIGH);
  18. delay(200);
  19. digitalWrite(buzzerPin,LOW);
  20. delay(200);
  21. }
  22. // make noise from low to high
  23. for(volume = 0; volume < 255; volume++){
  24. analogWrite(buzzerPin,volume);
  25. delay(20);
  26. }
  27. // make noise from high to low
  28. for(volume = 255; volume > 0; volume--){
  29. analogWrite(buzzerPin,volume);
  30. delay(20);
  31. }
  32. }