Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created October 22, 2021 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/dd539db1caca27e2032d2512b35f58d9 to your computer and use it in GitHub Desktop.
Save jenschr/dd539db1caca27e2032d2512b35f58d9 to your computer and use it in GitHub Desktop.
int lastVal = 0;
long lastKnock = 0;
void setup() {
Serial.begin( 9600 );
}
void loop() {
int readVal = analogRead( A2 );
if( readVal > lastVal+5 ){
int knockDiff = abs(readVal)-lastVal;
long nowTime = millis();
int diff = nowTime - lastKnock;
if( lastKnock != 0 && diff > 20 && diff < 2000 ){
Serial.print(knockDiff);
Serial.print(":");
if( diff > 300 ){
Serial.println( "knock" );
} else {
Serial.println( "doubleknock" );
}
}
lastKnock = nowTime;
}
lastVal = abs( readVal );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment