Weather station, HELP

So i have my weather station everything works fine.

But, i want to make my station this way: when i pressed the button it will show the temperature only
then i will press it again and it will show the humidity only and for last if i press it , it will show the pressure only and if i press it again it will show the temperature again and it will go around again.

If you have same time for my code please add the solution into my code, i would be very glad.
The button is connected to pin 6.

Here is my code:

#include <LiquidCrystal.h> //pripojíme knižnicu pre LCD 16x2
#include <Wire.h> // pripojíme knižnicu pre prepojovacie káble
#include <Adafruit_Sensor.h> // pripojíme knižnicu pre senzory
#include <Adafruit_BMP085_U.h> // pripojíme knižnicu pre senzor BMP180( BMP085 )
#include <cactus_io_SHT31.h> // pripojíme knižnicu pre senzor SHT31
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
cactus_io_SHT31 sht31;

///Prepojenie BMP180 a SHT31 k Arduinu UNO

//Pripojíme SCL na analógový vstup A5
//Pripojíme SDA na analógový vstup A4
//Pripojáme VDA na 3,3 Volta
//Pripojím GND na GND Arduino

const int buttonPin = 6;
const int backLight = 9;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // priradíme vývody

LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // zadáme vývody na LCD monitore

void setup()
{
Serial.begin(9600); //Priradíme Serial Monitor
pinMode(backLight,HIGH);
digitalWrite(backLight,HIGH);
lcd.begin(16, 2); // Zapneme obrazovku
lcd.print(" METEOSTANICA ");// Vypíše nám Meteostanica Viktor
lcd.setCursor(0,1);
lcd.print(" Viktor Joba 4F ");
delay(1500); // oneskorenie v ms
lcd.clear(); // vyčistíme obrazovku

if(!bmp.begin())
{
// cyklus na ten prípad ak by bol nijaky problem so senzorom BMP180
lcd.print("NO BMP180 DETECTED!");
while(1);
}

// cyklus na ten prípad ak by bol nijaky problem so senzorom SHT31
if (!sht31.begin()) {
lcd.println("NO SHT31 DETECTED!"); //
while(1) ;
}
}

void loop(){
sensors_event_t event; // vytvoríme nove udalosti
bmp.getEvent(&event);
if (event.pressure) {
float temperature;
bmp.getTemperature(&temperature); // načítame hodnotu teploty zo senzora BMP180

lcd.setCursor(0,0); // nastvíme kurzor na začiatok prvého riadku
lcd.print("T:");
lcd.print(temperature); // vypíše nám teplotu s nepresnosťou +/- 2
lcd.print(char(178)); // znak
lcd.print("C");

lcd.print(" H:");
lcd.print(round(sht31.getHumidity())); // načíta hodnotu vlhkosti zo senzora SHT31 s nepresnostou
lcd.print("%");

lcd.setCursor(0,1); // nastavíme kurzor na začiatok druhého riadku
lcd.print(" P:");
lcd.print(event.pressure); // načítame hodnotu tlaku zo senzora BMP180
lcd.print(" hPa ");
}
else
{
lcd.println("Sensor error"); // Ak nastane niaka chyba
}
delay(250);
}

kopka_meteostania.ino (2.45 KB)

I would use the method in the state change detection example to detect and count the button presses and then use the buttonPushCounter to choose which item to display on the LCD.

Pseudo code:

if(buttonPushCounter == 0)
{
  // display temperature
}
else if(buttonPushCounter == 1)
{
  // display hunmidity
}
else if(buttonPushCounter == 2)
{
  // display pressure
}
else
{
  buttonPushCounter = 0;
}

Thanks for the answer! I like your method.

I Have a problem with write it..

Please can you add into my official code?

Please can you add into my official code?

No. This is not the free code place, but we are glad to help you learn to code. Show the code that you tried, describe what it does and how that differs from what you want the code to do. Please read the "how to use the forum - please read" stickies to see how to format and post code. Also, if you get compile errors, post the entire text of the error message(s) in code tags.

WHAT SHOULD I CHANGE?? IT DONT WORK HELP PLEASE

#include <LiquidCrystal.h> //pripojíme knižnicu pre LCD 16x2
#include <Wire.h> // pripojíme knižnicu pre prepojovacie káble
#include <Adafruit_Sensor.h> // pripojíme knižnicu pre senzory
#include <Adafruit_BMP085_U.h> // pripojíme knižnicu pre senzor BMP180( BMP085 )
#include <cactus_io_SHT31.h> // pripojíme knižnicu pre senzor SHT31

//Prepojenie BMP180 a SHT31 k Arduinu UNO

//Pripojíme SCL na analógový vstup A5
//Pripojíme SDA na analógový vstup A4
//Pripojáme VDA na 3,3 Volta
//Pripojím GND na GND Arduino

