Hi all. First post here. New to Arduino projects and really appreciate the forum so far, thanks to everyone who posts!
I have a question about a project I'm working on for a controlled reptile environment and haven't been able to figure it out on my own.
Currently the Arduino is programmed to read a temp/humidity sensor and turn on/off a heat lamp depending on current temp. The issue is if I unplug the sensor it will display the last recorded reading. If I plug the sensor back in the values update accordingly. Otherwise the serial print, LCD screen and heat lamp relay all act as if the last recorded values are the current values.
This issue only started happening once I added the day light lamp timer and relay code which when I removed one at a time doesn't resolve so it's hard to isolate. Similarly, if I re-upload the code to the Arduino it will display the correct reading whether unplugged or not which I don't understand why the loop restarting is not fulfilling a similar function.
For clarification the full code turns on/off a day light lamp through relay, heat lamp control through relay and temp/humidity sensor with LCD readings.
#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2); // the display is connected to said pins
int val;
int tempPin = 0; // temp sensor is connected to pin A0
int hlamp = 9; // heat lamp is to pin 9
const int lamp = 8; // lamp is connected to pin 8
int lampState = LOW;
dht DHT;
unsigned long previousMillis = 0; // beginning delay for lamp
const long interval = 60000; // on for x amount of time
void setup(){
Serial.begin(9600);
delay(500); //Delay to let system boot
delay(1000); //Wait before accessing Sensor
lcd.begin(16, 2); //lcd = liquid crystal display the lcd is 16x2 ; 2 rows 16 cells
pinMode(hlamp, OUTPUT);
pinMode(lamp, OUTPUT);
digitalWrite(lamp, HIGH);
}//end "setup()"
void loop(){
//begin lamp timer
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (lampState == LOW) {
lampState = HIGH;
} else {
lampState = LOW;
}}
digitalWrite(lamp, lampState);
//end lamp timer
DHT.read11(dht_apin);
if (1.8*DHT.temperature+32 < 91) // max temp is 91
{
digitalWrite(hlamp, LOW); // turn on heat lamp
Serial.print("LAMP ON");
}
else // else turn off the heat lamp
{
digitalWrite(hlamp, HIGH);
Serial.print("LAMP OFF ");
}
Serial.print(", HUMIDITY:, ");
Serial.print((int)round(DHT.humidity));
Serial.print(" , TEMP:, ");
Serial.println(round(1.8*DHT.temperature+32));
lcd.print(" TEMP HUMID");
lcd.setCursor(2, 1);
lcd.print((int)round(1.8*DHT.temperature+32));
lcd.setCursor(5,1);
lcd.print("F");
lcd.setCursor(10, 1);
lcd.print((int)round(DHT.humidity));
lcd.setCursor(13, 1);
lcd.print("%");
delay(3000);
lcd.clear();
}// end loop
Thank you in advance.