ADC Scaling issue

vk6bgn

New Member
Hello All,

I have a bit of a scaling problem. I am sure it's not that hard. I searched the forums last night for "scaling" and read about 15 pages of search results until midnight last night and toyed around with it a bit more today, but no luck. Possibly some of the math guru's can help.

This is the scaling for a 10K ohm potentiometer in my 28X2 chip project when the potentiomerter is fully CCW and CW. This scales a number from 6 to 85.
"1st scaled number"
ReadADC A.3, b0
w0 = b0 * 79 / 255 + 6

My problem is, I would like to scale the same potentiometer, but in the opposite direction from 42 to 4. (what do you call this... reverse scaling???)
"2nd scaled number"
ReadADC A.3, b2
w1 = b2 ??????

So.... when the 1st scaled number = 6, I'd like the second scaled number to be 42. And when the first scaled number = 85, I'd like the second scaled number to be 4.



Thanks in advance.
"The Addict"
 
Last edited:

AllyCat

Senior Member
Hi,

You can do the pot reversal simply with: b2 = 255 - b2 .

Then the scaling can be: w1 = {42 - 4} * b2 / 255 + 4 .

PICaxe Basic doesn't recognise the braces {} , but you can omit them, or (as for your first example) pre-calculate the enclosed value as a constant.

Then it can be a simple "one liner": w1 = 255 - b2 * 38 / 255 + 4 .

Cheers, Alan.
 

hippy

Technical Support
Staff member
One thing to bear in mind with scaling, for example converting 0-255 to 0-3 ...

w0 = adc * 3 / 255

That gives four possible results, 0, 1, 2 or 3, but will only deliver the 3 when the pot is hard up to its extreme and delivering 255 as a raw ADC reading. So that's pretty much a third of each pot movement giving 0, 1 and 2 rather than having each quarter giving 0, 1, 2 and 3.

It may not be important in most cases but it is worth bearing in mind.
 

hippy

Technical Support
Staff member
w0 = adc * 4 / 255 MAX 3
I believe that does do the job. And I think another alternative is -

w0 = adc * 4 / 256

The generic case for scaling 0..255 to MINIMUM..MAXIMUM would therefore be -

w0 = MAXIMUM - MINIMUM + 1 * adc / 256 + MINIMUM

Note that as long as the result is 255 or lower, a byte variable can be used to store the result.
 
Top