Advertisement
britneybeatey

Piezo Buzzer

Jun 29th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. /*
  2. * Britney Beatey
  3. * 6/21/17
  4. */
  5. const int photoPin = A0;
  6. const int piezoPin = 8;
  7. int min = 40;
  8. int max = 1000;
  9. int photoVal = 0;
  10. unsigned long time;
  11. int minVal = 1000;
  12. int maxVal = 0;
  13. int timeVal = 0;
  14.  
  15.  
  16. void setup()
  17. {
  18. // put your setup code here, to run once:
  19. pinMode (photoPin, INPUT);
  20. pinMode (piezoPin, OUTPUT);
  21. Serial.begin(9600);
  22.  
  23.  
  24. while(millis() - timeVal < 5000)
  25. {
  26. photoVal = analogRead (photoPin);
  27. if (photoVal > max)
  28. {
  29. max = photoVal;
  30. }
  31. else if (photoVal < min)
  32. {
  33. min = photoVal;
  34. }
  35. }
  36. }
  37. void loop() {
  38. // put your main code here, to run repeatedly:
  39. photoVal = analogRead (photoPin);
  40. photoVal = map(photoVal, min, max, 50, 560);
  41. tone(piezoPin, photoVal, 20);
  42. noTone (piezoPin);
  43. delay (100);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement