Interactive Door Alarm with Arduino

This sketch makes an amazing door alarm. It gives the person who opens the door few seconds and tell that person to close it. If the person closes the door then it says thank you otherwise it starts counting down to 1. It the person closes the door before it says 1, it will not turn on the alarm but when it says one that's it, then it will turn on the alarm and will not stop until you manually stop it.

If you are clever enough you can find out by the sketch, and if you want know why I didn't explain in detail then  read more...
Ad

The sketch

#include <SoftwareSerial.h>
#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6 //connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22

int trigPin1 = 8;
int echoPin1 = 9;

void setup()
{
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
      mySerial.begin(9600);
        Serial.begin(9600);
      delay(500);//Wait chip initialization is complete
        sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
      delay(200);//wait for 200ms
}

void sensor(){
int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

      Serial.print("1st Sensor: ");
      Serial.print(distance1);
      Serial.print("cm  ");

  if (distance1 >=7) {  // Change the number for long or short distances.
 sendCommand(CMD_PLAY_W_VOL, 0X0F04);//play the second song with volume 30 class
   delay(4000);
   comma();
  } else {
delay(100);
sensor();
  }
}

void comma(){
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >=7) {
 sendCommand(CMD_PLAY_W_VOL, 0X0F05);
   delay(7000);
 
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >=7) {
   commb();
  } else {
sendCommand(CMD_PLAY_W_VOL, 0X0F07);
  }
 
  } else {
sendCommand(CMD_PLAY_W_VOL, 0X0F07);
  } 
}


void commb(){
  sendCommand(CMD_PLAY_W_VOL, 0X0F03);
  delay(2000);
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >=7) {

  sendCommand(CMD_PLAY_W_VOL, 0X0F02);
  delay(2000);
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >=7) {
   sendCommand(CMD_PLAY_W_VOL, 0X0F01);
  delay(2000);
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >=7) {
sendCommand(CMD_PLAY_W_VOL, 0X0F06);
delay(2700);
    commc();
  } else {
sendCommand(CMD_PLAY_W_VOL, 0X0F07);
  }
     
  } else {
sendCommand(CMD_PLAY_W_VOL, 0X0F07);
  }
  
  } else {
sendCommand(CMD_PLAY_W_VOL, 0X0F07);
  }
}

void commc(){
sendCommand(CMD_PLAY_W_VOL, 0X0F08);
delay(400);
commc();
}


void loop() {
delay(100);
Serial.println("\n");
sensor();
}

void sendCommand(int8_t command, int16_t dat)
{
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//
  {
    mySerial.write(Send_buf[i]) ;
  }

}

Ad


No comments:

Post a Comment



Newly posted:
Reason why rich getting richer and poor getting poorer