Hello
I am working on a project for creating a car parking sensor. I have set up a project in which the LCD will detect the distance between the object and the sensor and print it on an LCD. However when an object is too close (less than 2cm), the LCD will print random numbers above 3000cm. This also happens when the object is out of range.
I have posted my code below, can anyone help me amend this.
Any help will be much appreciated ![]()
#include <NewPing.h>
#include <LiquidCrystal.h>
LiquidCrystal LCD(12, 11, 5, 4, 3, 2); //create lcd object
#define trigPin 7 //Sensor Trip to pin 7
#define echoPin 6 //sensor echo to pin 6
int maximumRange = 20; //Maximum range needed
int minimumRange = 0; //Minimum range needed
long duration, distance;
byte customChar[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2);// start 16 column 2 row
LCD.setCursor(0,0); // LCD cursor to top left
LCD.print("Distance:"); //Print message to first row
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW); //set triger pin low
delayMicroseconds(2); // let signal settle
digitalWrite(trigPin, HIGH);//set trigpin high
delayMicroseconds(15); // delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); // Delay in high state
duration = pulseIn(echoPin, HIGH); //duration in microseconds
distance = duration/58.2;
Serial.print (distance);
LCD.setCursor(10,0); //set cursor to first column of second row
LCD.print(" "); //prints blanks to clear the row
LCD.setCursor(10,0); //Set Cursor again to first column of second row
LCD.print(distance); //print distance
LCD.print("cm");// units
delay(250); //pause
if (distance<64)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(0, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(0,1);
LCD.print(" ");
}
if (distance<60)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(1, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(1,1);
LCD.print(" ");
}
if (distance<56)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(2, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(2,1);
LCD.print(" ");
}
if (distance<52)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(3, 1);
// print the custom char to the lcd
// why typecast? see: http://arduino.cc/forum/index.php?topic=74666.0
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(3,1);
LCD.print(" ");
}
if (distance<48)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(4, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(4,1);
LCD.print(" ");
}
if (distance<44)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(5, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(5,1);
LCD.print(" ");
}
if (distance<40)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(6, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(6,1);
LCD.print(" ");
}
if (distance<36)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(7, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(7,1);
LCD.print(" ");
}
if (distance<32)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(8, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(8,1);
LCD.print(" ");
}
if (distance<28)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(9, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(9,1);
LCD.print(" ");
}
if (distance<24)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(10, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(10,1);
LCD.print(" ");
}
if (distance<20)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(11, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(11,1);
LCD.print(" ");
}
if (distance<16)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(12, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(12,1);
LCD.print(" ");
}
if (distance<12)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(13, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(13,1);
LCD.print(" ");
}
if (distance<8)
{
// create a new custom character
LCD.createChar(0, customChar);
// set up number of columns and rows
LCD.setCursor(14, 1);
// print the custom char to the lcd
LCD.write((uint8_t)0);
}
else
{
LCD.setCursor(14,1);
LCD.print(" ");
}
}