Test circuit for MD20A with feedback pot

Gramps

Senior Member
The objective here was to get the motor to stop when b1 and b3 are equal. However the motor does not stop as desired.
Obviously, I'm missing something. Hint please!
Gramps
Code:
'Test circuit for MD20A with feedback pot
'Adapting Erco's code
' C.5 goes to center wiper on desired pos. pot
' C.7 goes to center wiper on feedback pot
'C.4 connects to direction pin on MD20A
'C.2 connects to PWM pin on MD20A
'pwmduty is 0 to 1023
#picaxe 28x2
#no_data

init: pwmout C.2, 221, 177 ' 9000Hz at 20% @ 8MHz  start pwm peroid is
'0-255 and duty cycles is 0-1023
main:
readadc 17, B1 ' read desired pos. pot, C.5 store ADC value 0-255 in B1
'sertxd (#b1," ",#w2,13,10)
debug
readadc 19, B6 ' read feedback pot, C.7 store ADC value 0-255 in B6
;sertxd (#b6," ",#w3,13,10)
debug
if b1<128 then reeverse

foreward: high c.4: b2=b1-128: goto speed
reeverse: low c.4: b2=128-b1

speed: w2=8*b2
pwmduty C.2,w2 ; set pwm duty
'sertxd (#b1," ",#w2,13,10)
debug
if b1=b3 then pwmout C.2, 199,0 '0% duty cycle turn off motor
endif
goto main ; loop back to start
 

Buzby

Senior Member
What exactly does the motor do ?.

You are reading the feedback pot into B6, but the test for stopping uses B3.,

It is much better to start using symbols. They make programming so more clearer, and debugging so much easier .

Cheers,

Buzby
 

AllyCat

Senior Member
Hi,

Code:
 .....
debug
if b1=b3 then
Very probably the issue is in the above. DEBUG is a very "slow" executing instruction and best avoided in any "timing critical" code. SERTXD( [of the required data byte(s)]...... is better, but that may need to be avoided (commented out) sometimes. However, even without that delay, the value of B1 is being read quite infrequently, so the value could be completely jumping over the equality so B1 = B3 might not be encountered.

You may need to read B1 more frequently (preferably immediately before the IF b1 = b3.... ) and use >= or <= (depending on the direction of travel) not just a simple =.

Also, you could use PWMOUT... 0 , PWMDUTY.... 0 or PWM ... OFF commands to stop the PWM and might need to decide which is the most appropriate in this case.

Cheers, Alan.
 

AllyCat

Senior Member
Hi,
Must we use 2 if statements? >= and <= ?
Yes, but only the "correct" test (i.e. to catch the overshoot) for the direction the motor is moving. Probably the easiest modification is to copy the last section of your code and paste it into the space between the "reeverse:" and "speed:" labels (with the appropriate => or =< in each section). The code can be tidied up or simplified later.
Code:
foreward: high c.4 : b2=b1-128 : goto speed
reeverse: low c.4: b2=128-b1

pwmduty C.2,w2      ; set pwm duty
' sertxd (#b1," rev ",#w2,13,10)
if b1 => b3 then pwmout C.2, 199,0      '0% duty cycle turn off motor
endif
goto main     ; loop back to start

speed: w2=8*b2
pwmduty C.2,w2      ; set pwm duty
' sertxd (#b1," fwd ",#w2,13,10)
if b1 =< b3 then pwmout C.2, 199,0      '0% duty cycle turn off motor
endif
goto main     ; loop back to start
Easiest is just to use trial and error to determine where to put the => and the =< . ;)

Cheers, Alan.
 

Gramps

Senior Member
paste it into the space between the "reeverse:" and "speed:" labels (with the appropriate => or =< in each section).
I didn't realize the place where the command is in the code would make a difference.
Hope to try your suggestion soon thanks Gramps
 

Gramps

Senior Member
Had a few minutes this afternoon on a rainy Saturday to try to get this code to work.

use trial and error
Here are the results and code so far:
The "desired pot" nicely controls the motor.
The "feedback pot" is supposed to stop the motor when it's value reaches the "desired pot" value.
However it does not effect the motor's operation at all.
Code:
'Test circuit for MD20A with feedback pot
'Adapting Erco's code
'C.5 goes to center wiper on desired pos. pot
'C.7 goes to center wiper on feedback pot
'C.4 connects to direction pin on MD20A
'C.2 connects to PWM pin on MD20A pwmduty is 0 to 1023
#picaxe 28x2
#no_data
#no_table
init:
    pwmout C.2, 221, 177 'start pwm period is 0-255 and duty cycles is 0-1023
main:
    readadc 17, B1 ' read desired pos. pot, C.5 store ADC value 0-255 in B1
    sertxd (#b1," ",#w2,13,10)
    pause 500
    readadc 19, B6 ' read feedback pot, C.7 store ADC value 0-255 in B6
    sertxd (#b6," ",#w3,13,10)
    pause 500
    if b1<128 then reeverse

foreward: high c.4: b2=b1-128: goto speed
    if b1<=b6 then pwmout C.2, 199,0 '0% duty cycle turn off motor
    endif
    if b1>=b6 then pwmout C.2, 199,0 '0% duty cycle turn off motor
    endif
reeverse: low c.4: b2=128-b1

speed: w2=8*b2
    pwmduty C.2,w2 ; set pwm duty
    sertxd (#b1," ",#w2,13,10)
goto main ; loop back to start
 

AllyCat

Senior Member
Hi,

No, you need only one test in the "forward" path and the other in the "reeverse" path. To keep the modification simple I've retained the two paths (i.e cut and pasted from the original "speed:" label) right through to the "loop back to the start (main)". So I think the last section of code should be:
Code:
   if b1<128 then goto reeverse      ; Choose either the forward or reeverse path
foreward:
   high c.4 : b2=b1-128        ; Now Fall into the original "speed" section
   w2=8*b2
   pwmduty C.2,w2              ; set pwm duty
'  sertxd (#b1," ",#w2,13,10)
   debug
   if b1 =< b3 then pwmout C.2, 199,0      ' 0% duty cycle turn off motor
   endif
   goto main        ; loop back to start (End of "foreward" path)
reeverse:
   low c.4 : b2=128-b1
   w2=8*b2
   pwmduty C.2,w2 ; set pwm duty
' sertxd (#b1," ",#w2,13,10)
   debug
   if b1 => b3 then pwmout C.2, 199,0        '0% duty cycle turn off motor
   endif
   goto main           ; loop back to start (End of "reeverse" path)
I've also moved a section of the "forward" code, to avoid an unnecessary jump to "speed:"

Cheers, Alan.
 

Gramps

Senior Member
Thank you! The feedback pot is now stopping the motor when both pots are the same value.
In between forward and reverse there is a dead space. As we approach the dead space the motor keeps moving forward (or reverse) in little bumps. What would cause that? How to correct it?
 

AllyCat

Senior Member
Hi,

You (still) have enormous time delays in the loop, either the DEBUGs or (now) those PAUSE 500s. They could cause all kinds of instability in the loop.

However, you have said that B1 (or b1, the same thing) is the "Desired Position", but the program is using that to determine the direction of movement (by the line: if b1<128 then reeverse ) . The motor direction (and speed) should be determined by the difference (i.e. subtraction) between the "actual" and "required" positions. Thus, I would have expected those 128s in the code to be b3. Or is it w3 or b6, because there seems considerable confusion in the code (note that b6 is one byte within w3) ? You really must start using SYMBOLs.

The code requires b2 (the desired motor speed) to be a "positive signed" byte (i.e. 0 to 127), so you may need to experiment whether to subtract b1 from b3 or b3 from b1 in the two paths (which will be opposite). To avoid confusing overflow errors, it also would be worthwhile to change the w2 (PWM duty cycle) calculations to w2 = 8 * b2 MAX 1023 .

Cheers, Alan.
 

Gramps

Senior Member
The motor direction (and speed) should be determined by the difference (i.e. subtraction) between the "actual" and "required" positions.
YES!
Do we even need if b1<128 then reeverse ?

Can we do like this?
IF the desired pot is GREATER then the feedback pot THEN FORWARD
IF the desired pot is LESS then the feedback pot THEN REEVERSE
IF the desired pot is = to the feedback pot THEN STOP
 

AllyCat

Senior Member
Hi,

No, you don't need the <128 . I guess it was an attempt to use b1 as a signed value.

Yes, that Pseudo-code should work. You don't even need the "=" test; if the "desired" is neither < nor > than "feedback", then it must be = so you can simply "fall through" into the STOP routine. You might need to swap the < and > (or the forward and reeverse labels) to make the motor turn in the correct direction.

Cheers, Alan.
 

Gramps

Senior Member
OK starting over with all new code.
Not sure that "Symbols" are correct.

'Shadow-Bot feedback control Pseudo-code
Code:
#picaxe 28x2
#no_data
#no_table

Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Turn on the Motor Controlor
Symbol  FORWARD_ = B.7 'Motor CW
Symbol  REVERSE_ = B.7 'Motor CCW
Symbol STOP_ = B.0 'Motor stop

INIT: pwmout B.0, 221, 177 'Turn on the Motor Controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b6," ",#w3,13,10)
    
    IF desired_pot more then feedback_pot THEN FORWARD_

    IF desired_pot less then feedback_pot THEN REVERSE_

    IF the desired_pot is = to feedback_pot THEN STOP_
    
    'NOTE: Limit of range 

    IF the feedback_pot is GREATER then [number] THEN STOP_

    IF the feedback_pot is LESS then [number] THEN STOP_

GoTo Main
 
'NOTE Correct PWM lines as: 
if b1 =< b3 then
:pwmout C.2, 199,0 
:endif
 

Gramps

Senior Member
yes, I thought that we need two symbols for high and low on B.7.
Just label it Direction and call the state of the pin in the code?
 

lbenson

Senior Member
I hadn't tried to follow the logic of the code. In the pseudo-code, "FORWARD_" stands for "do something which causes the motor to step forward", "REVERSE_" stands for "do something which causes the motor to step backwards", and "STOP_" stands for "do something which causes the motor to stop". Just a pin number by itself cannot cause an action (as you would see if you tried the code in the simulator).

The syntactic logic of IF ... THEN SOMELABEL requires that whatever bare value (not a command) follows "THEN" must be a label, and "GOTO" is implied. So you have to write the routines "FORWARD_:", etc. They are not symbolic names for a pin.
 

Gramps

Senior Member
First must get the symbols correct.

The syntactic logic of IF ... THEN SOMELABEL requires that whatever bare value (not a command) follows "
Like This?
desired_pot (value) is > (greater then ) feedback_pot (value) THEN Forward
Code:
if b1 =>b3 then 'IF desired_pot (value) is > (greater then ) feedback_pot (value)
pwmout C.2, 199,0 'turn off PWM ' THEN Forward
endif
 

Gramps

Senior Member
Thus far..........
Code:
'Shadow-Bot feedback control draft
'[code]
#picaxe 28x2
#no_data
#no_table

Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM

INIT: pwmout B.0, 221, 177 'Energize Motor controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b6," ",#w3,13,10)
    
    IF b1 => b3 then 'desired_pot value is greater then feedback_pot value
    low B.7 
    pwmout B.0, 221, 177' rotate Forward_
    endif
    IF b1 =< b3 then 'desired_pot value is less then feedback_pot value
    high B.7
    pwmout B.0, 221, 177' rotate Reverse_
    endif
 

lbenson

Senior Member
That looks syntactically better, but use your symbols:
symbol desired_pot_value=b1
symbol feedback_pot_value=b3
. . .
if desired_pot_value > feedback_pot_value then

It's good practice to show your program's "block" structure with indentation. For instance, commands within IF...ENDIF, DO...LOOP, FOR...NEXT make up a block (which may have within it other block structures with further indentation), so:
Code:
if  desired_pot_value > feedback_pot_value then
  low B.7 
  pwmout B.0, 221, 177' rotate Forward_
endif
(I use 2 spaces for each level of block indentation--other people use 3, 4, 8 or some other number which suits them.)

Now, B.0 already has a symbolic name, Motor, so you should use that. And B.7 should have a symbolic name, so
symbol direction=B.7
Code:
if  desired_pot_value > feedback_pot_value then
  low direction ' forward
  pwmout motor, 221, 177 ' rotate Forward_
endif
And so for the Reverse also.

Note that if you want to stop when desired_pot_value = feedback_pot_value, you have to code for that also (and change the "=>" and "=<" in your other comparisons to omit the "=" (since otherwise you will never get to your "desired_pot_value = feedback_pot_value" condition, or perhaps more accurately, you will perhaps already have moved past your stop point when you issue the stop command--with that in mind, you might need to put the stop condition first--but depending on motor speed you may end up "hunting"--going back and forth past the stop points seeking the exact ADC reading).

Try all that and see what other issues arise.
 
Last edited:

Gramps

Senior Member
Corrected I think!
Code:
'Shadow-Bot feedback control code
#picaxe 28x2
#no_data
#no_table
Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7

INIT: pwmout MOTOR, 199, 399 'Energize Motor controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b3," ",#w3,13,10)
    
    if  desired_pot_value = feedback_pot_value then
      pwmout MOTOR, 199, 0' STOP
    endif
    
    if  desired_pot_value > feedback_pot_value then
      high Direction
      pwmout MOTOR, 199, 399' rotate Forward
    endif
    
    if  desired_pot_value < feedback_pot_value then
      low Direction 
      pwmout MOTOR, 199, 399' rotate Reverse
    endif

goto main
When the two pots are EXACTLY lined up the motor controller stops.
Otherwise it "hunts".
We would like a little more latitude.
Can we do a "more or less 10 "??
 

lbenson

Senior Member
(This refers to post 19)
Excellent, but it would seem that in the case where B1=B3 (desired_pot_value > feedback_pot_value) the first if statement with "=>" will cause the motor to be set to go forward and then the second IF statement with "=<" will immediately cause it to go into reverse--with no STOP condition.
 

lbenson

Senior Member
Re post 22, Excellent indeed.
Yes, you can set up a band. You have to address the possibility that desired_pot_value may be either greater or less than feedback_pot_value. I generally handle this in a manner that may be clunky and wordy, but which makes it clear what is happening, e.g.:
Code:
symbol diff=b4

if desired_pot_value > feedback_pot_value then
  diff=desired_pot_value - feedback_pot_value
else
  diff=feedback_pot_value - desired_pot_value
endif
if diff < 6 then ' from 5 below a match to 5 above
   pwmout MOTOR, 199, 0' STOP
endif
 

Gramps

Senior Member
Added Limit of motion.
Code:
'Shadow-Bot feedback control code
#picaxe 28x2
#no_data
#no_table
Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7

INIT: pwmout MOTOR, 199, 399 'Energize Motor controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b3," ",#w3,13,10)
    debug
    if  desired_pot_value = feedback_pot_value then
      pwmout MOTOR, 199, 0' STOP
    endif
   
    if  desired_pot_value > feedback_pot_value then
      high Direction
      pwmout MOTOR, 199, 399' rotate Forward
    endif
   
    if  desired_pot_value < feedback_pot_value then
      low Direction
      pwmout MOTOR, 199, 399' rotate Reverse
    endif
'Limit of movement
    if feedback_pot_value > 200 then
      pwmout MOTOR, 199, 0' STOP
      endif
    if feedback_pot_value < 70 then
      pwmout MOTOR, 199, 0' STOP
    endif
  
goto main
 

AllyCat

Senior Member
Hi,

Yes, that's looking much better, but you haven't got the b1 , b3 and w3 (?!) replaced by the symbols in the main code yet.

Yes, you could extend the "dead space" as Lance shows, but your earlier code calculated a "speed" value, to slow the motor down as the correct alignment was approached. IMHO that's a better solution, you just need to calculate a variable "PWM" value by subtracting the "desired" and "motor" positions to (always) give a positive value (in each appropriate path). To make the motor move fast until it's near to the correct position, you probably need to multiply the "difference" (speed) by quite a large number (i.e. more than the 8 in the earlier code), but set a maximum PWM value, e.g. difference * 20 MAX 399 (to use in the PWMOUT command), to be consistent with your present values.

Again, the "399" really should be symbolised (e.g. symbol MAXPWMVALUE = 399) so that it's easy to see and change its value. Similarly the "20" might be configured with a symbol LOOPGAIN = 20 to permit optimisation of any overshoot.
 

Gramps

Senior Member
Yes, you can set up a band.
Inserted your snipit but still hunting up and back. Changed the
if diff < 6 then
to 12 and 20 but still chattering. Inserted debug which shows the two pots are both steady on with numbers only 2 digits apart.
 

Gramps

Senior Member
you just need to calculate a variable "PWM" value by subtracting the "desired" and "motor" positions to (always) give a positive value (in each appropriate path). To make the motor move fast until it's near to the correct position, you probably need to multiply the "difference" (speed) by quite a large number (i.e. more than the 8 in the earlier code), but set a maximum PWM value, e.g. difference * 20 MAX 399 (to use in the PWMOUT command), to be consistent with your present values.
Sorry you lost me! Brain getting foggy. May have to get some :sleep:
 

lbenson

Senior Member
to 12 and 20 but still chattering. Inserted debug which shows the two pots are both steady on with numbers only 2 digits apart.
Provide current code. This should work. FORWARD or REVERSE should be executed ONLY if diff > 5, so

Code:
if diff < 6 then
   pwmout MOTOR, 199, 0' STOP
else
    if  desired_pot_value > feedback_pot_value then
      high Direction
      pwmout MOTOR, 199, 399' rotate Forward
    endif
   
    if  desired_pot_value < feedback_pot_value then
      low Direction
      pwmout MOTOR, 199, 399' rotate Reverse
endif
 

Gramps

Senior Member
Code:
'Shadow-Bot feedback control code
#picaxe 28x2
#no_data
#no_table
Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
Symbol MAXPWMVALUE = 399
Symbol diff=b4

INIT: pwmout MOTOR, 199, MAXPWMVALUE  'Energize Motor controller

Main:



    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b3," ",#w3,13,10)
    'debug
 
 if desired_pot_value > feedback_pot_value then
   diff=desired_pot_value - feedback_pot_value
   else
   diff=feedback_pot_value - desired_pot_value
   endif
if diff < 12 then ' from 5 below a match to 5 above
   pwmout MOTOR, end' STOP
    endif
   
   
    if  desired_pot_value > feedback_pot_value then
      high Direction
      pwmout MOTOR, 199, MAXPWMVALUE' rotate Forward
    endif
    
    if  desired_pot_value < feedback_pot_value then
      low Direction 
      pwmout MOTOR, 199, MAXPWMVALUE' rotate Reverse
    endif
'Limit of movement
    if feedback_pot_value > 200 then
      pwmout MOTOR, off' STOP
      endif
    if feedback_pot_value < 70 then
      pwmout MOTOR, off' STOP
    endif
   
goto main
 

AllyCat

Senior Member
Hi,
Sorry you lost me!
With the code that you've just posted, introduce a new variable symbol Speed = w5 (for example) and a constant symbol LOOPGAIN = 20 (as a starting guess). Then, in each path (instead of the if diff < 12 then .. ) calculate the required speed immediately after each calculation of the variable diff , for example Speed = diff * LOOPGAIN max MAXPWMVALUE . Then change all the (non-OFF) PWMOUT commands to PWMOUT MOTOR , 199 , Speed .

Also, you might find it better to move the "limits" section of the program to immediately after the READADCs and then looping each back to the start (with a GOTO main ) so that the remainder of the code is not executed. That should avoid the motor "creeping" a little on each execution of the main loop.

Cheers, Alan.
 

lbenson

Senior Member
Try this:
Code:
'Shadow-Bot feedback control code
#picaxe 28x2
#no_data
#no_table
Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol diff=b4
Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
Symbol MAXPWMVALUE = 399

INIT: pwmout MOTOR, 199, MAXPWMVALUE  'Energize Motor controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b3," ",#w3,13,10)
    'debug
 
if desired_pot_value > feedback_pot_value then
   diff=desired_pot_value - feedback_pot_value
else
   diff=feedback_pot_value - desired_pot_value
endif
if diff < 12 then ' from 5 below a match to 5 above
   pwmout MOTOR, OFF ' STOP
else
    if  desired_pot_value > feedback_pot_value then
      high Direction
      pwmout MOTOR, 199, MAXPWMVALUE' rotate Forward
    elseif  desired_pot_value < feedback_pot_value then
      low Direction 
      pwmout MOTOR, 199, MAXPWMVALUE' rotate Reverse
    endif
endif

'Limit of movement
#rem
    if feedback_pot_value > 200 then
      pwmout MOTOR, off' STOP
      endif
    if feedback_pot_value < 70 then
      pwmout MOTOR, off' STOP
    endif
#endrem   
goto main
 

lbenson

Senior Member
Because the program has 2 major states, motor on and motor off, and two sub-states, forward and reverse, I'd find this a good occasion to use two of the bit variables, bit0-bit7, which make up part of b0.

So "symbol bMotorState=bit0 ' 0=off,1=on" would define the state of the motor with 0 indicating off and 1 indicating on.
"symbol bReverseForward=bit1 ' 0=reverse,=forward" sets up bit 1 to indicate reverse (0) or forward (1).

Because it's possible that issuing pwmout many times a second, as the current program does, could cause glitchiness (I don't know for sure). This program only turns on (or off) PWM when it is not in the desired state.

Similarly for motor direction, this only issues the required HIGH or LOW on the direction pin when a change of direction is required.

(Note this code has been corrected to fix my error in copying pointed out by Gramps below.)
Code:
'Shadow-Bot feedback control code
#picaxe 28x2
#no_data
#no_table

symbol xb0 = b0 ' reserved for bit variables
symbol bMotorState=bit0     ' 0=off,1=on
symbol bReverseForward=bit1 ' 0=reverse,1=forward
symbol REVERSE_=0
symbol FORWARD_=1

Symbol desired_pot_value = b1
Symbol feedback_pot_value = b3
Symbol diff=b4
Symbol  desired_pot = 13 'B.5
Symbol  feedback_pot = 11'B.4
Symbol MOTOR = B.0' Energize PWM
Symbol Direction = B.7
Symbol MAXPWMVALUE = 399

'INIT: pwmout MOTOR, 199, MAXPWMVALUE  'Energize Motor controller

Main:

    Readadc Desired_pot, b1
    sertxd (#b1," ",#w2,13,10)

    Readadc feedback_pot, b3
    sertxd (#b3," ",#w3,13,10)
    'debug
 
if desired_pot_value > feedback_pot_value then
   diff=desired_pot_value - feedback_pot_value
else
   diff=feedback_pot_value - desired_pot_value
endif
if diff < 12 then ' from 5 below a match to 5 above
  if bMotorState=1 then
    pwmout MOTOR, OFF ' STOP
    bMotorState=0
  endif
else ' motor must be set for proper direction and then activated
  if  desired_pot_value > feedback_pot_value then
    if bReverseForward = REVERSE_ then
      high Direction ' go forward
      bReverseForward = FORWARD_
    endif
  elseif  desired_pot_value < feedback_pot_value then
    if bReverseForward = FORWARD_ then
      low Direction ' go in reverse
      bReverseForward = REVERSE_
    endif
  endif
  if bMotorState=0 then
    bMotorState=1
    pwmout MOTOR, 199, MAXPWMVALUE' rotate Reverse
  endif
endif

'Limit of movement
#rem
    if feedback_pot_value > 200 then
      pwmout MOTOR, off' STOP
      endif
    if feedback_pot_value < 70 then
      pwmout MOTOR, off' STOP
    endif
#endrem   
goto main
 
Last edited:

lbenson

Senior Member
xb0 is as intended, reserving a variable you won't use because the bits which make it up are being used.

xsymbol is a typo. Not sure how that happened since I cut and pasted it from the Program Editor where it had just passed syntax.
 

Gramps

Senior Member
and then looping each back to the start (with a GOTO main
Ok, I think I see what you mean. Too tired to work on it this evening.
DUMB Question, how does a GOTO main work? Why does the program bypass this command sometimes and sometimes it doesn't.
 

lbenson

Senior Member
In copying the program, I managed to replace this line with that "x" before the symbol definition:

symbol bReverseForward=bit1 ' 0=reverse,=forward

This should go after the symbol statement for bit0
 

lbenson

Senior Member
How does a GOTO main work? Why does the program bypass this command sometimes and sometimes it doesn't.
In what circumstances do you think this GOTO statement is not being executed (and what do you think is being executed instead)?
 

AllyCat

Senior Member
Hi,

If the program flow "encounters" a GOTO then it always will be executed. But if the "condition" in an IF ,,, THEN. command is not met (for example when NOT at an end-stop), then subsequent commands will be ignored until the ENDIF is reached (that's one reason for indenting all the lines before an ENDIF). So I was proposing to put a GOTO main immediately before the ENDIF (to avoid any later commands in the loop countermanding the PWM OFF).

Cheers, Alan.
 
Top