#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#include <Servo.h>
const int inputPin = 2;
int pirState = LOW;
int val = 0;
int pinSpeaker = 3;
int angle = 0;
Servo servo;
void setup() {
lcd.begin(16,2);
pinMode(inputPin, INPUT);
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
servo.attach(5);
// put your setup code here, to run once:
}
void loop() {
val = digitalRead(inputPin);
Serial.println(val);
if (val == HIGH) {
lcd.print("motion detected");
for (angle = 0; angle < 180; angle++) {
servo.write(angle);
digitalWrite(pinSpeaker, HIGH);
Playtone(50, 150);
delay(50);
}
}
else {
lcd.clear();
digitalWrite(pinSpeaker, LOW);
servo.write(90);
}
delay(450);
// put your main code here, to run repeatedly:
}
void Playtone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker, HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}