Back to Parent

/* Knock Sensor
   http://www.arduino.cc/en/Tutorial/Knock
   
   created 25 Mar 2007
   by David Cuartielles <http://www.0j0.org>
   modified 30 Aug 2011
   by Tom Igoe
   This example code is in the public domain.

 */
 
// these constants won't change:
const int ledPin = 11;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 50;  // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int timer = 0;

void setup() {
 pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
 Serial.begin(9600);       // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);    
  Serial.println(analogRead(sensorReading));
  
  // if the sensor reading is greater than the threshold:
  if (sensorReading <= threshold) {
    //timer initiate
    timer = 800; //800
    // update the LED pin itself:        
    analogWrite(ledPin, 100);
    // send the string "Knock!" back to the computer, followed by newline
  }
  
  if (timer < 1){
   // update the LED pin itself:        
    analogWrite(ledPin, 0); 
    }

  timer = timer-1;
  if (timer <= 0)
  {timer = 0;
  }
  delay(10);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0