int backLight = 9;
const int buttonPin = 6;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // priradíme vývody
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // zadáme vývody na LCD monitore

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

cactus_io_SHT31 sht31;

void setup()
{
Serial.begin(9600); //Priradíme Serial Monitor
pinMode(backLight,HIGH);
digitalWrite(backLight,HIGH);
pinMode(buttonPin,INPUT);
lcd.begin(16, 2); // Zapneme obrazovku
lcd.print(" METEOSTANICA ");// Vypíše nám Meteostanica Viktor
lcd.setCursor(0,1);
lcd.print(" Viktor Joba 4F ");
delay(1500); // oneskorenie v ms
lcd.clear(); // vyčistíme obrazovku

if(!bmp.begin())
{
// cyklus na ten prípad ak by bol nijaky problem so senzorom BMP180
lcd.print("NO BMP180 DETECTED!");
while(1);
}

// cyklus na ten prípad ak by bol nijaky problem so senzorom SHT31
if (!sht31.begin()) {
lcd.println("NO SHT31 DETECTED!"); //
while(1) ;
}

}

void loop(){

sensors_event_t event; // vytvoríme nove udalosti
bmp.getEvent(&event);
if (event.pressure){

float temperature;
bmp.getTemperature(&temperature); // načítame hodnotu teploty zo senzora BMP180

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState){
if (buttonState = HIGH){
if (buttonPushCounter = 0)
lcd.setCursor(0,0); // nastvíme kurzor na začiatok prvého riadku
lcd.print("T:");
lcd.print(temperature); // vypíše nám teplotu s nepresnosťou +/- 2
lcd.print(char(178)); // znak
lcd.print("C");
} else if (buttonPushCounter = 1) {
lcd.print(" H:");
lcd.print(round(sht31.getHumidity())); // načíta hodnotu vlhkosti zo senzora SHT31 s nepresnostou
lcd.print("%");
} else if (buttonPushCounter = 2) {
lcd.setCursor(0,0); // nastavíme kurzor na začiatok druhého riadku
lcd.print("P:");
lcd.print(event.pressure); // načítame hodnotu tlaku zo senzora BMP180
lcd.print("hPa");
}
delay(50);
lastButtonState = buttonState;
}
} else {
lcd.print("ERROR");
}
delay(250);
}

try.ino (2.75 KB)

Don't panic. We will get you there.

 if (buttonState = HIGH){

In your if and if else statements you have syntax like above. You are making a comparison in the if and if else statements. Use = for assignment and == for comparison. So the statements should be like:

if (buttonState == HIGH){

Note the == instead of = in the comparison.

From:
https://www.arduino.cc/en/Reference/If

Warning:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

Please use code tags when posting code like it says in the "how to use the forum" stickies.

Here is your code with the if and if else statements fixed. Also formatted better and in code tags. I cannot compile or test the code cause I don't have the libraries.

#include <LiquidCrystal.h> //pripojíme knižnicu pre LCD 16x2
#include <Wire.h> // pripojíme knižnicu pre prepojovacie káble
#include <Adafruit_Sensor.h> // pripojíme knižnicu pre senzory
#include <Adafruit_BMP085_U.h> // pripojíme knižnicu pre senzor BMP180( BMP085 )
#include <cactus_io_SHT31.h> // pripojíme knižnicu pre senzor SHT31


//Prepojenie BMP180 a SHT31 k Arduinu UNO

//Pripojíme SCL na analógový vstup A5
//Pripojíme SDA na analógový vstup A4
//Pripojáme VDA na 3,3 Volta
//Pripojím GND na GND Arduino

int backLight = 9;
const int buttonPin = 6;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // priradíme vývody
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);   // zadáme vývody na LCD monitore

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

cactus_io_SHT31 sht31;

void setup()
{
  Serial.begin(9600); //Priradíme Serial Monitor
  pinMode(backLight, HIGH);
  digitalWrite(backLight, HIGH);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2); // Zapneme obrazovku
  lcd.print("  METEOSTANICA ");// Vypíše nám Meteostanica Viktor
  lcd.setCursor(0, 1);
  lcd.print(" Viktor Joba 4F  ");
  delay(1500); // oneskorenie v ms
  lcd.clear(); // vyčistíme obrazovku


  if (!bmp.begin())
  {
    // cyklus na ten prípad ak by bol nijaky problem so senzorom BMP180
    lcd.print("NO BMP180 DETECTED!");
    while (1);
  }

  // cyklus na ten prípad ak by bol nijaky problem so senzorom SHT31
  if (!sht31.begin()) {
    lcd.println("NO SHT31 DETECTED!"); //
    while (1) ;
  }


}

