Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int passiveBuzzerPin = A0;
- const int activeBuzzerPin = 9;
- const int pushButton = 2;
- int maxBuzzerValue = 0;
- int passiveBuzzerValue = 0;
- #define NOTE_A4 440
- #define NOTE_C5 523
- #define NOTE_E5 659
- int buttonState;
- int lastButtonState = LOW;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(passiveBuzzerPin, INPUT);
- pinMode(activeBuzzerPin, OUTPUT);
- pinMode(pushButton, INPUT_PULLUP);
- }
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- void buzzerPlay() {
- tone(activeBuzzerPin, NOTE_C5, 125);
- }
- const int threshold = 100;
- int playState = 0;
- unsigned long initialKnock = 0;
- int reached = 0;
- void loop() {
- // put your main code here, to run repeatedly:
- passiveBuzzerValue = analogRead(passiveBuzzerPin);
- int reading = digitalRead(pushButton);
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if (passiveBuzzerValue >= threshold && reached == 0) {
- initialKnock = millis();
- reached = 1;
- }
- if (millis() - initialKnock == 5000 && reached == 1) {
- playState = 1;
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading != buttonState) {
- buttonState = reading;
- if (buttonState == HIGH) {
- playState = !playState;
- initialKnock = 0;
- }
- }
- }
- Serial.print(initialKnock);
- //Serial.print(" ");
- Serial.println(passiveBuzzerValue);
- if (playState)
- buzzerPlay();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement