help with Scaling
jason0627
Posts: 17
Hello i am having difficulty understanding scaling. I am going through the book what's a micro controller and came to this issue.
so i set up a circuit with a 10k pot. my task is to control a servo with the pot. I have gone through setting up a capacitor with the pot and servo and i understand that.
the book has a range of 1 to 691 (range being the value when pot is all the way to left. and value all the way to right)
now we get into the math for scaling (area of my confusion)
they want to scale the range from 500 - 1000 since this is going to be used to control a servo.
the max time is given as this 691 x (500/691) = 691 x .724 = 500
min time is 1 x (500/691) = .724
since the values are now scaled we add the offset
max time 500 +500 = 1000
min time .724 + 500 = 500
i get this and follow but the next step is this
take the fractional value that you want to use and multiply it by 256
.724 * 256 = 185.34 (185 rounded.)
my question is where did this 256 come from. I don't understand it is it always going to be 256?
thanks for looking at my problem. I tried to make it clear but if not i provided a link to the book if you need clarification.
Page 158 http://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
so i set up a circuit with a 10k pot. my task is to control a servo with the pot. I have gone through setting up a capacitor with the pot and servo and i understand that.
the book has a range of 1 to 691 (range being the value when pot is all the way to left. and value all the way to right)
now we get into the math for scaling (area of my confusion)
they want to scale the range from 500 - 1000 since this is going to be used to control a servo.
the max time is given as this 691 x (500/691) = 691 x .724 = 500
min time is 1 x (500/691) = .724
since the values are now scaled we add the offset
max time 500 +500 = 1000
min time .724 + 500 = 500
i get this and follow but the next step is this
take the fractional value that you want to use and multiply it by 256
.724 * 256 = 185.34 (185 rounded.)
my question is where did this 256 come from. I don't understand it is it always going to be 256?
thanks for looking at my problem. I tried to make it clear but if not i provided a link to the book if you need clarification.
Page 158 http://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
Comments
You are scaling it to a byte which is written in binary as 11111111. When you convert to decimal you get 255. The 0 makes it 256.
Mybad, I did not mean a byte, I menat a word
Mystillbad, a byte is 8 digits long. I gave up booze for the New Year but it is still not helping.
This is one of those moments when you ask a question and the correct answer makes you realize you have a lot more to learn.
I remember doing this when I went through WAM but have missled you, The correct answer is:
The Multiply Middle operator (*/) multiplies variables and/or constants, returning the middle 16 bits of the 32-bit result. This has the effect of multiplying a value by a whole number and a fraction. The whole number is the upper byte of the multiplier (0 to 255 whole units) and the fraction is the lower byte of the multiplier (0 to 255 units of 1/256 each). The */ (star-slash) operator gives you an excellent workaround for the BASIC Stamp's integer-only math. Suppose you want to multiply a value by 1.5. The whole number, and therefore the upper byte of the multiplier, would be 1, and the lower byte (fractional part) would be 128, since 128/256 = 0.5. It may be clearer to express the */ multiplier in hex—as $0180—since hex keeps the contents of the upper and lower bytes separate.
I looked it up in the Basic Stamp Help that is on the editor.
You need the right math to proportionally convert a (number X in the range 1-691) to a (number Y in the range 500-1000) to drive a servo. For a linear solution y=mx+b, you suspect the formula is along the lines of: Y= (C * X) + 500, where C is a constant, the ratio of the (range of Y)/(range of X). You have already determined this to be 500/690, or 0.725 (rounding up). So the formula is Y= (0.725 X) + 500
You could almost multiply X by 725 then divide by 1000 to get 0.725 X, but when using integer math, you must be careful not to let any result go over 65535 (AKA overflow), and obviously fractions are lost. At the high end, 691 x 725 will clearly exceed 65535. So, a workaround is to do the math in two smaller steps and add them together, then add the final 500.
Y= (X * 7/10) + (X * 25/1000) + 500