Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h> //Link of library: https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c
- #define BUZZER 5 //BUZZER
- int trigPin = 11; // Trigger
- int echoPin = 12; // Echo
- long duration, cm, inches;
- //Define LCD pinout
- const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
- //Define I2C Address - change if reqiuired
- const int i2c_addr = 0x27;
- // Define LCD display connections
- LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
- void setup() {
- //Serial Port begin
- //Serial.begin (9600);
- //Define inputs and outputs
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- noTone(BUZZER);
- // Set display type as 20 char, 4 rows
- lcd.begin(20,4);
- // Print a message to the LCD.
- lcd.setCursor(0,0);
- lcd.print("Welcome to");
- lcd.setCursor(0,1);
- lcd.print("Ultra Sonic Sensor");
- lcd.setCursor(0,2);
- lcd.print("Distance");
- lcd.setCursor(0,3);
- lcd.print("Mesurement");
- delay(3000);
- lcd.clear();
- }
- void loop() {
- // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
- // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
- digitalWrite(trigPin, LOW);
- delayMicroseconds(5);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Read the signal from the sensor: a HIGH pulse whose
- // duration is the time (in microseconds) from the sending
- // of the ping to the reception of its echo off of an object.
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- // Convert the time into a distance
- //char cm[],inches[];
- cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
- inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
- char bufferIn[100];
- char bufferCm[100];
- Serial.print(inches);
- Serial.print("in, ");
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- delay(250);
- lcd.setCursor(0,0);
- sprintf(bufferIn, "Distance: %d in.", inches);
- lcd.print(bufferIn);
- lcd.setCursor(0,1);
- sprintf(bufferCm, "Distance: %d cm.", cm);
- lcd.print(bufferCm);
- //lcd.setCursor(0,1);
- //lcd.print("Centimeter: %d",cm);
- //lcd.setCursor(0,2);
- //lcd.print("A-Maghulog ng barya");
- //lcd.setCursor(0,3);
- //lcd.print("B-Pumili ng Port.");
- delay(100);
- if (inches <= 54 && inches >= 33) {
- lcd.setCursor(0,3);
- sprintf(bufferIn, "Distance is LT 32in.", inches);
- lcd.print(bufferIn);
- delay(100);
- for(int i = 0; i < 5; i++)
- //for(;;)
- {
- //lcd.backlight();
- tone(BUZZER,1000);
- delay(150);
- //lcd.noBacklight();
- noTone(BUZZER);
- delay(100);
- }
- }else if (inches <= 32 && inches >= 13) {
- lcd.setCursor(0,3);
- sprintf(bufferIn, "Distance is LT 24in.", inches);
- lcd.print(bufferIn);
- delay(250);
- for(int i = 0; i < 5; i++)
- //for(;;)
- {
- //lcd.backlight();
- tone(BUZZER,500);
- delay(100);
- //lcd.noBacklight();
- noTone(BUZZER);
- delay(100);
- }
- }else if (inches <= 12) {
- lcd.setCursor(0,3);
- sprintf(bufferIn, "Distance is LT 24in.", inches);
- lcd.print(bufferIn);
- delay(100);
- tone(BUZZER,2000);
- delay(10);
- }else{
- lcd.backlight();
- lcd.setCursor(0,3);
- sprintf(bufferIn, "Distance is GT 21in.", inches);
- lcd.print(bufferIn);
- delay(100);
- noTone(BUZZER);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement