Created
July 18, 2022 05:33
-
-
Save labsguru/ad6e5021eb3251167bca7b1dd0618ec2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const int knockPin = 8; //https://kitsguru.com/products/the-knock-sensor-module | |
| const int ledPin = 7; | |
| int knockVal = HIGH; | |
| boolean knockAlarm = false; | |
| unsigned long prevKnockTime; | |
| int knockAlarmTime = 100; | |
| void setup () | |
| { | |
| Serial.begin(9600); | |
| pinMode (ledPin, OUTPUT) ; | |
| pinMode (knockPin, INPUT) ; | |
| } | |
| void loop () | |
| { | |
| knockVal = digitalRead (knockPin) ; | |
| if (knockVal == LOW) | |
| { | |
| prevKnockTime = millis(); | |
| if (!knockAlarm) | |
| { | |
| Serial.println("KNOCK, KNOCK"); | |
| digitalWrite(ledPin,HIGH); | |
| knockAlarm = true; | |
| delay(1000); | |
| } | |
| } | |
| else | |
| { | |
| if( (millis()-prevKnockTime) > knockAlarmTime && knockAlarm) | |
| { | |
| digitalWrite(ledPin,LOW); | |
| Serial.println("No Knocks"); | |
| knockAlarm = false; | |
| } | |
| } | |
| } //CREDITS : https://www.electronicshub.org/knock-sensor-with-arduino/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment