Skip to content

Instantly share code, notes, and snippets.

@tyhenry
Last active January 7, 2019 01:01
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 tyhenry/2c8127e4411045588a09252141a9dce5 to your computer and use it in GitHub Desktop.
Save tyhenry/2c8127e4411045588a09252141a9dce5 to your computer and use it in GitHub Desktop.
piezo knock sensor code
/* A simple sketch to detect a secret knock using a piezo transducer
copied from
https://programmingelectronics.com/how-to-make-a-secret-knock-detector-to-trigger-anything-with-only-an-arduino-and-a-few-cheap-components/
to be combined with solenoid control circuit
https://core-electronics.com.au/tutorials/solenoid-control-with-arduino.html
Created JUL 2015 by
Michael James
https://programmingelectronics.com/
This code is in the public domain
*/
const int outputPin = 6; // led indicator connected to digital pin
const int knockSensor = A0; // the piezo is connected to an analog pin
const int thresholdHIGH = 150; // threshold value to decide when the detected knock is hard (HIGH)
const int thresholdLOW = 120; // threshold value to decide when the detected knock is gentle (LOW)
const int secretKnockLength = 4; //How many knocks are in your secret knock
/* This is the secret knock sequence
* 0 represents a LOW or quiet knock
* 1 represents a HIGH or loud knock
* The sequence can be as long as you like, but longer codes increase the difficulty of matching */
const int secretKnock = {0, 0, 1, 0};
int secretCounter = 0; //this tracks the correct knocks and allows you to move through the sequence
int sensorReading = 0; // variable to store the value read from the sensor pin
void setup() {
//Set the output pin as an OUTPUT
pinMode(outputPin, OUTPUT);
//Begin Serial Communication.
Serial.begin(9600);
}
void loop() {
// read the piezo sensor and store the value in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// First determine is knock if Hard (HIGH) or Gentle (LOW)
//Hard knock (HIGH) is detected
if (sensorReading >= thresholdHIGH) {
//Check to see if a Hard Knock matches the Secret Knock in the correct sequence.
if (secretKnock == 1) {
//The Knock was correct, iterate the counter.
secretCounter++;
Serial.println("Correct");
} else {
//The Knock was incorrect, reset the counter
secretCounter = 0;
Serial.println("Fail - You are a spy!");
}//close if
//Allow some time to pass before sampling again to ensure a clear signal.
delay(100);
//Gentle knock (LOW) is detected
} else if (sensorReading >= thresholdLOW) {
//Check to see if a Gentle Knock matches the Secret Knock in the correct sequence.
if (secretKnock == 0) {
//The Knock was correct, iterate the counter.
secretCounter++;
Serial.println("Correct");
} else {
//The Knock was incorrect, reset the counter.
secretCounter = 0;
Serial.println("Fail - You are a spy!");
}//close if
//Allow some time to pass before sampling again to ensure a clear signal.
delay(100);
}//close if else
//Check for successful entry of the code, by seeing if the entire array has been walked through.
if (secretCounter == (secretKnockLength) ) {
Serial.println("Welcome in fellow Illuminante!");
//if the sececret knock is correct, illuminate the LED for a couple seconds
digitalWrite(outputPin, HIGH);
delay(2000);
digitalWrite(outputPin, LOW);
//Reset the secret counter to 0.
secretCounter = 0;
}//close success check
}//close loop
const int piezoPin = A5; // the piezo is connected to an analog pin
const int solenoidPin = 12; // out
const int threshold = 3;
const int solenoidDur = 5000; // milliseconds solenoid is active after buzz detection
int piezoVal = 0; // variable to store the value read from the sensor pin
void setup() {
Serial.begin(9600);
pinMode(solenoidPin, OUTPUT);
digitalWrite( solenoidPin, LOW );
}
void loop() {
piezoVal = analogRead( piezoPin );
if (piezoVal >= threshold) {
Serial.print("Buzz! piezo sensor read: "); Serial.print(piezoVal);
Serial.print(", threshold: "); Serial.print(threshold);
Serial.print("\n\tactivating plunger for ms: "); Serial.print(solenoidDur);
Serial.print(" ...");
digitalWrite( solenoidPin, HIGH );
int start_t = millis();
bool active = true;
while (active){
int elapsed = millis() - start_t;
if ( elapsed % (solenoidDur / 5) == 0){
Serial.print(" ...");
}
else if (elapsed > solenoidDur){
active = false;
Serial.print(" done.\n");
digitalWrite( solenoidPin, LOW );
}
delay(1);
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment