Help with maths how to change a incremental number into a decremental number


New Member
Hi can any one help I am trying to make a distance sensor using a hall effect sensor. What I want to end up with is a sensor that can detect a magnetic field and slow a motor using pwm as it approaches the field. The problem is the sensor only gives an increasing posative number as it approaches or leaves the field. I have tried breaking the number into two words bo and b1 then using bo as the control of the pwm but the sensor is then not sensitive enough. I have also tried if statements ( if the reading is this then pwm output fast or slow) but this doesn't work well either. What I'm thinking is to change the posative going number into a direct opposite then feed that Into the pwm control can anyone help with the maths?


Senior Member
It would help to know what range of numbers you're looking at, and to see the code you have that doesn't do what you want it to.

One easy way to invert your values would be to subtract the number you have from the possible maximum value of a variable (255 for a byte and 65535 for a word). So if your reading is in W0, you could do W1 = 65535-W0. Then as W0 increased, W1 would decrease.

You may want to do some scaling to get values within a range that is convenient to use, but that would require that you provide more information on the use of this "decremental number".


New Member
Thank you i'll post what i have but its not much at the moment. The main problem is usualy I would place the sensor between pos and ref resistor to neg to get the result but I am using a hall sensor so the output is pos and goes neg as a magnet aproches. I'll post the values also.
Thank you for your help


New Member
Pic axe 14M2 hall effect OH49E. Working voltage 5 volts motor connected to picaxe through 1 x 2n7000 mosfet. It is hoped the picaxe will also be able to operate as an ir reciever to flash some leds and operate a relay to reverse the motor. I belive this may be too much and will have to be devided in to two chips.


Senior Member
It's better to post your code as text rather than as a pdf file, which takes several steps to view. Place within the forum editing tags, "[ code]" and "[ /code]" (omitting the spaces I put in to keep the forum from acting on those tags), like this:
#picaxe 14M2
setfreq m4 ; All M2 parts internal k31,
; k250, k500, m1, m2, m4, m8,m16,m32
symbol speed_control = C.0
symbol val = w0 ; word (16-bit) user variable

symbol pwmPin = B.2
b10 = 255
b12 = 155
b14 = 100
b16 = 50
b18 = 10
b20 = 0
pwmout pwmdiv4, pwmPin, 24, 50

readadc10 speed_control, val ; read 10-bit ADC into variable w0
if val < b10 then let b22 = b10
if val < b14 then let b22 = b12
if val < b15 then let b22 = b14
if val < b18 then let b22 = b20
end if
end if
end if
end if

pwmduty pwmPin, b22 ; set pwm duty
goto main ; loop back to star
Several points. There's nothing invalid in the use of "GOTO main:", especially in a program this small, but as a recent poster said, GOTO is never needed, and often leads to problems. It's more conventional in modern basic to put everything in main: within a DO LOOP construct.

Your variables b10, b12, b14, b16, b18, b20 appear to be constants. Unless you intend for other code to change them, you should define them as such, e.g., "symbol LIMIT1 155" (if you need to define them at all).

Your series of "IF ... ENDIF" statements is confusing. It would probably be better written with a SELECT CASE statement:

  select val
    case < 10: b22=0
    case < 50: b22=100
    case < 100: b22=155
    else: b22=255
(If I got that right.)
And it is best to define all your "b" variables as you have defined "val", e.g. "symbol b22=pwmVal".

Good luck, try again, test code in the PE6 simulator, and get back to us with additional questions.