mercoledì 15 giugno 2016

Arduino Tutorial - Easy secret knock detector

In this project I build a circuit with a Led and a piezo to function as a on and off a Led when it receives a number of knocking sounds within a given range.


Step 1: Things you need

 Hardware
  • LED
  • Diode(Zener 5.1v).
  • Piezo Buzzer
  • 470Ω Resistor
  • 1MΩ Resistor
  • Arduino uno
  • jumper wire
  • Breadboard
Software
  • Arduino IDE

Step 2: Watch the video tutorial

Step 3: Schematics

 

The circuit is so simple:
Arduino Pins: Breadboard:
Pin A2 --------------------------1MΩ-------------------Gnd
Pin A2 ------------------------Diode-------------------Gnd
Pin A2 ------------------Piezo Buzzer---------------Gnd
Pin 8 -------------------------470Ω-------Led 1------Gnd
Gnd-------------------------------------------------------Gnd



Step 4: Arduino Sketch

"const int outputPin = 5; // led indicator connected to digital pin const int knockSensor = A2; // the piezo is connected to an analog pin const int thresholdHIGH =120; // threshold value to decide when the detected knock is hard (HIGH) const int thresholdLOW = 70; // threshold value to decide when the detected knock is gentle (LOW) const int secretKnockLength = 3; //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[secretKnockLength] = {0, 0, 1}; 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[secretCounter] == 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"); digitalWrite(outputPin, LOW); }//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[secretCounter] == 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"); }//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"); //if the sececret knock is correct, illuminate the LED for a couple seconds digitalWrite(outputPin, HIGH); //Reset the secret counter to 0. secretCounter = 0; }//close success check }//close loop".

 

Canale Telegram Leocity IOT

Ecco il link per il materiale delle lezioni : https://t.me/CorsoIOTDaVinci