how to put a button statement in a button statement

Hello All,

I am a newbie to Arduino, and I’m trying to make a wireless weather station where I put the DHT11 module on an Arduino board outside with a NRF24L01 Transceiver module and another Arduino board inside where I project the data on to an LCD screen (also with a NRF24L01 Transceiver module).

I have made a menu on the LCD screen so I can choose between temperature and humidity, but I don’t know how to use the select button to show the data.

Check out the LCD I am using:
https://www.jaycar.com.au/arduino-compatible-2-x-16-lcd-controller-module/p/XC4454

And also the code:

#include <LiquidCrystal.h>
const int pin_RS = 8; 
const int pin_EN = 9; 
const int pin_d4 = 4; 
const int pin_d5 = 5; 
const int pin_d6 = 6; 
const int pin_d7 = 7; 
const int pin_BL = 10; 
LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);

};
void setup() {
 lcd.begin(16, 2);
 delay(3000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("   Loading...");
 delay(1000);
 lcd.clear();
 delay(1000);
 lcd.print("   Loading...");
 delay(1000);
 lcd.clear();
 delay(1000);
 lcd.print("   Loading...");
 delay(1000);
 lcd.clear();
 delay(1000);
 lcd.print("   Loading...");
 delay(1000);
 lcd.clear();
 lcd.print("   Loading...");
 delay(500);
 lcd.setCursor(0,0);
 lcd.clear();
 lcd.print("     Setup");
 lcd.setCursor(0,1);
 delay(500);
 lcd.print("    Complete");
 delay(2000);
 lcd.clear() ; 
 lcd.setCursor(0,0);
 lcd.print("    Welcome");
 lcd.setCursor(0,1);
 delay(500);
 lcd.print("       To");
 delay(2000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Wireless Weather");
 lcd.setCursor(0,1);
 lcd.print("    Station");
 delay(2000);
 lcd.clear();\
 lcd.setCursor(0,0);
 lcd.print("Temperature -");
 lcd.setCursor(0,1);
 lcd.write("Humidity");

 
 
}
void loop() {
  int x;
 x = analogRead (0);
 lcd.setCursor(0,0);
 
 if (x < 200) {
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Temperature -");
   lcd.setCursor(0,1);
   lcd.print("Humidity");
   
   
 }
 else if (x < 400){
   lcd.clear();
   lcd.print("Temperature");
   lcd.setCursor(0,1);
   lcd.print("Humidity    -");
 }
   else if (x < 600){
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Temperature -");
   lcd.setCursor(0,1);
   lcd.print("Humidity");
}
}

Some help pls…

A vid of the setup:
https://bit.ly/3mmsSlc

This company gives very poor user-support on their website and wants the user to pay high prices

I would never by at this shop again

The buttons are connected to a resistor-network so pressing a button will give different values with the analog read.

as a general hint: use self-explaining names for all variables and functions. It makes your code much easier to maintain.
At the moment your code is short enough to remember ah! "x" is the analog-read. As soon as you have more than five variables it becomes hard to remember after three months. With single letters you have to lookup the meaning by scrolling through the code again and again. If the variables/functions-name has a self-explaining name you just read it
and everything is clear.

here is a demo-code that shows non-blocking timing
how to use the serial monitor to check what is going on in your code
and is a testprogram to find out which values are measured by each button-press
then you should code a range for each value because the values will vary a little

#include <LiquidCrystal.h>
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;

LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);


void setup() {
 Serial.begin(115200);
 Serial.println("Setup-Start"); 
 lcd.begin(16, 2);
}


boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired
}

unsigned long MyTimerSerial;
unsigned long MyTimerLCD;

const byte Button_ADC = 0;

void loop() {
  int ADC_Value;
  ADC_Value = analogRead (Button_ADC);

  // once every 1000 milliseconds the function TimePeriodIsOver becomes true
  // so only once every 1000 milliseconds the code inside the if-condition
  // is executed which slows down the serial output to a readable speed
  if (  TimePeriodIsOver(MyTimerSerial,1000) ){
    Serial.print("ADC Value is:");
    Serial.println(ADC_Value);
  }

  // once every 250 milliseconds the function TimePeriodIsOver becomes true
  // so only once every 250 milliseconds the code inside the if-condition
  // is executed which gives a pretty fast reaction on the button-press
  if (  TimePeriodIsOver(MyTimerLCD,250) ){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ADC Value:");
    lcd.print(ADC_Value);
  }
    
}

best regards Stefan

superninja931:
A vid of the setup:
Google Drive: Sign-in

link needs access-rights.
In general: using bit.ly-links to external websites is always a risc for users to be re-directed to an virus-infectious website.
You should NOT use such links. use links that lead directly to well known websites . Like youtube etc.
best regards Stefan

