Stuck with DHT11 and state machine

I am trying to make a project that drives a motor forward when temperature is above a certain amount, stop when it is between two values, and backward when it is below a certain amount.
I have every state as its own if statement, but I don’t think that is correct.
As of now, I have switched every motor “on” (backwards or forward) to simply HIGH to test with an LED. However, my current code has the LED remain on constantly, no matter what happens to the other inputs. I am stuck, and other questions have only led me to get more confused…

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7

int Switchopen = 8;
int Switchclose = 6;
int motorPin = 9;

void setup(){
lcd.begin(16, 2);
pinMode(13, OUTPUT);
//Serial.begin(9600);
}

void loop()
{
{
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(9,0);
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print(“C”);
lcd.setCursor(0,1);
lcd.print(“Humidity: “);
lcd.print(DHT.humidity);
lcd.print(”%”);
delay(1000);
}
{
int Switchopen = 8;
int Switchclose = 6;
pinMode(Switchopen, INPUT);
pinMode(Switchclose, INPUT);
}
{
if ((((DHT.temperature >= 24.00) && (DHT.temperature <= 27.00) && (Switchopen == LOW) && (Switchclose == LOW))))
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature < 24.00) && (Switchopen == LOW) && (Switchclose == LOW)));
analogWrite(motorPin, HIGH); //Backward
if (((DHT.temperature > 27.00) && (Switchopen == LOW) && (Switchclose == LOW)));
analogWrite(motorPin, HIGH); //Forward
if ((((DHT.temperature >= 24.00) && (DHT.temperature <= 27.00) && (Switchopen == LOW) && (Switchclose == HIGH))));
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature < 24.00) && (Switchopen == LOW) && (Switchclose == HIGH)));
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature > 27.00) && (Switchopen == LOW) && (Switchclose == HIGH)));
analogWrite(motorPin, HIGH); //Forward
if ((((DHT.temperature >= 24.00) && (DHT.temperature <= 27.00) && (Switchopen == HIGH) && (Switchclose == LOW))));
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature < 24.00) && (Switchopen == HIGH) && (Switchclose == LOW)));
analogWrite(motorPin, HIGH); //backward
if (((DHT.temperature > 27.00) && (Switchopen == HIGH) && (Switchclose == LOW)));
analogWrite(motorPin, HIGH); //backward
if ((((DHT.temperature > 24.00) && (DHT.temperature < 27.00) && (Switchopen == HIGH) && (Switchclose == HIGH))));
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature < 24.00) && (Switchopen == HIGH) && (Switchclose == HIGH)));
analogWrite(motorPin, LOW); //OFF
if (((DHT.temperature > 27.00) && (Switchopen == HIGH) && (Switchclose == HIGH)));
analogWrite(motorPin, LOW); //OFF
}
{ delay(1000);
}
}

Temperature_Display.ino (2.34 KB)

It would help us if You use code tags, </>, around a formatted code.
Parts of Your code now placd in loop() ought to be written inside Setup().

  pinMode(Switchopen, INPUT);
  pinMode(Switchclose, INPUT);

The statements

 int Switchopen = 8;
  int Switchclose = 6;

are already defined in Setup(). Defining them in loop() creates another instance of them.
I probably miss something but in loop()I can’t find any LED operations.

You have some INPUTS declared. Why not INPUT_PULLUP?

Attach the wiring diagram, please.

 if (((DHT.temperature > 27.00) && (Switchopen == LOW) && (Switchclose == LOW)));
   analogWrite(motorPin, HIGH); //Forward

When an if() statement ends with a semicolon the following statement is executed unconditionally. Lose the semicolons after if().

 int Switchopen = 8;

Declare a variable called Switchopen and assign it the value of 8. Nowhere in your code is the switch actually read. That doesn't happen automatically, you have to tell the code to do it. See the reference page about pinMode(); and digitalRead();

OHH!! Thank you guys for the input, I also just realized I uploaded the wrong version of the code, I fixed most of these little issues in the newer one, that’s the one I wanted looked at. Here it is…

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define dhtPIN_7
#define DHTTYPE DHT11 dht11

dht DHT;
int Switchopen = 8; //Limit switch, on when opened completely
int Switchclose = 6; //Limit switch, on when closed completely
int motorPin = 9;
int temp = DHT.temperature ; //not sure if this is right, because DHT.temp serial reads as 1022 not 27
int tempMax = 29.00; //max temp is 29 degrees C
int tempMin = 24.00; //min temp is 24 degrees C

void setup(){
temp = analogRead(A0); //I was told to include this but I see no reason why
pinMode(9, OUTPUT);
pinMode(A0, INPUT);
lcd.begin(16, 2);
//float t = dht.readTemperature;
Serial.begin(9600);
}

