Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. const int passiveBuzzerPin = A0;
  2. const int activeBuzzerPin = 9;
  3. const int pushButton = 2;
  4. int maxBuzzerValue = 0;
  5. int passiveBuzzerValue = 0;
  6.  
  7. #define NOTE_A4 440
  8. #define NOTE_C5 523
  9. #define NOTE_E5 659
  10.  
  11. int buttonState;
  12. int lastButtonState = LOW;
  13. void setup() {
  14. // put your setup code here, to run once:
  15. Serial.begin(9600);
  16. pinMode(passiveBuzzerPin, INPUT);
  17. pinMode(activeBuzzerPin, OUTPUT);
  18. pinMode(pushButton, INPUT_PULLUP);
  19. }
  20.  
  21. unsigned long lastDebounceTime = 0;
  22. unsigned long debounceDelay = 50;
  23.  
  24. void buzzerPlay() {
  25. tone(activeBuzzerPin, NOTE_C5, 125);
  26.  
  27. }
  28.  
  29. const int threshold = 100;
  30. int playState = 0;
  31. unsigned long initialKnock = 0;
  32. int reached = 0;
  33. void loop() {
  34. // put your main code here, to run repeatedly:
  35. passiveBuzzerValue = analogRead(passiveBuzzerPin);
  36. int reading = digitalRead(pushButton);
  37.  
  38. if (reading != lastButtonState) {
  39. lastDebounceTime = millis();
  40. }
  41.  
  42. if (passiveBuzzerValue >= threshold && reached == 0) {
  43. initialKnock = millis();
  44. reached = 1;
  45. }
  46.  
  47. if (millis() - initialKnock == 5000 && reached == 1) {
  48. playState = 1;
  49. }
  50. if ((millis() - lastDebounceTime) > debounceDelay) {
  51. if (reading != buttonState) {
  52. buttonState = reading;
  53.  
  54. if (buttonState == HIGH) {
  55. playState = !playState;
  56. initialKnock = 0;
  57. }
  58. }
  59. }
  60.  
  61. Serial.print(initialKnock);
  62. //Serial.print(" ");
  63. Serial.println(passiveBuzzerValue);
  64. if (playState)
  65. buzzerPlay();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement