#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define PIN_TRIG 3
#define PIN_ECHO 2
const int led = 4;
const int tonepin = 5;
// #define pirout 6
// int pirState = LOW;
// int val = 0;
void setup() {
lcd.begin(16, 2);
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(led, OUTPUT);
// pinMode(pirout, INPUT);
lcd.print("Hello World!");
}
void loop() {
// you can now interact with the LCD, e.g.:
delay(2000);
lcd.clear();
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
int distancecm = duration / 58;
int distancein = duration / 148;
Serial.print("Distance in CM: ");
Serial.println(distancecm);
Serial.print("Distance in inches: ");
Serial.println(distancein);
lcd.print("Distance(cm):");
lcd.setCursor(0, 2);
lcd.print(distancecm);
if (distancecm <= 20) {
tone(tonepin, 262, 250); // Plays 262Hz tone for 0.250 seconds
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
tone(tonepin, 262, 250); // Plays 262Hz tone for 0.250 seconds
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(1000);
}
// val = digitalRead(pirout); // read input value
// if (val == HIGH) { // check if the input is HIGH
// digitalWrite(led, HIGH); // turn LED ON
// if (pirState == LOW) {
// // we have just turned on
// Serial.println("Motion detected!");
// tone(tonepin, 262, 250);
// delay(1000);
// tone(tonepin, 262, 250);
// // We only want to print on the output change, not state
// pirState = HIGH;
// }
// } else {
// digitalWrite(led, LOW); // turn LED OFF
// if (pirState == HIGH) {
// // we have just turned of
// Serial.println("Motion ended!");
// // We only want to print on the output change, not state
// pirState = LOW;
// }
// }
}