void loop()
{
  sensors_event_t event; // vytvoríme nove udalosti
  bmp.getEvent(&event);
  if (event.pressure)
  {
    float temperature;
    bmp.getTemperature(&temperature); // načítame hodnotu teploty zo senzora BMP180

    buttonState = digitalRead(buttonPin);

    if (buttonState != lastButtonState) 
    {
      if (buttonState == HIGH)
      {
        if (buttonPushCounter == 0)
        {
          lcd.setCursor(0, 0); // nastvíme kurzor na začiatok prvého riadku
          lcd.print("T:");
          lcd.print(temperature); // vypíše nám teplotu s nepresnosťou +/- 2
          lcd.print(char(178));   // znak
          lcd.print("C");
        } 
        else if (buttonPushCounter == 1) 
        {
          lcd.print("  H:");
          lcd.print(round(sht31.getHumidity()));  // načíta hodnotu vlhkosti zo senzora SHT31 s nepresnostou
          lcd.print("%");
        } 
        else if (buttonPushCounter == 2) 
        {
          lcd.setCursor(0, 0); // nastavíme kurzor na začiatok druhého riadku
          lcd.print("P:");
          lcd.print(event.pressure); // načítame hodnotu tlaku zo senzora BMP180
          lcd.print("hPa");
        }
        // here we reset the buttonPushCounter so that the menu wraps around to 0 after 3 pushes
         else
        {
          buttonPushCounter = 0;
        }
        delay(50);
        lastButtonState = buttonState;
      }
    } 
    else 
    {
      lcd.print("ERROR");
    }
    delay(250);
  }

Still doesnt work.. :frowning:

How am I supposed to respond to "Still doesn't work"? You need to tell us what is happening. I don't have the hardware and libraries to duplicate your project so that I can test it, so I must depend on you to tell me what is wrong.

One thing that I noticed is that the buttonPushCounter is not incremented so I added that to the code.

#include <LiquidCrystal.h> //pripojíme knižnicu pre LCD 16x2
#include <Wire.h> // pripojíme knižnicu pre prepojovacie káble
#include <Adafruit_Sensor.h> // pripojíme knižnicu pre senzory
#include <Adafruit_BMP085_U.h> // pripojíme knižnicu pre senzor BMP180( BMP085 )
#include <cactus_io_SHT31.h> // pripojíme knižnicu pre senzor SHT31


//Prepojenie BMP180 a SHT31 k Arduinu UNO

//Pripojíme SCL na analógový vstup A5
//Pripojíme SDA na analógový vstup A4
//Pripojáme VDA na 3,3 Volta
//Pripojím GND na GND Arduino

int backLight = 9;
const int buttonPin = 6;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // priradíme vývody
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);   // zadáme vývody na LCD monitore

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

cactus_io_SHT31 sht31;

void setup()
{
  Serial.begin(9600); //Priradíme Serial Monitor
  pinMode(backLight, HIGH);
  digitalWrite(backLight, HIGH);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2); // Zapneme obrazovku
  lcd.print("  METEOSTANICA ");// Vypíše nám Meteostanica Viktor
  lcd.setCursor(0, 1);
  lcd.print(" Viktor Joba 4F  ");
  delay(1500); // oneskorenie v ms
  lcd.clear(); // vyčistíme obrazovku


  if (!bmp.begin())
  {
    // cyklus na ten prípad ak by bol nijaky problem so senzorom BMP180
    lcd.print("NO BMP180 DETECTED!");
    while (1);
  }

  // cyklus na ten prípad ak by bol nijaky problem so senzorom SHT31
  if (!sht31.begin()) {
    lcd.println("NO SHT31 DETECTED!"); //
    while (1) ;
  }


}

