New User Bipolar Stepper motor

Been working with 125G EXI Servo B1228 but to its limitations have switch to bipolar steppers...Using the experimenter board 28x2 chip along with motor driver L293D<ordered today from :http://www.hvwtech.com/products_view.asp?ProductID=341>

Unable to test the following code until driver arrives

Trying to move the stepper manual from right to left 1 step at a time
Code:
main: ‘ make a label called ‘main’
     

     if pinc.7 = 1  then  manualmove ‘ jump if the input is on
      
     goto main ‘ else loop back around
  
 manualmove: 'make a label called manualmove'
     
      pause 100 ‘short delay
                  
      if pinc.4 = 1  then  moveleft ‘ jump if the input is on
                  
      if pinc.3 = 1  then  moveright ‘ jump if the input is on
      
      if pinc.2 = 1  then  main ‘ jump if the input is on
       goto manualmove ‘ else loop back around
      
moveleft: 'make a label called moveleft

       pause 100 ‘short delay
             
       gosub lstep ‘ call left step sub-procedure
                     
lstep: let b1 = b1 + 1

       goto step2 ‘ goto the lookup table
       
       step2: let b1 = b1 & %00000011 ‘ mask lower two bits of b1
       
       goto manualmove
moveright: 'make a lael called moveright

        pause 100 ‘short delay
        
        gosub rstep ‘ call left step sub-procedure
         
        rstep: let b1 = b1 - 1
         
        goto step3 ‘ goto the lookup table
        
        step3: let b1 = b1 & %00000011 ‘ mask lower two bits of b1
        
        goto manualmove
Do you guys and gals think it will work
 
Last edited:

lbenson

Senior Member
I can't speak to whether your code will approximately do what you want it to, but I see two issues--one substantive and one having to do with coding in a way which makes it easier for others to understand, and, with practice, will make it easier for you to understand.

The substantive issue is that you have "gosub"s with no returns. These will probably not do what you expect, and will eventually cause stack errors. When you use a "gosub" the subroutine you call must end with a "return"--and execution continues after the gosub statement.

Regarding formatting and coding practice, one line of theory suggests that using "goto" leads to "spaghetti code", which goes here and there with no thread which is easy to follow. To avoid that you could use nested "if"s and gosubs as follows (but I may not understand the significance of your inputs).

Code:
main:
  do
    if pinc.7 = 1 then
      pause 100
      if pinc.4 = 1 then
        gosub moveleft
      endif
      if pinc.3 = 1 then
        gosub moveright
      endif
    endif
  loop

moveleft:
  pause 100
  b1 = b1 + 1 & %00000011
  return

moveright:
' similar to moveleft--add your own code here
  return
To make the code easier to understand, "blocks" (for instance, blocks of code following a label, blocks of code within a "do ... loop" statement, blocks of code within "if ... endif") are indented. On the forum, indentation is preserved if the practice is followed of putting code within the tags, &#91;code&#93;...&#91;/code&#93;
 
ty lbenson .....your points are well taken... Have added and changed around a few inputs...

next step is to try to record the stepper postion.....then to add a cycle start...

Code:
main:
  do
      if pinc.1 = 1 then 'move stepper left
        pause 2000
       gosub moveleft
      endif
    if pinc.2 = 1 then 'move stepper right
      pause 2000
       gosub moveright
      endif
    loop

moveleft:
  pause 1000
   b1 = b1 + 1 & %00000011
    if pinc.7 = 1 then 'learn mode
     pause 2000
      gosub learn
    endif 
  return

moveright:
 pause 1000
  b1 = b1 - 1 & %00000011
   pause 2000
    if pinc.7 = 1 then 'learn mode
     pause 2000
      gosub learn
    endif
  return
learn:
'add code here
   pause 2000
 return
 

lbenson

Senior Member
Much easier to follow (for me, at least). One point--the same level of indentation is used in any particular block of code--there is further indentation only with a new block (within "if", "do", "for", etc.).

Code:
main:
  do  ' in block1 and start of block2
    statement1-in-block2
    statement2-in-block2
    if --- then  ' still in block2
      statement1-in-block3
      gosub sub1  ' still in block3
      statement3-in-block3
      statement4-in-block3
    endif  ' end of block3, back in block2
    statement4-in-block2
  loop ' last statement of block1
 
Top