does this link work?
https://drive.google.com/file/d/1PNnyjQB4346iYtvvs99ImzxZdQa3s_h7/view

superninja931:
does this link work?
Google Drive: Sign-in

no. This is still a private owned webadress which could be infectious or malicious
If you want to show your own setup-procedure what is so hard about uploading a video to youtube or vimeo
or using a blog-site like wordpress etc.?

Sorry but I don't understand,
What I want to do is show the data when I select either temperature or humidity on the LCD (btw I haven't gotten on to the coding with the DHT11).
And the only reason I bought from Jaycar is because it's just down the road so I don't have to wait for shipping (and + eBay can be a little on the low-quality side of things).

It might be that the reading of the ADC if you press another button is simply not matching your if-condition

else if (x < 600){

and to analyse that you should compile and upload my testcode. This testcode will show the values you get from the ADC

best regards Stefan

So here is a version of your code for testpurposes that
shows just the ADC-value of no if-condition evaluates to true
And it gives you an impression how fast the loop repeats.
The numbers of the counter counts up every 0,1 second through a delay(100)
without this delay it would be even way faster looping

#include <LiquidCrystal.h>
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;

LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);

int MyDemoCounter = 0;

void setup() {
  // make sure to adjust the baudrate in the serial monitor
  // to match the value inside the parenthesis
  Serial.begin(115200);
  Serial.println("Setup Start");

  lcd.begin(16, 2);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Wireless Weather");
  lcd.setCursor(0, 1);
  lcd.print("    Station");
  delay(2000);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperature -");
  lcd.setCursor(0, 1);
  lcd.write("Humidity");

}

const byte Button_ADC = 0;

void loop() {
  int ADC_Value;
  ADC_Value = analogRead (Button_ADC);

  lcd.setCursor(0, 0);

  if (ADC_Value < 200) {
    lcd.clear();
    lcd.setCursor(0, 0);
             //1234567890123456  16 characters in line
    lcd.print("Temperature -");
    lcd.setCursor(0, 1);
             //1234567890123456  16 characters in line
    lcd.print("Humidity");
  }

  else if (ADC_Value < 400) {
    lcd.clear();
    lcd.print("Temperature");
    lcd.setCursor(0, 1);
    lcd.print("Humidity    -");
  }

  else if (ADC_Value < 600) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperature -");
    lcd.setCursor(0, 1);
    lcd.print("Humidity");
  }
  // if none of the if- and else-if-condition evaluated to true
  // just show value 
  else {
    MyDemoCounter++;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ADC:");
    lcd.print(ADC_Value);

    lcd.setCursor(0, 1);
    lcd.print("Cnt:");
    lcd.print(MyDemoCounter);
    lcd.print("      ");
    
  }  
  // without any delay this code updates the LCD so fast that the second row
  // becomes dimmed because of all the fast calls to lcd.clear()
  delay(100);
}

best regards Stefan

if you need something else you should describe in detail what should happens step by step in detail
You should post a functional description in normal words

1 if I press button (which one?) then thing1 happens (what should happen?)

2 if thing1 has happened what should happen next ? depending on what?

etc. etc. etc.

best regards Stefan

oh ok,

  1. The weather station turns on
  2. The LCD shows 2 options (Temperature/Humidity)
  3. By default when the weather station is turned on the marker (a dash) is next to temperature
  4. If you push the 'Down' Button the marker moves next to the humidity
  5. If you push the 'Select/Right' Button the LCD shows the related information (based on which option the marker is next to
  6. If you push the 'Left' Button the LCD returns to the menu where it displays either Temperature/Humidity.

The Video Link:

OK a short answer because I want to go sleeping:

with the button-press uo/down a variable example-name "ShowTempOrHum"
stores a value representing temp or another value representing humidity

if select-button is pressed print temperure or humidity depending on the value of variable "ShowTempOrHum"

if button left is pressed return to show menu to choose from

Anyway what do you think about re-designing what the display shows?

you've got 2 x 16 characters

Temp: 23,45°C
Humid.: 78,56%

should fit easily on the display all the time
So there would be no need for button-presses to show the actual values.

Of course old programmers wisdom: a program is never finished it's always just working (- bugfree / not crashing - most of the time)

As long as there is free-memory additional functions could be added, refined, tweaked - repeat

like store min/max of the day, (make time when to begin next dayperiod adjustable), add a clock,
min/max of of the week / of the month calculate averages, send data to thingspeak etc. etc. etc, etc. etc.

This all leads into the direction of using a statemachine

here is a tutorial about statemachines

If you want to keep the button-presses your statemachine has states like
temp is chosen
hum is chosen

show tempertaure-value
show humidity-value
show menu choose temp/hum

best regards Stefan

Ok Thx

If you have any questions about the statemachine example-code or questions how to transfer your code into a state-machine-version just post the question and your complete sketch.

best regards Stefan