Digital Tape Measure NOT WORKING! please help

#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.

So this code is meant to display

And what does it do?

What is an "AP CS"?

Try debouncing the button, Put a delay after

  lcd.setCursor(0, 0);
    lcd.write(inches + "in away");
    lcd.setCursor(0, 1);
    lcd.write("from object!")

    Serial.println("button pressed")

Firstly,

This is my final for AP CS and it's not working

What is the output, What is happening right now.

There could be a problem with your ultrasonic sensor, We'll talk about the button later, Are you sure your ultrasonic sensor is working?

Secondly Is your LCD display working? Check that separately. Learn to comment your code.

Okay, thank you for the help, yes the ultra sonic sensor is working properly and the lcd panel does display things, i.e. if I wrote lcd.write("test"); it would display test! so they both work. When i press the button the serial monitor just stops... the planel turns off and then turns on and the sensor stops reading numbers... Not sure what to do, not sure how to fix... AP CS is advanced placement computer science... its a class.

akshaykirti:
Try debouncing the button, Put a delay after

  lcd.setCursor(0, 0);

lcd.write(inches + "in away");
    lcd.setCursor(0, 1);
    lcd.write("from object!")

Serial.println("button pressed")

how do I do this...?

Not sure what ISRs have to do with this.

@OP How is the momentary switch wired up?

That ISR was for another post. Sorry, deleted it

I have a feeling that if the board is resetting and everything stops, you may be shorting someting

Yeah, How have you connected the button.

I think when you press the button you connect ground and source and hence bad things happen

The switch should have a pull down resistor connected to it. This is how you do it...

The first one is a pull down. A pull down as your default value is LOW

Note that you don't really need that 100 ohm resistor. The 10k one is important

There is a modify button you can press instead of making a new post every time you have a new thought.

It looks to me from the photos that you have the button connected across the power rails:

  • Button red wire is connected to the ground bus on the breadboard
  • Button black wire is connected to a row on the breadboard
  • White wire connects same breadboard row to +5V bus on breadboard

btw it's a good idea to colour-code your wires, e.g. use black for ground, red for +5V, and other colours for signal wires.

dc42:
It looks to me from the photos that you have the button connected across the power rails:

  • Button red wire is connected to the ground bus on the breadboard
  • Button black wire is connected to a row on the breadboard
  • White wire connects same breadboard row to +5V bus on breadboard

btw it's a good idea to colour-code your wires, e.g. use black for ground, red for +5V, and other colours for signal wires.

very true haha sadly I was limited colors... ANYWAY!!! to everyone, I will fix the button circuitry when I get into school in the morning. BUT! does the code look correct?? becayse that'd be awesome.

Yup. The code does seen correct. If it seems to give you trouble, add a delay for 100ms or so after the message Serial.println("button pressed");

Thank you everyone so much!

BOOM works! biggest problem was i was trying to write! an int!!!! I needed to print it. thank you!