A quick summary to begin with:
I am programming a temperature/humidity sensor (DHT22), that code I got up and running but I wanted to implement something extra. I want indicate the right temprature with 3 leds, 1 blue led when it is too cold, 1 green led for the right temperature and 1 red led for when it is too hot. I made an if statement with the temperature values and added the leds to those values but when an led turned on it wont turn of, that is the problem at the moment. I am new to programming and cant find the solution on my own, I hope someone can help me. Thank you for reading!
Regards
Daniel
my code: #include <DHT.h> #define DHTTYPE DHT22 #define DHTPIN 2
Read the “How to use the forum” thread and follow directions in section #7 about posting code.
There are several things wrong with your program. I think the biggest problem is the way you use “else(LOW)” to set pin states. I’m pretty sure it doesn’t work that way.
If you are comparing a value against against multiple constants, you should start either with the largest constant or the lowest constant and use if, elseif and else so you don’t keep comparing the value once you’ve found a match.
You should also use constants instead of using pin numbers. The numbers you use to compare the temperature against should also be constants. This way it would be easy to change the pin assignments or use different temperature thresholds. You just change the number once at the top of the program instead of search through the code looking to see where each number is used.
Using good constant names also makes the code easier to read.
OK, so having appreciated it, do what he said. Read the instructions, then go back and modify your post to mark up the code as such so we can examine it conveniently and accurately.
UKHeliBob:
Using variables of the smallest applicable size is also good practice, as in
I don't know if the OP is still around but I appreciate the suggestion.
Thanks
Edit: I still agree it's a good idea to declare variables in the smallest applicable size, but I found it interesting when declaring a constant, the type of variable (at least byte and int) doesn't affect the program size.
I see it as declaring an entity, some are variable, some are constant.
I personally only use NAMES_IN_ALL_CAPITALS_WITH_UNDERSCORES only for #define,
const values get standard camelCaseNames, but that's a pure matter of taste.
I'm sure this is all old news but I'm relatively new to the Arduino so I was curious about the code size of the various constant/variable options.
#define and const both produce the same size of code. The variables size declaration doesn't matter when declaring a constant (at least not with byte and int).
The byte and the int constants took the same code space and variable space.
byte and int made a difference when the value wasn't being declared as a constant.
Hi,
[SOAPBOX}
We didn't HELP the OP.
We solved his problem.
He was too quick.
Did he learn anything???
Should have given him link to Reference .. If else
Then let HIM do a rewrite.
[/SOAPBOX]
UKHeliBob:
I have always found the notion of declaring a variable as a constant somewhat contradictory
That is because you see it as “declaring a variable”.
But it isn’t!
It is in fact, declaring a constant (a “named constant”). No-one ever said - the language never said - it was a variable. It is specified as a particular type of constant in the same way as is a variable, because it must have that characteristic when it is used.