Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lpdc

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

/*

/////////Lpdc/////////

* All these following Libraries can be downloaded here as .Zip-File:

https://arduino-info.wikispaces.com

*/

#include <Wire.h>

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

#define trigPin 12

#define echoPin 10

#define max_dist 400

//Parameter below refer to: Addr, En, Rw, Rs, d4, d5, d6, d7

//Most displays use I2C Adress 0x27 but a FEW (Including those labelled "MH" ) use 0x3F (Need to be
changed in code).

//Displays backpack interface labelled "A0 A1 A2".

LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x3F I2C bus address for an unmodified backpack

float duration;

int distance;

void setup() {

/*

* Activate LCD module:


* for 16 character(colums) x 2 line LCD module => I2C LCD DISPLAY VERSION 1.

*/

lcd.begin (16,2); //Activation

lcd.setBacklightPin(3,POSITIVE); // Blacklightpin & Polarity Setup

lcd.setBacklight(HIGH); // Activate Blacklight

/*initialize serial port*/

Serial.begin(9600);

/*Initialize PIN Modes*/

pinMode(trigPin,OUTPUT);

pinMode(echoPin,INPUT);

void loop() {

/////Write the pulse to the HC-SR04 Trigger Pin/////

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

///Measure the response from the HC-SR04 Echo Pin///

duration = pulseIn(echoPin, HIGH);


// Use 340 metres per seconds as speed of sound

distance = (duration / 2) * 0.0340;

//Send results to LCD

Serial.println(distance);//TEST!

if(distance >=max_dist || distance <=2){

lcd.clear();// important to clear LCD after display

lcd.setCursor(0,0); // Set cursor to the begin of 1st line, also possible with lcd.home()

lcd.print("Distance"); // Print first line

lcd.setCursor(0,1);// Set cursor to the begin of 2nd line

lcd.print("Out of range"); // Print first line

}else{

lcd.clear();

lcd.setCursor(0,0); // Set cursor to the begin of 1st line, also possible with lcd.home()

lcd.print("Distance to Obj."); // Print first line

lcd.setCursor(0,1);// Set cursor to the begin of 2nd line

lcd.print(distance); // Print first line

lcd.print(" cm");

delay(100);// Wait 1/4 s for display

delay(100);///Wait to Measure the response from the HC-SR04 Echo Pin

You might also like