lcd controlled by push button

Hello everyone, in this code i'm using three push button to control my lcd each button is responsible for displaying sensors reading from the project. I came up with this code but the lcd is displaying mixed information and sometimes unknown letters, please help!!!

#include "DHT.h"
#define DHTPin 0
DHT dht;
#include<LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 3, 4, 5, 6);
const int trigPin = 7;
const int echoPin = 8;
const int TempRelay = 9;
//const int HumRelay = 10;
//const int HeatRelay = 11;
const int LampRelay = 12;
const int PumbRelay = 13;
const int KillSwitch_Rly = 14;
const int BuzrPin = 15;
int pb1=16;
int pb2=17;
int pb3=18;
const int ldrPin = A1;
const int SmokeSnsrPin = A2;
const int soil_sensor1 = A3;
const int soil_sensor2 = A4;
int state1=0;
int state2=0;
int state3=0;
long duration;
int distanceCm;
int distancePercent;
int safetyDistance = 18;
int soil1Status;
int soil2Status;
int SmokeLvl;
int TempSensitivity = 10;
int HumSensitivity = 50;
int soil1Percent;
int soil2Percent;
int smokePercent;
int ldrStatus;
int LDRSensitivity = 150;
int SmokeSensitivity = 420;
int SoilSensitivity = 700;
int SoilEnough = 350;
int temp;
int humi;
void setup() {
pinMode (LampRelay, OUTPUT);
pinMode (PumbRelay, OUTPUT);
pinMode (KillSwitch_Rly, OUTPUT);
pinMode (ldrPin, INPUT);
pinMode (SmokeSnsrPin, INPUT);
pinMode (BuzrPin, OUTPUT);
pinMode (TempRelay, OUTPUT);
//pinMode (HumRelay, OUTPUT);
//pinMode (HeatRelay, OUTPUT);
pinMode (soil_sensor1, INPUT);
pinMode (soil_sensor2, INPUT);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(pb1,INPUT);
pinMode(pb2,INPUT);
pinMode(pb3,INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();

}

void loop() {

state1=digitalRead(pb1);
state2=digitalRead(pb2);
state3=digitalRead(pb3);
if(state1==HIGH){

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration0.034/2;
distancePercent = duration
0.0133/2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dist: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
lcd.setCursor(0,1);
lcd.print("Percent: ");
lcd.print(distancePercent);
lcd.print(" %");
//delay(1000);

}

else if (state2==HIGH){

soil1Status = analogRead (soil_sensor1);
soil2Status = analogRead (soil_sensor2);
soil1Percent = (soil1Status * 0.0009766) * 100;
soil2Percent = (soil2Status * 0.0009766) * 100;
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("Soil M.1: ");// Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(soil1Percent); // Prints the soil mositure value from the sensor
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print ("Soil M.2: ");
lcd.print(soil2Percent);
lcd.print(" %");
//delay(1000);

}
else if (state3==HIGH){
float temp = dht.getTemperature();
float humi = dht.getHumidity();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Humi: ");
lcd.print(humi);
lcd.print(" %");

}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Green house");
delay(1000);

}

delay(500);
lcd.clear();
}

mohamedadawi:
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");

You are trying to print a float, I'm not sure how well the library likes that.
Try converting it into a Char first

do you mean to change float into char for displaying the the temp?

mohamedadawi:
do you mean to change float into char for displaying the the temp?

Convert the temp into a char and then print the char

I changed but still displaying mixed and unknown characters. do you think there is a problem with the delay ?

What Arduino board? Most Arduino boards use pins 0 and 1 for the serial port.
You are using pin 1 for the LCD.

--- bill

bperrybap:
What Arduino board? Most Arduino boards use pins 0 and 1 for the serial port.
You are using pin 1 for the LCD.

--- bill

i'm using Mega, and i think the pin number will not effect anything

It does, of course.

Did you check bperrybap's remark, or did you assume what you are stating in post #6 ?
bperrybap is a respected member who has shown he knows what he's talking about.
So you'd better check out his remarks before you toss them aside.

