While loop and if statements not working

Hi Arduino Community,

I would really like some help with the code for my project.

I am using a gas sensor, DHT11 sensor, two LEDs and a 12V relay.

I want the relay to turn on a 12V cooling element when the gas sensor reads a certain value.

While the gas sensor is at this value I want the temperature sensor to read the temperature and determine whether the blue or yellow LED should be on.

If none of the conditions are met I want the yellow LED to stay on, blue LED off and the relay to be off. I also need the sensors to continuously detect gas and temperature.

Currently the while loop I have prevents my sensors from continuously detecting. When I take the while loop out and just have if statements the sensors are continuously sensing (I use the Serial Monitor to see this), which is what I need.

I have tried else if statements as well, but I am not sure where I am going wrong.

Code I have done.

#include <DHT.h>
#define DHT_Sensor 11 // Defines pin number to which the sensor is connected
#define Gas_Sensor A0 //Gas Sensor on Analog pin 0
#define YellowLED 8 // Yellow LED on Digital Pin 8
#define BlueLED 9 // Blue LED on Digital Pin 9
#define Relay 10 // SPDT Relay on Digital Pin 10
dht DHT; // Creates DHT Object
void setup() {

** //Initialise the Digital Pins as OUTPUTS**
** pinMode (YellowLED, OUTPUT);**
** pinMode (BlueLED, OUTPUT);**
** pinMode (Relay, OUTPUT);**
** Serial.begin(9600); //Initialise the Serial Communication at 9600 bits per second**
}
void loop() {
int DHT_SensorValue = 20;
int Gas_SensorValue = 0; // Set the sensor value
int readData = DHT.read11(DHT_Sensor); // Reads the data from the sensor
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
** Gas_SensorValue = analogRead (Gas_Sensor); //Read the input on A0**
** Serial.println(Gas_SensorValue, DEC);**
** Serial.print("Temperature = "); //Read the Temperature Sensor**
** Serial.print(t);**
** if (Gas_SensorValue >= 120) {**
** digitalWrite (Relay, HIGH);**
** }**
** else {**
** digitalWrite (Relay, LOW);**
** digitalWrite (YellowLED, HIGH);**
** digitalWrite (BlueLED, LOW);**
** }**
** while (Gas_SensorValue >= 120); {**
** if (DHT_Sensor > 16) {**
** digitalWrite (YellowLED, HIGH);**
** digitalWrite (BlueLED, LOW);**
** }**
** else {**
** digitalWrite (YellowLED, LOW);**
** digitalWrite (BlueLED, HIGH);**
}
}
}

Please help.

Thanks

while (Gas_SensorValue >= 120);

Get rid of the semicolon at the end of the while and try it.

(deleted)

Yes... You'll be stuck in the while() loop as long as the condition is true... And once the yellow LED goes high and the blue LED goes low, the LED state will never change as long as you are stuck in that loop.

I'm not sure I've got all of the possibilities... I think I'd need to see a [u]truth table[/u] showing all of the input conditions and the associated output states.

If I understand correctly, you've got two conditional inputs (DHT & GAS) and that means there are 4 states. You just need to define the LED & relay states for those 4 conditions.

You probably need some AND conditions for your if-statments - i.e. If the gas and the DHT sensor are above their thresholds, do something.

Shai_Design:
int DHT_SensorValue = 20;
int Gas_SensorValue = 0; // Set the sensor value

  • Why would you set these values? As soon as you read data from the sensor it will override this.

Shai_Design:
int readData = DHT.read11(DHT_Sensor); // Reads the data from the sensor

-You never do anything with this so why even have it?

Shai_Design:
if (DHT_Sensor > 16) {

-This doesn't make sense. You are saying "if my pin changes" which you've already defined as pin 11.

I would highly recommend looking at the DHT example in the Arduino IDE to make yourself more familiar. I was almost able to understand what you were looking to do until i read "DHT_Sensor > 16".
Also, what Arduino are you using?

-Laggy