One of my first projects is a program to use the arduino as a Dehydrator. A DHT11 should give me data about humidity and temperature in the Dehydrator. I want to use this data to trigger a relay which will then trigger either a heating element (a ceramic heater lamp) and/or a fan that sucks the humidity out of the dehydrator.
I want to use an LCD monitor to show me the current humidity and the current temperature. It will also show me the thresholds for MaxTemperatur (which switches the heater off) and MaxHumidity (which switches the fan on)
Finally, I will use 2 capacitors to change the values of the maximal temperature and the maximal humidity.
Good News. Everything works. But there are 2 Problems i cannot solve
Problem when i rotate the capacitor connected to A0 the MaxHumidity Value changes as well
Problem when i rotate the capacitor connected to A0 the MaxTemperatur Value changes from 0 to 10 to 20 to 30 to 40 till 90 and then it increases by +1 till 49. Thats obviously not 50.
#include <dht11.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#define DHT11PIN 4
dht11 DHT11;
int Fanout = 2;
int Heat = 3;
void setup()
{
Serial.begin(9600);
pinMode(Fanout, OUTPUT);
pinMode(Heat, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
Serial.println();
int chk = DHT11.read(DHT11PIN);
//defining Pin 11 as DHT11 Datapin
int TempMax = analogRead(A0);
// read out Potentiometer Value and define as Variable
TempMax = map(TempMax, 0, 1023, 0, 50);
//remap Value to accomodate possible Min and Max Temperatur Values of DHT11
int HumMax = analogRead(A1);
// read out Potentiometer Value and define as Variable
HumMax = map(HumMax, 0, 1023, 20, 90);
//remap Value to accomodate possible Min and Max Humidity Values of DHT11
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity);
Serial.print("Temperature (C): ");
Serial.println((float)DHT11.temperature);
Serial.print("TempMax (C): ");
Serial.println((float)TempMax);
Serial.print("HumMax (%): ");
Serial.println((float)HumMax);
if (DHT11.humidity > HumMax) {digitalWrite(Fanout, HIGH);} //If the HumMax Value is exceeded the Fan will start to blow out the AIR
else {digitalWrite(Fanout, LOW);}
if (DHT11.temperature > TempMax) {digitalWrite(Heat, HIGH);} //If the TempMax Value is exceeded the Heating Relay will switch of
else {digitalWrite(Heat, LOW);}
lcd.setCursor(0, 0);
lcd.print("Te Hu MaxT MaxH");
//Setting the 4 Kategories on the LCD
lcd.setCursor(0, 1);
lcd.print(DHT11.temperature);
lcd.setCursor(3, 1);
lcd.print(DHT11.humidity);
lcd.setCursor(6, 1);
lcd.print(TempMax);
lcd.setCursor(11, 1);
lcd.print(HumMax);
delay(100);
}
Hey man. Thanx for your answer. Yes you are right. I mean a potentiometer. I made a Fritzing of my setup. I have not yet connected a 2. potentiometer for the Humidity Value because my "Starter Kit" came with just 2 potentiometer.
Thx! switching the pot with a resistor and the plugin the pot into the himudity analog pin helped and solved the problem of simultaniously increasing and decreaing MaxHumidity AND MaxTemperature.
The second problem still excists. If MaxTemperatur value falls und 10 it De-and increases in steps of 10. 10 20 30 40.
The third problem still excists. The Values stop at MaxTemperature 49 and MaxHumidity 88. They also stop at MaxTemperature 3 and MaxHumidity 21. MaxHumidity also kind of fluctates +-1. Is it due to insufficient accuracy of the pot? Maybe poor buildquality?
Problem when i rotate the capacitor connected to A0 the MaxTemperatur Value changes from 0 to 10 to 20 to 30 to 40 till 90 and then it increases by +1 till 49. Thats obviously not 50.
You have two issues with the temperature(and humidity as well). The first has been addressed by @pommel where you are not overwriting the previous value correctly. I always print spaces in the field where the data will go and then reposition the cursor back to the starting index and print the data.
Another issue with the lcd management is that any unchanging data like the headers, should be printed in setup() and not with each pass of the loop. It reduces the time spent printing to the lcd which can be important in many cases.
The issue with the failure to map to the maximum value relates to the math of the map function which returns an integer.
Work the math. If your analog read does not return the exact input max value, it won't map to the output max. For example if you have
TempMax = map(TempMax, 0, 1023, 0, 50) and the analog reading is 1022 you will map to 49, because of the integer truncation.
You can examine the analog reading value you see by printing out the unmapped raw values of the two analog readings. You might want to set the top end of the analog reading to be mapped as 1020.