#include <LiquidCrystal.h>
const int trigPin = 8;
const int echoPin = 9;
const int buttonPin = 7;
//const int ledPin = 6;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonState = 0;
void setup() {
Serial.begin(9600);
//pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
lcd.print("Press button");
lcd.setCursor(0, 1);
lcd.print("for distance");
}
void loop()
{
long duration, inches;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
//Serial.print(inches);
//Serial.println("in");
delay(100);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
lcd.setCursor(0, 0);
lcd.write(inches + "in away");
lcd.setCursor(0, 1);
lcd.write("from object!")
Serial.println("button pressed");
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
So this code is meant to display "press button for distance" on the 1602 LCD display until the button is pressed and when the button is pressed it will display the instant serial display which is the inches away from the ultra sonic 4-pin sensor... The idea is pretty much a digital tap measure that displays the distance when the button is pressed and displays "press button for distance" when the button isn't pressed. This is my final for AP CS and it's not working! Someone please try and help or suggest something!!!! Thank you!
This picture below is my circuitry.