Code not acting as expected

Pauly1980

New Member
Hi all,
I wrote the attached code to output a servo position to a BLDC motor controller with a soft start/stop rate.
It takes a few samples from a hall effect sensor and averages them, zeros them out and multiplies them to get a 75 to 225 value for the servo position.
The funny thing is the ramp rate works but when it hits the final value it goes back to 75 and starts ramping up again. However if I comment out the elseif that goes to the subtract routine it stops at the final value but I can never decrease the output.
Any ideas???
Also sorry if my code is not the best written in the world.
Code:
;C.4 hall effect sensor
;C.1 Servo out
init:
servo C.1,225
wait 2
servo C.1,75
main:

    let b20=50 ;sets the soft start ramp rate
    let b21=15 ;sets the soft stop ramp rate
    let b12=b13
    pause 5
    readadc C.4,b0 ;reads the hall effect adc
    pause 5 ;waits 5ms before taking the next input
    readadc C.4,b1
    pause 5
    readadc C.4,b2
    pause 5
    readadc C.4,b3
    pause 5
    readadc C.4,b4
    pause 5
    readadc C.4,b5
    pause 5
    readadc C.4,b6
    pause 5
    readadc C.4,b7
let b8=b0+b1+b2+b3+b4+b5+b6+b7/8 min 128        ;takes an average input over the last 40mS, limits returned value to 128 so as to stop maths overrun
let b10=b8-128 ;takes the min input of hall effect switch and zeroes it
let b11=b10*12+75 min 75 max 225 ;increases the value returned due to low sensitivity
if b11>b12 then let b13=b11/b20+b12 min 75 max 225 ;implements the soft start ramp rate
elseif b11=b12 then goto main
elseif b11<b12 then gosub subtract
endif
debug
servoout:
servopos C.1,b13
goto main
subtract:
let b14=b11*10/b21
let b13=b12-b14 min 75 max 225 ;implements the soft stop ramp rate
return
 
Last edited:

Aries

New Member
Try putting this into the PE6 simulator, giving b0 to b7 suitable values and trace what happens.
 
Top