arduino mega with photoresistor and DHT11

hello.
I have a project using arduino mega.

In this case if the Temperature is above 25C the relay will turn on the cooling fan, and will turn off if the value is below 25C.

If the photoresistor shows a value of 850 or more the lights will turn off and if below 850 the lights will turn on.
If the humidity is below 21% the outlet fans are off and if above 21% they turn on.

can someone please confirm on what pin is the photoresistor and the DHT11 connected.
i am using the following code:

#define dht11_pin 54 //Analog port 0 on Arduino Mega2560

byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while (!digitalRead(dht11_pin));
delayMicroseconds(30);
if (digitalRead(dht11_pin) != 0 )
bitSet(result, 7-i);
while (digitalRead(dht11_pin));
}
return result;
}
//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int HumidityTempSensor = 0;
int lightSensor = 1;
//int tempSensor = 2;
//define variables to store moisture, light, and temperature values
int ht_val;
int light_val;
//int temp_val;

void setup() {
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
Serial.begin(9600); //open serial port
pinMode (2, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
digitalWrite (2, LOW);
digitalWrite (7, LOW);
digitalWrite (8, LOW);
digitalWrite (9, LOW);
}

void loop() {
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition

digitalWrite(dht11_pin, LOW);
delay(18);
digitalWrite(dht11_pin, HIGH);
delayMicroseconds(1);
pinMode(dht11_pin, INPUT);
delayMicroseconds(40);

if (digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
delay(1000);
return;
}
delayMicroseconds(80);
if (!digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 2 not met"); //wair for second response signal:HIGH
return;
}

delayMicroseconds(80);// now ready for data reception
for (i=0; i<5; i++)
{ dht11_dat = read_dht11_dat();} //recieved 40 bits data. Details are described in datasheet

  • pinMode(dht11_pin, OUTPUT);*
  • digitalWrite(dht11_pin, HIGH);*
  • byte dht11_check_sum = dht11_dat[0]+dht11_dat[2];// check check_sum*
  • if(dht11_dat[4]!= dht11_check_sum)*
  • {*
  • Serial.println("DHT11 checksum error");*
  • }*
    ht_val = analogRead(HumidityTempSensor); // read the value from the HumidityTemp sensor
    Serial.print("Humidity = ");
    Serial.print( dht11_dat[0], DEC );
    Serial.print("% ");
    Serial.print("Temperature = ");
    Serial.print( dht11_dat[2], DEC );
    Serial.println("C ");
    if (dht11_dat[0] < 21)
    {
    Serial.println("Turning Humidity Outletfan OFF");
    digitalWrite (7, HIGH);
    delay(5000);
    }
    if (dht11_dat[0] > 21)
    {
    Serial.println("Turning Humidity Outletfan ON");
    digitalWrite (7, LOW);
    delay(5000);
    }
    if (dht11_dat[2] > 25)
    {
    Serial.println("Turning Cooling Fan ON");
    digitalWrite (9, HIGH);
    delay(5000);
    }
    if (dht11_dat[2] < 25)
    {
    Serial.println("Turning Cooling Fan OFF");
    digitalWrite (9, LOW);
    delay(5000);
    }
    light_val = analogRead(lightSensor); // read the value from the photosensor
    Serial.print("Light Seonsor = ");
    Serial.println( light_val );
    if (light_val > 850)
    {
    Serial.println("Turning Lights ON");
    digitalWrite (8, HIGH);
    delay(5000);
    }
    if (light_val < 850)
    {
    Serial.println("Turning Lights OFF");
    digitalWrite (8, LOW);
    delay(5000);
    }
    //temp_val = analogRead(tempSensor);
    //Serial.print("temp sensor reads ");
    //Serial.println( temp_val );
    //if (temp_val < 920)
    //{
    //Serial.println("turning on low-temperature LED");
    //digitalWrite (2, HIGH);
    //delay(2000);
    //}
    //if (temp_val > 920)
    //{
    //Serial.println("turning off low-temperature LED");
    //digitalWrite (2, LOW);
    //delay(2000);
    //}
    }

The light sensor is PIN 1.

int lightSensor = 1;

However looking at the code it seems the command to read it is commented out in this sketch so it won't actually read anything.

light_val = analogRead(lightSensor); // read the value from the photosensor

The code suddenly becomes italics which is usually a sign of a large code block being remmed out - probably best to use the code button (top left) to include code in the posts so that all the special characters read right.

The dht11 is being read on pin 54

#define dht11_pin 54 //Analog port 0 on Arduino Mega2560

Now the rem statement on that line is a little confusing because this module needs a digital port and in other places in the code that's how this pin is defined.

while (!digitalRead(dht11_pin));

Also watch out because that pin is being toggled between a read and a write, which is allowed. But if you modify the code make sure that your mods are in the right place.

I also noticed there are no Pullups on the digital pins for the dht11. Probably a good idea for this module especially with the read and write changing state.