Arduino MAILBOX ALERT KY-031 Shock sensor.

Hello world! I made a Simple Example project for the KY-031
In this topic i made a simple Arduino MAILBOX ALERT.
Of course you can use this code for something else that produces shocks that you want to be notified about.

Schematic:
Arduino MAILBOX ALERT

Parts list:
Arduino Uno, I used the RobotDyn Uno.. A chinese copy with 7 analog pins. and a micro usb.
KY-031 Shock sensor.
KY-016 Led. or any other led.
Buzzer
Set of Jumper Wires MM / FF / FM.

Code:

//Author: Danny van den Brande, arduinosensors.nl
//This is a example of what you can do with the shock sensor.
//I made a simple Arduino 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;
    }
  }
}
Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *