I'm new to Arduino and have been having success with the basics such as closing a relay within a programmed temperature range. However, I could use some help with stepping up my program language knowledge another level.
I would like to close relay #1 only while ascending through a set temperature range, and then, close relay #2 only while descending through the same set range.
I've spent a few hours experimenting but can't quite figure out the right language. Thank you in advance for the help.
At the very minimum you need to store a few temperature readings, so you know if the readings are increasing or decreasing. Obviously, they are increasing if the difference (temp_now - temp_last) is positive.
If you are considering a temperature range, then you may need to store whether the temperature has exceeded a minimum or maximum value in the recent past, in addition to whether it is increasing or decreasing.
Is your software setting the temperature or just measuring it?
I won't attempt to give you any real code since I can't test it without your hardware, but the basic logic for checking rising or falling temperature would be:
Read current temperature.
Old temp = current temp
Read current temp again
If current temp > old temp, temperature is rising
If current temp < old temp, temperature is falling
Start loop over.
In a real world application, you might want to subtract the two temps and do nothing if they are very close. Then take one action if you have a big-enough positive difference and another action if you have a big-enough negative difference.
You may also want a delay so you don't measure the current temperature immediately after making the two readings equal.
And, you may not want to update the old temp every time through the loop. ... Probably only after you've detected a rising or falling temperature and you're ready to "reset" and look for a new temp change.
The software is measuring the temperature. I have set the lower boundary and the upper boundary of my target temperature range.
It's been easy to get the compiler to activate (close) the relay when the temperature enters the range and then relax (open) the relay when the temperature is above or below the range. Of course, now I want two relays to act with different trends in temperature readings, one with ascending temp and the other with descending temp.
DVDdoug:
Read current temperature.
Old temp = current temp
Read current temp again
If current temp > old temp, temperature is rising
If current temp < old temp, temperature is falling
Start loop over.
This is along the line of what I think would work. I'm just not sure how to write it into code and where it would be in relation to the "digitalWrite" for each relay.
DVDdoug:
Read current temperature.
Old temp = current temp
Read current temp again
If current temp > old temp, temperature is rising
If current temp < old temp, temperature is falling
Start loop over.
Personally I'd be taking more than one sample and average over time. You'll find that your temperature sensor will fluctuate a bit and if you poll it too fast you'll end up wearing out your relays.
Depending on the response time you require I'd be taking a number of samples, storing them in an array and calculate a rate of change (ROC) figure. Then you can test that ROC figure against some thresholds to switch the relay on/off. You may also want to have a dead zone in the middle (ROC~=0) where no relays are on. Depends on the situation.
Edit: Test with LEDs not relays. Save wearing them out.
jfsprague:
I'm just not sure how to write it into code and where it would be in relation to the "digitalWrite" for each relay.
Make your best attempt and if it does not work post your code and we can try to help. Without seeing your code it is impossible to know what part of the problem is causing you trouble.
And please use the code button </> so your code looks like this and is easy to copy to a text editor