My goal is to have my LCD display a face and once you get close to it a random message will be displayed for a few seconds, then it will go back to the face. I have the LCD and the sonar both working separately, but when I put them together my LCD gives me some funky output. I'm new to programming hardware any help would be awesome!
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
#define echoPin 13
#define trigPin 10
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop()
{
//Make a scan to see if anything is close enough
int distance = getDistance();
//get a random number this will be used for our random message
int randomNum = random(10);
//print our main face for 10 seconds
mainDisplay();
delay(10000);
//If our distance was less than 15 cm away
//clear the screen and wait 2 seconds
//print our random message based off our random number for 2 seconds
//otherwise clear the screen and wait 2 seconds
if (distance < 15)
{
lcd.clear();
delay(2000);
if (randomNum < 5)
{
lcd.print("< 5");
delay(2000);
lcd.clear();
lcd.blink();
delay(1000);
}
else
{
lcd.print("> 5");
delay(2000);
lcd.clear();
lcd.blink();
delay(10000);
}
}
else
{
delay(2000);
lcd.clear();
lcd.blink();
delay(10000);
}
//loop back and print our face again
}
void mainDisplay()
{
//print our main face
lcd.home();
lcd.print(" * .. * ");
lcd.setCursor(2, 1);
lcd.print("____________ ");
}
//check the distance of the nearest target and return the value in cm
int getDistance()
{
long distance;
long duration;
//bool check;
//check = false;
for (int i = 0; i < 1; i++)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2;
Serial.print(distance);
Serial.print('\n');
}
return distance;
}