Danny van den Brande
Published © CC BY-SA

Mailbox Alert With the KY-031 Shock sensor

I made a Simple Example project for the KY-031. This time I made a simple mailbox alert.

BeginnerShowcase (no instructions)1 hour1,821
Mailbox Alert With the KY-031 Shock sensor

Things used in this project

Story

Read more

Schematics

Schematic

Code

KY-031_MAILBOX_ALERT.ino

Arduino
This is a example of what you can do with the shock sensor.
I made a simple mailbox alert project.
//Author: Danny van den Brande, arduinosensors.nl
//This is a example of what you can do with the shock sensor.
//I made a simple mailbox alert project.
//Offcourse this can be used to detect anything else that produces a shock.
int ShockSensor = 4;
int RedLed = 5;
int GreenLed = 6;
int Buzzer = 7;
int value = HIGH;
boolean Alarm = false;

unsigned long LatestKnockTime; 
int TriggerTime = 25; //You can change this if you want the alarm to trigger longer.

void setup ()
{
  Serial.begin(9600);  
  pinMode (ShockSensor, INPUT) ; //the sensor set to input
  pinMode (Buzzer, OUTPUT);
  pinMode (RedLed, OUTPUT);
  pinMode (GreenLed, OUTPUT);
}
void loop ()
{
  value = digitalRead (ShockSensor) ; // Reading value
  
  if (value == LOW) 
  {
  
    LatestKnockTime = millis(); // 
    if (!Alarm){
      Serial.println("Shock detected");// You can add a lcd and make it print on LCD
      digitalWrite (GreenLed, HIGH);
      digitalWrite (Buzzer, HIGH);
      delay(1000);
      digitalWrite (Buzzer, LOW);
      delay(1000);
      digitalWrite (Buzzer, HIGH);
      delay(1000);
      digitalWrite (Buzzer, LOW);
      delay(1000);
      digitalWrite (Buzzer, HIGH);
      delay(1000);
      digitalWrite (Buzzer, LOW);
      delay(1000);
      digitalWrite (Buzzer, HIGH);
      delay(1000);
      digitalWrite (Buzzer, LOW);
      delay(1000);
      Alarm = true;
    }
  }
  else
  {
    if( (millis()-LatestKnockTime) > TriggerTime  &&  Alarm){
      Serial.println("Nothing Detected");
      digitalWrite (Buzzer, LOW);
      Alarm = false;
    }
  }
}

Credits

Danny van den Brande
36 projects • 110 followers
"Hello world."

Comments