Happy New Year! I'm looking for guidance on my first sketch. I want to operate relay with a maximum humidity setting. Obviously I need to write my own sketch's but honestly the "if" "else" loop is hard to understand taking a bit here from one tutorial and a bit from another and what the hell. Lol puff puff pass California!! 619
aikoaiko:
Happy New Year! I'm looking for guidance on my first sketch.
Great! Please post it in code tags so we can have a look.
But first, if-else is not a loop. There is no repetition in it.
The if-else construct is literally English.
if condition
{
do this
} else {
do this
}
ElCaron:
The if-else construct is literally English.
Yeah, that's not the one most people choke on. I wonder if
aikoaiko:
Lol puff puff pass California!! 619
has anything to do with it?
[chong]Whuuuutttt? You mean if (this thing is true) then do { that thing }? Wooooowwwweeee man but like what does "true" really mean you know? I mean truth is subjective riiiiiight? [/chong]
/* this is for the AI of the Vagabond Box. Max humidity will operate function of relay: */
//Initialize DHT11 and include library
#include <dht.h>
#define DHT11_PIN 8
//initialize relay
Int relay = 7;
void setup() {
//Set relay as output:
pinMode (relay, OUTPUT);
digitalWrite (relay, HIGH);
serial.begin (9600);
}
void loop () {
/* At this point I am totally lost. I feel like I should start a for loop to monitor the humidity, engaging relay when maximum value is reached followed by an else state to disengage relay. Honestly I am lost. Lol*/
}
What do you want your loop function to do over and over? One good option would be to have it
- Take a reading
- react to that reading
and that's about it.
So you need to find (1)the line of code that can get the reading from your sensor and store it into a variable and then you need (2) an if else statement. It's really pretty simple and if you'd do just a tiny little bit of searching about it you'd find that you are nowhere near the first person to want to do something like this and there are about a gillion tutorials out there on just this type of thing with temperature or humidity or whatever sensor you like.
The real secret to this stuff is to think out the logic in regular English (or whatever your mother tongue is) and THEN write code to accomplish that logic. Trying to think the logic through in terms of code (ie. "some kind of for loop with a monitor and a maximum whatsit") is a recipe for frustration.
And finally a hint. Remember, the loop function already loops and repeats. For what you have described there will be no for loops, while loops, or any other kind of looping structure.
//I think I got it
//Initialize DHT11 and include library:
#include <dht.h>
dht DHT;
#define DHT11_PIN8
//initialize relay
int relay = 7;
Void setup () {
//Set relay as output
pinMode (relay, OUTPUT);
digitalWrite (relay, HIGH);
serial.begin (9600);
}
void loop () {
int readData =
DHT.read22 (dataPin);
//reads data from sensor:
float h = DHT.humidity; //get values
serial.print ("humidity =");
serial.print (h);
serial.printIn ( "%");
if (humidity >= 22) {
digitalWrite (relay, HIGH);
}
else if (humidity <=20) {
digitalWrite (relay, LOW);
}
delay (2000);
}
// Hopefully this is correct?
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Compile your code and see what you get, you won't damage anything.
Don't scared of getting errors, we all get them..
Thanks .. Tom..
aikoaiko:
// Hopefully this is correct?
Does it do what you want it to do?
Thanks Tom!
Tommy
Delta_G:
Does it do what you want it to do?
I won't know till manana.
// Hopefully this is correct?
What do you think happens if humidity == 21?
jremington:
What do you think happens if humidity == 21?
EDITED: So as not to spoil @jremington's question for the OP.
jremington:
What do you think happens if humidity == 21?
That is hysteresis...
If at reset humidity is 21% then relay will be HIGH as set in void setup().
Tom...