LCD display . Scroll between 2 or more sets of readings

Hi,

I’ve set up an Arduino Uno to display the temperature and humidity from a DHT22 on a 2 line LCD display. (It works!)

If I were to add in a second (or more) DHT22 is there a way for me to scroll between the sets of readings using a push button?

Is there a tutorial or example of this?

Thanks

// DHT humidity/temperature sensor

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

#define DHTTYPE DHT22   // DHT 22

DHT dht(DHTPIN, DHTTYPE);

// LCD RS=3, EN=4, DS4=5, DS5=6, DS6=7, DS7=8
#include <LiquidCrystal.h>
LiquidCrystal lcd( 3, 4, 5, 6, 7, 8);

const int ledPin = 10;       // pin that the LED is attached to

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  dht.begin();
}

void loop() {

  int h = dht.readHumidity();
  int t = dht.readTemperature();

  // set the cursor to (0,0):
  lcd.setCursor(0, 0);
  // print from 0 to 9:

  lcd.print("Temperature:");
  lcd.print(t);
  lcd.print("'C");
  // set the cursor to (16,1):
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");

  if (h > 49)  {
    digitalWrite(ledPin, HIGH); Serial.println("Pump ON  ");
  }
  else if (h < 50)  {
    digitalWrite(ledPin, LOW); Serial.println("Pump OFF  ");
  }


  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C");

  delay(2000);
}

If you wish to scroll between to displays, you will have to write that code yourself.

. Read your button (wire it up one side to ground, the other to a pin and enable INPUT_PULLUP) Get that working. LOW = button pressed, HIGH = button not pressed.
. Look at the StateChangeDetection example in the IDE (File->examples->02.Digital->StateChangeDetection) to figure out how to know when your button changes state. You don't really care if it is currently being pushed or not, but you do care when it gets pushed.
. Create a new variable, maybe 'displayScreen' and whenever the button state changes from HIGH to LOW (a push), you toggle this variable from 0 to 1 or true/false
. Use this variable in an if() statement to determine which information you will display on the LCD each time through loop().

If you get stuck, post your code, your errors and people will help you.

Excellent, thanks.

A bit of reading up to do.

So, could I use a counter which increases by 1 for each time the button is pressed and use that number to display the information from a particular sensor?

If the button were to be pressed say 3 times could I display the information (humidity and temperature) from sensor number 3 and so on?

Cheers

Most definitely. And when your counter reaches the maximum, make sure it rolls over to 0.

counter == 0 -> display sensor 1
counter == 1 -> display sensor 2
counter == 2 -> display sensor 3
counter == 3 -> make counter go back to 0

blh64:
Most definitely. And when your counter reaches the maximum, make sure it rolls over to 0.

counter == 0 -> display sensor 1
counter == 1 -> display sensor 2
counter == 2 -> display sensor 3
counter == 3 -> make counter go back to 0

Or you can do as follows without making counter go back to 0
(counter % 3) == 0 -> display sensor 1
(counter % 3) == 1 -> display sensor 2
(counter % 3) == 2 -> display sensor 3

Ok, thanks.

I've got a bit of testing to do now...

Cheers

IoT_hobbyist:
Or you can do as follows without making counter go back to 0
(counter % 3) == 0 -> display sensor 1
(counter % 3) == 1 -> display sensor 2
(counter % 3) == 2 -> display sensor 3

but why would you want counter in increase indefinitely? At some point, it will roll over, depending on the type. A more universal method

counter = (counter+1) % 3;

will always keep it in the range of 0..2