0% found this document useful (0 votes)
68 views2 pages

IOT Assignment 2

This document contains code for an Arduino project that measures distance using an ultrasonic sensor and displays it on an LCD screen. The code initializes the LCD screen, sets pin assignments for the ultrasonic sensor, takes distance measurements in a loop, prints the results to the serial monitor and LCD screen, and displays an out of range message for distances over 300cm.

Uploaded by

Satadeep Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

IOT Assignment 2

This document contains code for an Arduino project that measures distance using an ultrasonic sensor and displays it on an LCD screen. The code initializes the LCD screen, sets pin assignments for the ultrasonic sensor, takes distance measurements in a loop, prints the results to the serial monitor and LCD screen, and displays an out of range message for distances over 300cm.

Uploaded by

Satadeep Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Project Source Code

##
//Program to
#include <LiquidCrystal.h
LiquidCrystal lcd(2, 3, 4, 5, 6, 7)
int sw = 10
int outputPin = 9; // set you output pi
int inputPin = 8; // set your input pi
long distance
long cm
void setup()
Serial.begin(9600); // set your baud rat
pinMode(outputPin, OUTPUT)
pinMode(inputPin,INPUT)
pinMode(sw, OUTPUT)
lcd.begin(16, 2)
lcd.setCursor(0, 0)
lcd.print("ENGINEERS GARAGE")
lcd.setCursor(0, 2)
lcd.print("Distance=")

void loop(

int SW=digitalRead(sw)
digitalWrite(outputPin, LOW)
delayMicroseconds(2)
digitalWrite(outputPin, HIGH)
delayMicroseconds(10)
digitalWrite(outputPin, LOW);
distance = pulseIn(inputPin, HIGH); //it now initalizes before
setup to speed up the proces
cm= distance/58; //I don't like reusing the same variable
name, it probably works fine though,
//but i don't really trust it,
besides you have plenty of memory for it
if (SW == HIGH

if (cm <= 200 || cm >= 0

Serial.println(cm);
delay(500)
lcd.setCursor(10, 2)
lcd.print(cm)
lcd.print(" cm")
delay(100)
}

>

if (cm > 300

lcd.setCursor(10, 2)
// lcd.print(cm+1)
lcd.print("O of R "); // out of rang
delay(100)

Serial.println(cm);
delay(500); // this will slow down your readings to 2 per second
it should help when watching the Serial termina
// if this helps then start to shrink it down to 100 then 10 then
1,
maybe even go to microsecond delays or remove completely if it
stops freezin

##

Circuit Diagram
}

You might also like