??? 09/08/08 13:58 Read: times Msg Score: +1 +1 Informative |
#158067 - A detail Responding to: ???'s previous message |
Chris said:
I can live with rounding as long as its close to those limits.....even the original function wasn't perfect in the sense that it never makes it to the highest limit......i.e. limits of 20-40 would yield 20-39 so I simply added 1 to the upper limit to get my 40....... Christoph said:
That sounds like the rounding was left out at some point during the calculations. The real reason that Chris's original function never made it to the upper limit is that the calculation is incorrect. Forgetting about word lengths and rounding and such for the moment, it's important to remember that the range of pot_level is 0-255, not 0-256. Therefore, the correct formula for scaling the input from the pot is HC_GAIN_LOW + ((HC_GAIN_HIGH - HC_GAIN_LOW) * pot_level / 255) and not HC_GAIN_LOW + ((HC_GAIN_HIGH - HC_GAIN_LOW) * pot_level / 256) Christoph's suggestion to round the results of the division would appear to fix the problem, as did Chris's patch of adding one to the upper limit. And either of these approaches might give results that are close enough, especially since Chris is not looking for a perfect solution. But neither is strictly correct as long as you're (effectively) dividing by 256 instead of 255. -- Russ |
Topic | Author | Date |
Scale offset using ints by byte position location | 01/01/70 00:00 | |
Does type casting make sense here?... | 01/01/70 00:00 | |
Show us more......... | 01/01/70 00:00 | |
Here are defs and original function | 01/01/70 00:00 | |
ints, at leastin Keil, at 16 bits wide. | 01/01/70 00:00 | |
Christoph, | 01/01/70 00:00 | |
That makes things clearer. | 01/01/70 00:00 | |
A detail | 01/01/70 00:00 | |
Take a look at the ranges | 01/01/70 00:00 | |
Brett, that is helpful, I should add.... | 01/01/70 00:00 | |
No floating-point math required. | 01/01/70 00:00 | |
Its working but I have a question on theory | 01/01/70 00:00 | |
Rounding instead of truncate | 01/01/70 00:00 | |
oh, that is interesting | 01/01/70 00:00 | |
Russ' comment is right, though.![]() | 01/01/70 00:00 |