I’m not getting the expected output. I’m sure it’s a syntax or typing problem, but I can’t figure it out. This is my function. It oversamples an diode-type temperature sensor.
This returns bizzare numbers. When I take the serial output that I get from all the debug messages, and put it in a spreadsheet, I get the correct numbers. There must be a problem with the line
That's probably it. But I don't understand why. My variable 'temp' is a float. My variable 'steps' is an int. So if I multiply a bunch of ints together, does it blow up even if the thing I'm setting them equal to is long enough to contain the answer?
Since 5, steps, and 100 are all integers it figures you want an integer answer. If the two sides of an operation are of different sizes it will promote the smaller one to match the larger one and the result will be of the larger type. It would probably work if you only changed 5 to 5L OR changes 'steps' to a long.
When you do an assignment it evaluates the right side of the assignment and then converts to the type of the left side. That's why an integer division assigned to a float gives an integer answer. If either side of the division was a float (or cast as a float) you would get a float result:
BetterSense:
There must be a problem with the line
temp=(5*steps*100)/(4096)) + offset);
As already pointed out, you can cast this to float to solve your problem - you can also combine all your constants into one float to make your math faster:
temp = ((5*100*steps)/4096) + offset);
becomes
temp = (500.0/4096.0) * steps + offset);
becomes
temp=0.1220703125 * steps + offset;
Now one floating point multiply rather than some multiplies and a floating point divide, which is much faster.