Check out what pins are in use on your board, and what functions those pins have.
You are using serial, and guess what that is connected to.

Never use pins 0 and 1 unless you are absolutely sure you know what you are doing, and also be sure it is necessary to do that.
If you want your Arduino to do what you expect it to do, that is.

You also seem to be using pin 13 for what looks like might eventually be for a relay:

const int PumbRelay          = 13;

Keep in mind that the bootloader blinks the LED during powerup and reset so anything that is attached to that pin will see the "blinks" as HIGH/LOW output transitions.

--- bill

MAS3:
It does, of course.

Did you check bperrybap's remark, or did you assume what you are stating in post #6 ?
bperrybap is a respected member who has shown he knows what he's talking about.
So you'd better check out his remarks before you toss them aside.

Check out what pins are in use on your board, and what functions those pins have.
You are using serial, and guess what that is connected to.

Never use pins 0 and 1 unless you are absolutely sure you know what you are doing, and also be sure it is necessary to do that.
If you want your Arduino to do what you expect it to do, that is.

do you mean i can't use pin 0 and 1 unless i'm receiving or sending data only and for pin 13 i can't use it also for digital write

If you don't use serial, you can use pins 0 and 1. So if you remove the Serial.begin() from setup, you're a step closer. But remember that serial is very useful for debugging.

You can use pin 13; however the bootloader also uses it so if you power up the Arduino, the relay might be activated/deactivated a couple of times before control is handed over to your application.

Hi.

I mean you need to know your device.
If you are going to use said pins, as well as others, you need to know about all of their functions.
Chips like these offer a great deal of interfaces.
There are more possible functions than there are pins.
So pins can be used in an either / or way.
You can either use serial, or use those pins for other functions, but you can't do both.
A MEGA has 4 serial ports, so be aware of all of them.

There are ways to have multiple functions of some pins (but not those serial pins).
Check out how SPI can be used for a lot of devices, where the bus itself occupies only 3 pins.

You can find out more by searching for "Arduino mega pinout"

MAS3:
Hi.

I mean you need to know your device.
If you are going to use said pins, as well as others, you need to know about all of their functions.
Chips like these offer a great deal of interfaces.
There are more possible functions than there are pins.
So pins can be used in an either / or way.
You can either use serial, or use those pins for other functions, but you can't do both.
A MEGA has 4 serial ports, so be aware of all of them.

There are ways to have multiple functions of some pins (but not those serial pins).
Check out how SPI can be used for a lot of devices, where the bus itself occupies only 3 pins.

You can find out more by searching for "Arduino mega pinout"

Serial: 0 (RX) and 1 (TX);
Serial 1: 19 (RX) and 18 (TX);
Serial 2: 17 (RX) and 16 (TX);
Serial 3: 15 (RX) and 14 (TX)
Those pins are for serial connection in Mega which means if i'm using serial begin i can't use them as digital write or read ?

If you use Serial.begin, you can't use 0 and 1, but use the others.

If you use Serial1.begin, you can't use 19 and 18, but use the others.

Remember that pins 0 and 1 are also use for communication with the PC on e.g. the Uno, Nano and the Mega.

Hopefully it works, but i have another problem with the relays whenever they turns on the lcd becomes dim although i'm using relay shield module.
need help

You should not feed the relays from the Arduino 5V pin; they draw too much power. You need a separate 5V power supply for the relays.

Power your module from an external source.
They usually allow you to either use the Arduino's 5 volt output or an external 5 volt source.

The Arduino's 5 volt output is quite limited, so if you have more than just a few parts to power, you should use separate power supplies.

As advertised on each and every of my posts, do connect all GNDs to each other.

If you need more help, point to (a link of) the exact relay module/shield you are using, there's a lot of them around.

(sorry sterretje, didn't notice page 2)

mohamedadawi:
Hopefully it works, but i have another problem with the relays whenever they turns on the lcd becomes dim although i'm using relay shield module.
need help

sounds like a power issue, how much current is the arduino getting from the usb port (or power supply if powered externally)?
If it uses more than 500mA, then you have your answer as in why the screen dims.