1

I've been trying to make a weather station using a I2C LCD Display and a DHT11, I know, the DHT11 is a bit low res and all that, but it came with the kit I ordered, so it's okay.

However, I'm trying to have it display like a "title" screen, and after a 2sec delay it should proceed to say "Welcome!" and "Press to Start", and wait there until the button is pressed, and after the button is pressed it should proceed to show the temp and humidity.

However it doesn't seem to mind any of the functions added, unless I set the "if" statement at the beginning of the loop to "LOW", then for some reason, the program stops at "Welcome!" and "press to start". If I change the "if" statement condition to "HIGH" it just proceeds straight through to the temp and humidity.

#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

//CONSTANTS
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

const int buttonPin = 6;

int buttonState = 1;

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  dht.begin();
  lcd.begin();
  lcd.clear();
  delay(1000);
  lcd.setCursor(0, 0);
  lcd.print("WeatherMate");
  lcd.setCursor(0, 1);
  delay(250);
  lcd.print("Press 'Start'");
  delay(1000);;
  if (buttonState = 0)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome!");
    lcd.setCursor(0, 1);
    lcd.print("Press Start");
  }
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState = 1) {
  delay(500);

  hum = dht.readHumidity();
  temp = dht.readTemperature();
  if (temp <= 25) {
    digitalWrite(13, LOW);
    digitalWrite(12, HIGH);
  }
  if (temp >= 25) {
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
  }
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

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

  delay(500); //Delay 2 sec.

}
}

Anyone? I'm stuck on this one.

  • 2
    Assignment is =. Comparison is ==. – Majenko Mar 27 '18 at 8:45
1

As Majenko points out in his comment, a couple of your if statements have the assignment operator (ie. =) whereas you are actually after the comparison operator (ie. ==).

if (buttonState = 0)

should be:

if (buttonState == 0)

And

if (buttonState = 1) {

should be:

if (buttonState == 1) {

When an assignment is used, the if statement will execute if the value being assigned is a non-zero. For more information see Using assignment as a condition expression? over on Stack Overflow.

| improve this answer | |
  • Actually it's not an indication of assignment success, but the value assigned 'falling through' to the if which is why changing 0 to 1 makes it stick in the first if. – Majenko Mar 27 '18 at 9:20
  • Thank you! This worked a charm, but it doesn't go to the "Welcome" screen now, any ideas? – Eirik Medlie Mar 27 '18 at 9:51
  • Where in setup are you reading the current state of the button? – Majenko Mar 27 '18 at 10:24
  • @Majenko Quite right. A mistruth that I learnt early on that has been hard to shake. I'll change it right away. I don't want to pass on this mistruth to others! – sa_leinad Mar 27 '18 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.