Rabu, 02 November 2016

KY-031 Knock Impact Sensor Module


KY-031 Knock Impact Sensor Module

KY-031 Knock Sensor PinOuts
KY-031 Knock Sensor Module Schematic
KY-031 Connection to the Arduino
Arduino Sketch
int knockPin = 10; // Use Pin 10 as our Input
int knockLED = 13;
int knockVal = HIGH; // This is where we record our shock measurement
boolean bAlarm = false;unsigned long lastKnockTime; // Record the time that we measured a shock

int knockAlarmTime = 500; // Number of milli seconds to keep the knock alarm high

void setup ()
{
  
Serial.begin(9600); 
  
pinMode (knockPin, INPUT); // input from the KY-031
    pinMode (knockLED, OUTPUT);
}

void loop ()
{
  knockVal = 
digitalRead (knockPin); // read KY-031 Value

  
if (knockVal == LOW) // If we see a knock
  {

    lastKnockTime = 
millis(); // record the time of the shock
    // The following is so you don't scroll on the output screen
    if (!bAlarm)
    {
      
Serial.println("KNOCK, KNOCK");
      digitalWrite (knockLED, HIGH);
      bAlarm = 
true;
    }
  }
  
else
  {
    
if( (millis()-lastKnockTime) > knockAlarmTime  &&  bAlarm)
    {
      
Serial.println("no knocks");
      digitalWrite (knockLED, LOW);
      bAlarm = 
false;
    }
  }
}


Sumber
Keyes KY-031 Arduino Knock Impact Sensor: Manual and Tutorial

Where to Buy
KY-031 Percussion Knock Impact Sensor Module

KY-031 Percussion Knock Impact Sensor Module

Tidak ada komentar:

Posting Komentar