I am very new to coding and apologies in advance if my inexperience shine through, I have been searching the forum to try and find a solution to my issue.
I am build a since project with my daughter and we are basically trying to get a LED to turn on and get brighter when the room temperature gets above a set temp (with a tolerance). We are just starting the project and can understand that we might not get some good indication from the change in an LED brightness. We are using a MAX6675 to read a K-type thermocouple. We had the temperature reading on the serial monitor which was the first step. We then added a for statement to say
Room temp is >= to set temp plus the tolerance temp
LED was less than 255
Brightness to step up but 10
but when adding the "for" section, it all just stops working, not sure why.
any help would be amazing, even just to steer us in the right direction.
Thanks for you time
#include <max6675.h>
//thermocouple//
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
//delay time//
int dt = 500;
// temperature settings//
float setTemp = 33.5;
float tolTemp = 2.5;
//LED//
int redLED = 9;
void setup() {
Serial.begin(9600);
delay(dt);
int brightness = 0;
}
void loop() {
//Temperature reading//
Serial.print("Room temperature = ");
Serial.print(thermocouple.readCelsius());
Serial.println("°C");
//LED control//
float roomTemp = thermocouple.readCelsius();
for (roomTemp >= (setTemp + tolTemp); redLED <=255; brightness + 10) {
analogWrite(redLED,brightness);
}
delay(dt);
}
"brightness" ONLY exists in setup(). You should be seeing compile warning when you try to use it again in loop().
Fix by making int brightness a global variable just like your other variables.
The loop will run while redLED is smaller or equal to 255 but it has a fixed value of 9 so the test will always be true. Add to that the fact that no variables change in the for loop because brightness + 10 does not add 10 to the value of brightness means that it will not end and in any case, exit from the loop is not dependant on the value of brightness anyway
Please explain in English what you want the for loop to do
Adding to what @Paul_KD7HB said, if you name your variables with "pin" such as redLEDpin it is much, much more obvious that the variable refers to an actual pin number which is something that doesn't change. It is not the value you read from the pin or write to the pin, but the pin itself. The thing that is changing is the brightness so that should be your variable in your for() loop.
Yes, I can imagine, I have zero experience in coding. We built this from watching YouTube clips and looking on the forum.
I guess the sketch is only half complete but I thought it might at least start. It would need the other part to turn the LED down/off again with the temp but stopped when it didn't work at all.
Basically we wanted to turn a fan on when the room temp increased but more so speed up the fan, having the speed increasing as the temperature increased further from the set point. I thought it would be easier to start simple with an LED.
The set temp was to be the temp when the light would come on dimly and then get brighter as the current temp gets higher above the set temp. We need to also add the reverse as I mentioned with the light getting softer as the current temp gets closer to the set temp and turning off if it goes below.
Hope that explains the idea a bit better and thank you if you can help with the coding.
Thank you very much for looking at what we have done and proving a solution, I will make the changes (in the morning, been up late trying to get it to work) and hopefully we get it to work and then add the reverse to get the LED to fade if temp goes down.
but the code always fades from 0 to whatever upper limit is calculated. If the temperature goes down, the next time through loop() it will again start at 0 (off) and fade up.
If you want it to be a steady value and then fade up/down depending on the current temperature v. the previous temperature, that's different code.