Trouble with my code

#include <LiquidCrystal.h>

const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int trigPin=10;
const int echogPin=9;
int buttonpin=8;
int buttonState= 0;
int flag=0;
long duration, distance;
void setup() {
 
  pinMode(buttonpin,INPUT);
  lcd.begin(16, 2);
  pinMode(trigPin,OUTPUT);
  pinMode(echogPin,INPUT);
  
}

void loop() {
 digitalWrite(trigPin,LOW);

 delayMicroseconds(2);
digitalWrite(trigPin,LOW);
 delayMicroseconds(10);
 digitalWrite(trigPin,HIGH);
 duration = pulseIn(echogPin,HIGH);
 distance = duration/57.8; 
 buttonState=digitalRead(buttonpin);
  if(buttonState==LOW){
    if (flag==0)
    {
      lcd.setCursor(0,0);
      lcd.print("Press 4 Distance");
      delay(100);
    flag=1;
    } else if(flag==1){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Press 4 Distance");
      lcd.setCursor(0,1);
      lcd.print(distance);
      lcd.print("cm");
    }
  }  
}

the lcd wasn't suppose to turn on and start working unless i press the button, can you point out the reason?

Your topic has been moved to the Programming category of the forum as it is not a problem with the IDE itself

Is there anything keeping the button pin in a known state when the button is not pressed ?

pinMode(buttonpin,INPUT_PULLUP);

i hooked up a resistor to the button so this should not be a problem right?

How exactly are the button and resistor wired ?

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

fixed it, turn out i got a bunch of faulty resistors and the circuit was not close

tried about 25 resistors until i got a functional one

not the solution but it is a wrong sequence

why not configure the pin as INPUT_PULLUP to use the internal pullup resistor?

i fixed everything, thank you. i move on to my next assignment and have another problem, this time my lcd cant display my measument from the dht 11 temp&humidity sensor. here is the schematic


and here is my code

#include "DHT.h"  
#include "LiquidCrystal.h" 
const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int DHTPIN = 3;      
const int DHTTYPE = DHT11;  

DHT dht(DHTPIN, DHTTYPE);
void setup() {

  dht.begin(); 
  lcd.begin(16,2);
        
}

void loop() {
 int h = dht.readHumidity();    
  int t = dht.readTemperature(); 
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.setCursor(12,0);
lcd.print(t);

lcd.setCursor(14,0);
lcd.print((char)223);
lcd.setCursor(15,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.setCursor(12,1);
lcd.print(h);
lcd.setCursor(14,1);
lcd.print("%");         
  delay(100);                     
}

the schematic does not have dht 11 sensor cause tinkercad does not have it, but im sure that i have correctly hooked up the sensor cause i tried a simple code like hello world and the lcd still does not display anything

Think about what you've just said. This is the equivalent of "I know I'm not speeding because my car isn't pink". Whether the sensor is connected correctly has no bearing on the functioning of the LCD.

So the display is blank? Or the value it shows doesn't make sense? Please be specific, it's essential when describing problems.

the display is blank, first i thought there was a problem with my code, so i tried using a simple Hello World to test the display and it still did not work

Stick with the "Hello, World" code until you get it displayed, don't worry about the sensor(but remove it, in case you did make a mistake there).
There are many types of 16x2 LCD out there, so it's possible the one you have isn't happy with the way the LCD library is handling it. In a case like that, I'd go with Bill Perry's exhaustive hd44780 LCD driver, it's your best chance for success. It's in the 'manage libraries' selection in your IDE. Type HD44780 in the search box, and scrolldown in the results until you find it. You may need to modify your calls to be successful, so message back here if you're having issues.

Hi @strugglingwithlife ,

put this project into a simulator..
seems to work, maybe check your connections??

Uno LCD Temp..

good luck.. ~q

Did you connect LCD backlight power?
Did you try to adjust a LCD display contrast via pot ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.