Code isnt playing nice

Hey Guys,

First time poster, and new to Arduino generally speaking.

So for a fun project I decided to make a distance measurer using HC-SR04 with my UNO.

So using a Serial Monitor I have been able to get my code to work, so decided to try an better this by incorporating an LCD...

To my dismay I have not been able to get the code to function for it to display my results i.e. distance in CM onto the LCD...

Could anyone with knowledge on this please help me... Code below.

#include <NewPing.h>
#include <LiquidCrystal.h>

#define PIN_ECHO 4
#define PIN_TRIG 5
#define MAX_DIST 500

NewPing sensor(PIN_TRIG, PIN_ECHO, MAX_DIST);

LiquidCrystal lcd(8,9,2,3,6,7);
int BackLitPin = 10;

void setup()
{
Serial.begin(9600);

lcd.begin(16, 2);

pinMode(BackLitPin,OUTPUT); //(Old) Back light Pin
digitalWrite(BackLitPin,HIGH); //(Old) Turn Back light on

}

void loop()
{
delay(50);
int time_taken=sensor.ping();
Serial.println(time_taken/US_ROUNDTRIP_CM);

lcd.setCursor(1,0); // Set where it starts to write
lcd.print(time_taken/US_ROUNDTRIP_CM);
lcd.print(" cm "); // add cm after the distance
}

try adjusting the display contrast potentiometer.

if you're getting nothing at all on the LCD, and you have one of the shields that has left,right... buttons on it, try this constructor:

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

wg0z! Thank you sir.

Not sure why, but changed the pins for the LCD to the once suggested and changed the ECHO and TRIGG to pins 12 and 13 and now getting the reading and CM displayed on the screen :slight_smile:

Didn't immediately realise it but I tried changing to two other unused pins but they were PWM pins which for some reason don't work for the ECHO and TRIGG to display on the LCD. (assuming its because they aren't digital pins?)

Thanks again chap!

(assuming its because they aren't digital pins?)

All PWM pins are digital pins with additional behavior added.