void loop()
{
Serial.println(DHT.temperature);
{
int chk = DHT.read11(dhtPIN_7);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(9,0);
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print(“C”);
lcd.setCursor(0,1);
lcd.print(“Humidity: “);
lcd.print(DHT.humidity);
lcd.print(”%”);
delay(1000);
}
{
int Switchopen = 8;
int Switchclose = 6;
pinMode(Switchopen, INPUT);
pinMode(Switchclose, INPUT);
}
{
if ((temp >= tempMin && temp <= tempMax) == true){ //when temp is between 24 and 27, STOP
digitalWrite(motorPin, LOW); //OFF
}
}
{
if (temp < tempMin && digitalRead(Switchopen) == LOW && digitalRead(Switchclose) == HIGH){
digitalWrite(motorPin, LOW);//OFF //when temp is less than 24 and window is closed, do nothing
}
else if (temp < tempMin)
(digitalWrite(motorPin, HIGH)); //close when temp is below 24 in any other situation than above
}
{
if (temp > tempMax && digitalRead(Switchopen) == LOW){
}
digitalWrite(motorPin, HIGH); //open when temp is above 27 and is closed
}
{
if (temp > tempMax && digitalRead(Switchopen) == HIGH && digitalRead(Switchclose) == LOW){
}
digitalWrite(motorPin, HIGH); //close when temp is above 27 and window is open
}
{
delay(1000);
}
}

There are plenty of the curly brackets {}. What are they doing there? I wonder if the last half of the code will be executed at all. Loop seams to be ended by } before the end of the code.

How would you suggest formatting it so it works? I feel like when I remove the brackets the if statements end up getting ignored. My issue is the LED ends up being always on HIGH

I don’t want to marks words but “You feel”… Drop that. Write code out of knowledge. Also use code tags when attaching code.

Look at my remarks out to the left in this code supplied code using tags:
Look for “Why?” Pointless use of { or }. Look for MISTAKE.

void loop()
{
  Serial.println(DHT.temperature);
  {                                                           Why?
    int chk = DHT.read11(dhtPIN_7);
    lcd.setCursor(0, 0);
    lcd.print("Temp: ");
    lcd.setCursor(9, 0);
    lcd.print(DHT.temperature);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(0, 1);
    lcd.print("Humidity: ");
    lcd.print(DHT.humidity);
    lcd.print("%");
    delay(1000);
  }                                                           Why?
  {                                                          Why?
    int Switchopen = 8;                                      Move to Setup
    int Switchclose = 6;                                     Move to Setup
    pinMode(Switchopen, INPUT);                              Move to Setup
    pinMode(Switchclose, INPUT);                             Move to Setup
  }                                                          Why?
  {                                                          Why?
  if ((temp >= tempMin && temp <= tempMax) == true) { //when temp is between 24 and 27, STOP
      digitalWrite(motorPin, LOW); //OFF
    }
  }                                                           Why?
  {                                                           Why?
    if (temp < tempMin && digitalRead(Switchopen) == LOW && digitalRead(Switchclose) == HIGH) {
      digitalWrite(motorPin, LOW);//OFF     //when temp is less than 24 and window is closed, do nothing
    }
    else if (temp < tempMin)
      (digitalWrite(motorPin, HIGH)); //close when temp is below 24 in any other situation than above
  }                                                           Why?
  {                                                           Why?
    if (temp > tempMax && digitalRead(Switchopen) == LOW) {
    }                                                                                              MISTAKE! It makes  the next line execute always.
    digitalWrite(motorPin, HIGH); //open when temp is above 27 and is closed
  }
  {                                                           Why?
    if (temp > tempMax && digitalRead(Switchopen) == HIGH && digitalRead(Switchclose) == LOW) {
    }                                                                                              MISTAKE!  makes  the next line execute always.
    digitalWrite(motorPin, HIGH); //close when temp is above 27 and window is open
  }
  {                                                           Why?
    delay(1000);
  }                                                           Why?

............remarks out to the RIGHT..... Use a wide window!

Every {} pair creates a block. Variables created inside that block are only accessible inside the block. Every time you write int varName = 2; you are creating a new variable with that name.

But if you just wrote varName = 2; then it updates a variable without creating a new one.

C will let you have many variables with the same name, so long as they have different scope. So the inner declarations inside blocks "override" the other variables sharing the name.

Try this:

void setup() {
  Serial.begin(9600);
  int test = 2;
  Serial.println(test); // prints 2

  {
    int test = 3;
    Serial.println(test); // prints 3
  } 

  Serial.println(test); // prints 2
}
void loop() {}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hey guys! Thanks for all the input :slight_smile: I finally got it to work, once I fixed all the errors you guys mentioned, all I had to do was make my variables a digitalRead, and then restate my temp value in the loop.

Good to hear!