void loop()
{
  sensors_event_t event; // vytvoríme nove udalosti
  bmp.getEvent(&event);
  if (event.pressure)
  {
    float temperature;
    bmp.getTemperature(&temperature); // načítame hodnotu teploty zo senzora BMP180

    buttonState = digitalRead(buttonPin);

    if (buttonState != lastButtonState) 
    {
      if (buttonState == HIGH)
      {
        if (buttonPushCounter == 0)
        {
          lcd.setCursor(0, 0); // nastvíme kurzor na začiatok prvého riadku
          lcd.print("T:");
          lcd.print(temperature); // vypíše nám teplotu s nepresnosťou +/- 2
          lcd.print(char(178));   // znak
          lcd.print("C");
          buttonPushCounter++;  // increment buttonPushCounter
        } 
        else if (buttonPushCounter == 1) 
        {
          lcd.print("  H:");
          lcd.print(round(sht31.getHumidity()));  // načíta hodnotu vlhkosti zo senzora SHT31 s nepresnostou
          lcd.print("%");
          buttonPushCounter++;  // increment buttonPushCounter
        } 
        else if (buttonPushCounter == 2) 
        {
          lcd.setCursor(0, 0); // nastavíme kurzor na začiatok druhého riadku
          lcd.print("P:");
          lcd.print(event.pressure); // načítame hodnotu tlaku zo senzora BMP180
          lcd.print("hPa");
          buttonPushCounter++;  // increment buttonPushCounter
        }
        else
        {
          buttonPushCounter = 0;
        }
        delay(50);
        lastButtonState = buttonState;
      }
    } 
    else 
    {
      lcd.print("ERROR");
    }
    delay(250);
  }

What happens now?

If i press the button it shows the temperature but if i press it again nothing happens.. The temperature stay here and nothing else is on the LCD

Thanks, that is a more useful description. Is that with the new code that I posted in reply #10?

EDIT I just realized that the reply 10 code probably won't work right either. Give me a bit to see if i can fix it.

Yes with the #10 doesnt work..
Okay thanks.

Please try this.

#include <LiquidCrystal.h> //pripojíme knižnicu pre LCD 16x2
#include <Wire.h> // pripojíme knižnicu pre prepojovacie káble
#include <Adafruit_Sensor.h> // pripojíme knižnicu pre senzory
#include <Adafruit_BMP085_U.h> // pripojíme knižnicu pre senzor BMP180( BMP085 )
#include <cactus_io_SHT31.h> // pripojíme knižnicu pre senzor SHT31


//Prepojenie BMP180 a SHT31 k Arduinu UNO

//Pripojíme SCL na analógový vstup A5
//Pripojíme SDA na analógový vstup A4
//Pripojáme VDA na 3,3 Volta
//Pripojím GND na GND Arduino

int backLight = 9;
const int buttonPin = 6;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // priradíme vývody
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);   // zadáme vývody na LCD monitore

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

cactus_io_SHT31 sht31;

void setup()
{
  Serial.begin(9600); //Priradíme Serial Monitor
  pinMode(backLight, HIGH);
  digitalWrite(backLight, HIGH);
  pinMode(buttonPin, INPUT);
  lcd.begin(16, 2); // Zapneme obrazovku
  lcd.print("  METEOSTANICA ");// Vypíše nám Meteostanica Viktor
  lcd.setCursor(0, 1);
  lcd.print(" Viktor Joba 4F  ");
  delay(1500); // oneskorenie v ms
  lcd.clear(); // vyčistíme obrazovku


  if (!bmp.begin())
  {
    // cyklus na ten prípad ak by bol nijaky problem so senzorom BMP180
    lcd.print("NO BMP180 DETECTED!");
    while (1);
  }

  // cyklus na ten prípad ak by bol nijaky problem so senzorom SHT31
  if (!sht31.begin()) {
    lcd.println("NO SHT31 DETECTED!"); //
    while (1) ;
  }


}

void loop()
{
  sensors_event_t event; // vytvoríme nove udalosti
  bmp.getEvent(&event);
  if (event.pressure)
  {
    float temperature;
    bmp.getTemperature(&temperature); // načítame hodnotu teploty zo senzora BMP180

    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState)
    {
      if (buttonState == HIGH)
      {
        buttonPushCounter++;
      }

      if (buttonPushCounter == 0)
      {
        lcd.setCursor(0, 0); // nastvíme kurzor na začiatok prvého riadku
        lcd.print("T:");
        lcd.print(temperature); // vypíše nám teplotu s nepresnosťou +/- 2
        lcd.print(char(178));   // znak
        lcd.print("C");

      }
      else if (buttonPushCounter == 1)
      {
        lcd.print("  H:");
        lcd.print(round(sht31.getHumidity()));  // načíta hodnotu vlhkosti zo senzora SHT31 s nepresnostou
        lcd.print("%");

      }
      else if (buttonPushCounter == 2)
      {
        lcd.setCursor(0, 0); // nastavíme kurzor na začiatok druhého riadku
        lcd.print("P:");
        lcd.print(event.pressure); // načítame hodnotu tlaku zo senzora BMP180
        lcd.print("hPa");
      }
      else
      {
        buttonPushCounter = 0;
      }
      delay(50);
      lastButtonState = buttonState;
    }
  }
  else
  {
      lcd.print("ERROR");
  }  
  delay(250);
}

still the same problem...
:{

I made a small change to the code in reply# 13 that may help (try it). How is your button wired? Do you have a pulldown resistor on the switch input pin (6)?

the one leg is connected to the pin 6 and the second to 5V on the board

What should i change

You need the pulldown resistor from the input pin to ground to prevent the switch from floating (in an undefined state) when the switch is not pressed (open). The value of the resistor is not critical. 1K to 100k will be fine with 10K being the usual value. It will also help (but nor necessary) to put a 0.1uf cap across the switch (so input pin 6 to 5V). That will help to debounce the switch and prevent spurious counts.

Here is the photo of my connection of button

What should i change??

As far as I can tell you have the switch wired right.