Code for distance measurement system

I have a doubt for a code for distance measurement system if u find any mistakes in this code pls tell or rectify them by giving the correct code

<#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define trigPin A0 //Sensor Trig pin connected to Arduino pin A0

#define echoPin A1 //Sensor Echo pin connected to Arduino pin A1

long distanceInch;

void setup()

{

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

lcd.init();

lcd.backlight();

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Simple Circuits");

delay(2000);

lcd.clear();

lcd.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0

lcd.print("Distance:");//Print Message on First Row

lcd.setCursor(0,1);

lcd.print("Distance:");

}

void loop()

{

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

distanceInch = duration*0.0133/2;

lcd.setCursor(9,0);

lcd.print(" ");

lcd.setCursor(9,0);

lcd.print( distance); //Print measured distance

lcd.print(" cm"); //Print your units.

lcd.setCursor(9,1); //

lcd.print(" "); //Print blanks to clear the row

lcd.setCursor(9,1);

lcd.print(distanceInch);

lcd.print(" inch"); //Print your units.

delay(200); //pause to let things settle

}/>

  1. "You" is spelled "you."
  2. You should start your own topic. Asking your question in someone else's topic is called "hijacking" and is not good.
  3. When posting code, use the <CODE> button and paste your code where directed.


4. distanceInch = duration * 0.0133 / 2; will be "0" if duration is "2" or less... and zero does not equal two.

Please don't hijack forum topics @techguru-88. I have split your post out to its own topic.

Hijacking is against the Arduino Forum rules. The reason is that it may sidetrack the discussion, even preventing the creator of the topic from getting the assistance they need. It also reduces your own chances of getting assistance.

This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

  • Where did you get this code ?

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.