I am working with a piece of hardware that I'm communicating with VIA serial control using a proprietary programming language that looks like a very dumbed down version of C.
The device reports it's current volume when queried. The range is -60 to + 20. How do I scale that to a 0-255 range which goes up in increments of 3?
Can you also provide an example of another value and other scale i.e. -15 to 15 scaled to 0-165, etc
To scale a range x0..x1 to a new range y0..y1:
y = y0 + (y1 - y0) * (x - x0) / (x1 - x0)
So for your first example above, x0 = -60, x1 = 20, y0 = 0, y1 = 255:
y = 0 + (255 - 0) * (x - -60) / (20 - -60)
=> y = 255 * (x + 60) / 80
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With