I am attempting to write a program using the humidity/temp sensor. The objective is to have the program turn on(close relay) if less than 40% humidity on initial start up. If there is a sensor error at any time, leave relay open. Then I want to try and make a loop with the sensor reading to stay on if under 50% and turn off if over 50%. Then circle back to the under 40% again so the relay isn't constantly going on and off. I can't find anything similar to this anywhere to model off of. Can someone please help me. Here is what I have so far.
/*
int sensorPin = 2;
int relayPin = 9;
void setup()
{
Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
if( sensorValue <40 ) // Change the value as per your requirement
{
digitalWrite(relayPin, HIGH);
Serial.print("relay on");
Serial.println(sensorValue);
delay(100);
}
loop
if ( sensorValue <50 )
{
digitalWrite(relayPin, HIGH);
Serial.print("relay on");
Serial.println(sensorValue);
delay(100);
}
if( sensorValue >50 )
{ digitalWrite(relayPin, LOW);
Serial.print("relay off");
Serial.println(sensorValue);
else
{
digitalWrite(relayPin, LOW);
Serial.print("relay off");
Serial.println(sensorValue);
}
