In my Pool Heater Project I have a strange bug that requires me to read the one wire buss three times to measure my two temperature sensors. The code operates fine as written but I end up reading the second sensor twice to keep everything in sync. I can't figure out why this is.
Have you found the auto-format button in the IDE yet? (ctrl T) your code because if the indentation makes it hard for this noob to C+ very hard to read...
That having been said. Do you have a pull-up on the one wire bus line, recommended is 4K7 or lower depending on conditions and wire length?
Your code looks fine to me, except for one thing.
Short answer: add "break;" after "Temp18B20.reset_search();"
Long answer:
There is a problem with the OneWire library. But that's for an infinite loop, which you aren't running into.
Here's the fix for it anyway:
This is because the third "unneccesary" loop is necessary in order to hit the "Temp18B20.reset_search();" line.
In the example code, there is a "return;" after this line, but in your case you don't want to break out of the function, just the loop. Thus, replace "return;" with "break;"
A better way would be to loop twice (like you expected) and then add a "Temp18B20.reset_search();" at the end of your function (after all the calculations are done). This should also work, as long as your sensors are all functioning properly and you don't add any more sensors. Then you could have the fail safe conditional "if ( !Temp18B20.search(addr)) {" end with "return false;" so that if you get there (which you shouldn't if you're reading twice, and have two good sensors) you know something is wrong, and the function shuts down the pump.
Wow, That fixed it. I was looking for a good tutorial on the one wire .h code I used but did not find it. So I was flying blind. Thank you again for your help. Ron