Skip to content

Instantly share code, notes, and snippets.

@ViliusKraujutis
Created March 20, 2016 11:57
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 ViliusKraujutis/2aa62165bf5ee2f7b179 to your computer and use it in GitHub Desktop.
Save ViliusKraujutis/2aa62165bf5ee2f7b179 to your computer and use it in GitHub Desktop.
does not detect knocks on table, but senses shocks while holding in hand
const int ledPin = 31; // define ledPin Interface
const int shockPin = 30; // define the percussion Sensor Interface
int val; // define numeric variables val
long lastKnockTimestamp = 0;
int knockTimeout = 2000;
void setup () {
Serial.begin(9600);
pinMode (ledPin, OUTPUT) ; // define ledPin as output interface
pinMode (shockPin, INPUT) ; // define knock sensor output interface
Serial.println("Running...");
}
void loop () {
val = digitalRead (shockPin) ; // read digital interface is assigned a value of 3 val
if (val == LOW) {
lastKnockTimestamp = millis();
}
if (millis() - lastKnockTimestamp < knockTimeout) {
digitalWrite (ledPin, HIGH);
} else {
digitalWrite (ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment