Bottom to start code sequence

Hi guys,

I’m wondering how to implement a push button to start the code sequence of a temperature and humidity sensor displayed on an i2c lcd. The code is open source and shown below. The code is running it’s own loop - with the bottom i was thinking of making it so that when the bottom is pressed the display should light up as well as start displaying the respective temperature and humidity, and after a certain amount of time the display would shutoff as well as the temperature and humidity readings.

Thanks in advance!

//Libraries
#include <DHT.h>
#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

//Constants
#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//ALWAYS USE THIS WITH LCD I2C and Addres 0x3F
#define I2C_ADDR 0x3F
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value
const int buttonPin = 2;
int buttonState = 1;

void setup()
{
  Serial.begin(9600);
  dht.begin();
  lcd.begin(16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
}

void loop()

{
  //Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print(" ");
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Hum:  ");
  lcd.print(hum);
  lcd.print(" %");

  delay(2000); //Delay 2 sec.
}

I'm wondering how to implement a bottom to start the code

A bottom? Do you mean a button (or, more properly, a switch)?

Im referring to a push bottom - as seen in this link: https://www.arduino.cc/en/Tutorial/Button

Have the code you want to control only run when a boolean variable is true.

Set the boolean to true when the button becomes pressed
Set the boolean to false to stop the code running

Get rid of the delay() and use millis() for timing
See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Qusse:
Im referring to a push bottom - as seen in this link: https://www.arduino.cc/en/Tutorial/Button

There are no push bottoms on that page.

PaulS:
There are no push bottoms on that page.

You seem to be quite ignorant? Can't take a misspelling.

Qusse:
You seem to be quite ignorant? Can't take a misspelling.

The compiler can't, either. If you can't spell, you are going to make a lousy programmer.

If you can't use proper punctuation, you are going to make a lousy programmer.

Oh give me a break, dude grab a snickers, you turn into a diva when you're hungry..

In a world where syntax is the holy grail, mis-spelled words are the source of 84% of programming errors.

mis-spelled words are the source of 84% of programming errors

and 72% of all statistics are made up :slight_smile:

And you can end up in jail pressing the wrong bottom. :slight_smile: