Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#include <LiquidCrystal.

h> // includes the LiquidCrystal Library

LiquidCrystal lcd(22, 23, 24, 25, 26, 27); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 37;

const int echoPin = 36;

long duration;

int distanceCm ;

const int lectura = 35;

int pir = 0;

const int led = 7;

const int buzzer=8;

int sensor= 48;

int state ;

const int barrera = 45;

int bar2 = 0;

void setup()

lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of
the display

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode (lectura,INPUT);

pinMode (led,OUTPUT);

pinMode (buzzer,OUTPUT);

pinMode (sensor,INPUT_PULLUP);

pinMode (barrera,INPUT);
}

void loop() {

pir = digitalRead (lectura);

state = digitalRead (sensor);

bar2 = digitalRead (barrera);

delay (50);

switch (state)

case HIGH :

{digitalWrite (led,HIGH),

tone (buzzer,300,50),

lcd.setCursor(0,1),

lcd.print("alarma mag 15mts");

delay(50);

switch (pir)

case HIGH :
{digitalWrite (led,HIGH),

tone (buzzer,300,50),

lcd.setCursor(0,1),

lcd.print("alarma pir 5mts");

delay(50);

switch (bar2)

{case LOW :

{digitalWrite (led,HIGH),

tone (buzzer,300,50),

lcd.setCursor(0,1),

lcd.print("alarma barrer 10mts");

delay(50);

default: digitalWrite (led,LOW);

delay (20);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(2);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distanceCm= duration*0.034/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed

lcd.print("Distancia: "); // Prints string "Distance" on the LCD

lcd.print(distanceCm); // Prints the distance value from the sensor


lcd.print(" cm");

delay(50);

if (distanceCm==5){

digitalWrite (led,HIGH);

tone (buzzer,300,50);

lcd.setCursor(0,1);

lcd.print("intruso 5cm "); }

else if ( 9<distanceCm &&distanceCm<=10){

digitalWrite (led,HIGH);

tone (buzzer,300,70);

lcd.setCursor(0,1);

lcd.print("intruso 10cm ");}

else if (14<distanceCm && distanceCm<=15){

digitalWrite (led,HIGH);

tone (buzzer,300,100);

lcd.setCursor(0,1);

lcd.print("intruso 15cm ");}

else

lcd.setCursor(0,1);

lcd.print("intruso mas 15cm ");

You might also like