Controlling a stepper motor

Hi!
Can someone help me or at least point me in the right direction.
Attached is a simple bit of code that I use to drive a stepper motor either clockwise or anticlockwise for a given number of steeps as determined by the sub routines ‘rotater’ and ‘rotatel’ which in itself works OK. But what if we start the motor rotating with the appropriate subroutine but instead of specifying a set number of steeps the motor rotates until another condition is met – say pin3 goes high or a set input pulse count is reached or some other controlling input. If this is possible, how do I write the code? Where do I start?
I’m using a 28X1 simply because that happened to be on the bread board from a previous project. The motor driver is a L293D.
 

Attachments

Thank you Stuart. It's going to take a bit of digesting, I need to get my head round it. Am I right in thinking you are using a key pad to input commands - that interests me. in the code listing you state you have left out the keypad routine do you think you could post it some time - just to help us rookies!

Thanks again

John
 

oracacle

Senior Member
If may also help of you could post some info on the motor. I will not be able to download your programme for a few days xue to work.
 
The motor is a Bipolar fitted with a 100:1 gearbox. The step angle is 15deg. At the moment I'm imputing on pins 0 or 1 and outputting to the motor driver on pins 4,5,6,7. I'll post a shot of the hook up if it will help?
 

bfgstew

Senior Member
Scanning routine for 4 x 3 matrix keypad.

Code:
GKP:
 	 Do
    		Gosub GTKP			' Wait until no key pressed
  			Loop Until key_value = 0
 	 Do
   		 Gosub GTKP			' Wait until key pressed
  			Loop Until key_value <> 0
  			If key_value = 11 Then
    			key_value = 0
  		End If
  	Return		
GTKP:	;Keypad scan coding
		key_pos = 0
		key_value = 0
	 	High ROW1 : gosub ScanCol : low ROW1
	 	High ROW2 : gosub ScanCol : low ROW2
	 	High ROW3 : gosub ScanCol : low ROW3 
	 	High ROW4 : gosub ScanCol : low ROW4
	 	Return	 	
	 	ScanCol:
	 		if COL1 = 1 then : key_value = key_pos + 1 : endif
	 		if COL2 = 1 then : key_value = key_pos + 2 : endif
	 		if COL3 = 1 then : key_value = key_pos + 3 : endif
	 		key_pos = key_pos + 3   
   		return
 

oracacle

Senior Member
hey there, finally got some time to have a look at your code, there seem to be some missing, the 2 gosub rotater and rotatel

this is what you posted

Code:
[color=Black]Main:[/color]

[color=Purple]pins [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]if  [/color][color=Purple]pin0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then  gosub [/color][color=Black]rotater:[/color]
[color=Blue]if  [/color][color=Purple]pin1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then  gosub [/color][color=Black]rotatel:[/color]

[color=Blue]goto [/color][color=Black]main:



inch15r: 
      [/color][color=Blue]pause [/color][color=Navy]100 
      [/color][color=Blue]for [/color][color=Purple]w5 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]100
      [/color][color=Blue]let [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b1 [/color][color=DarkCyan]- [/color][color=Navy]1       
      [/color][color=Blue]gosub [/color][color=Black]step1
      [/color][color=Blue]next [/color][color=Purple]w5
            [/color]
[color=Blue]goto [/color][color=Black]main
      
inch15l: 
      [/color][color=Blue]pause [/color][color=Navy]100 
            [/color][color=Blue]for [/color][color=Purple]w5 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]2400
            [/color][color=Blue]let [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b1 [/color][color=DarkCyan]+ [/color][color=Navy]1       
            [/color][color=Blue]gosub [/color][color=Black]step1
            [/color][color=Blue]next [/color][color=Purple]w5
                  [/color][color=Blue]pause  [/color][color=Navy]500
      [/color][color=Blue]gosub [/color][color=Black]inch15r:[/color]
[color=Blue]goto [/color][color=Black]main
      
step1:
      [/color][color=Blue]let [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Purple]b1 [/color][color=DarkCyan]& [/color][color=Navy]%00000011
      [/color][color=Blue]lookup [/color][color=Purple]b1[/color][color=Black],[/color][color=Blue]([/color][color=Navy]%10100000[/color][color=Black],[/color][color=Navy]%10010000[/color][color=Black],[/color][color=Navy]%01010000[/color][color=Black],[/color][color=Navy]%01100000[/color][color=Blue])[/color][color=Black],[/color][color=Purple]b2 
      [/color][color=Blue]let [/color][color=Purple]pins [/color][color=DarkCyan]= [/color][color=Purple]b2                 [/color]
[color=Blue]return[/color]
inch15r is what i presume will be moving the motor 100 steps right a and inch15l moving 2400 steps left

i dont know exactly what youe intentions are, but this what i chucked together

Code:
[color=Blue]symbol [/color][color=Purple]motor      [/color][color=DarkCyan]= [/color][color=Purple]b0              [/color][color=Green]'output variable, hold value for motor coils[/color]
[color=Blue]symbol [/color][color=Purple]steppos    [/color][color=DarkCyan]= [/color][color=Purple]b1              [/color][color=Green]'stepper motor lookup value[/color]
[color=Blue]symbol [/color][color=Purple]loop1      [/color][color=DarkCyan]= [/color][color=Purple]w2              [/color][color=Green]'counter for loop[/color]
[color=Blue]symbol [/color][color=Purple]loop2      [/color][color=DarkCyan]= [/color][color=Purple]w3              [/color][color=Green]'max value for loop[/color]

[color=Black]main:[/color]
[color=Green];check for right
      [/color][color=Blue]if [/color][color=Purple]pin0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
            let [/color][color=Purple]loop2 [/color][color=DarkCyan]= [/color][color=Navy]100
            [/color][color=Blue]for [/color][color=Purple]loop1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Purple]loop2
            [/color][color=Blue]inc [/color][color=Purple]steppos 
            [/color][color=Blue]gosub [/color][color=Black]step1
            [/color][color=Blue]next [/color][color=Purple]loop1
            [/color][color=Blue]let [/color][color=Purple]loop1 [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]end if[/color]
[color=Green];check for left
      [/color][color=Blue]if [/color][color=Purple]pin1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
            let [/color][color=Purple]loop2 [/color][color=DarkCyan]= [/color][color=Navy]2400
            [/color][color=Blue]for [/color][color=Purple]loop1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Purple]loop2
            [/color][color=Blue]dec [/color][color=Purple]steppos 
            [/color][color=Blue]gosub [/color][color=Black]step1
            [/color][color=Blue]next [/color][color=Purple]loop1
            [/color][color=Blue]let [/color][color=Purple]loop1 [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]endif
      
      goto [/color][color=Black]main
      
step1:
      
      [/color][color=Blue]let [/color][color=Purple]steppos [/color][color=DarkCyan]= [/color][color=Purple]steppos [/color][color=DarkCyan]& [/color][color=Navy]%00000011
      [/color][color=Blue]lookup [/color][color=Purple]steppos[/color][color=Black],[/color][color=Blue]([/color][color=Navy]%10100000[/color][color=Black],[/color][color=Navy]%10010000[/color][color=Black],[/color][color=Navy]%01010000[/color][color=Black],[/color][color=Navy]%01100000[/color][color=Blue])[/color][color=Black],[/color][color=Purple]motor 
      [/color][color=Blue]let [/color][color=Purple]pins [/color][color=DarkCyan]= [/color][color=Purple]motor
      [/color][color=Blue]return[/color]
seems to work in simulator
 
Whoops! You&#8217;re right! The code I posted is part of a larger programme and I was using it as you say to set an inching function for a stepper motor. I changed the gosub command but forgot to change the gobsub headings before I posted it, they should read &#8216;rotater&#8217; and &#8216;rotatel&#8217; &#8211; sorry about that!
Thank you for the code I&#8217;ll have a play around with that.
The object is to pre determine radial positions by the number of motor steps from a zero reference and allocate that position a number, input that number from a keypad and get the motor to step to that position. The problem in the past has, as ever, been backlash in the motor gearing. I&#8217;ve tried using a magnetic encoder to control the position but couldn&#8217;t successfully integrate the PWM output from the encoder into the programme, (probably the subject of a future thread)? The reason for the change in approach is - I&#8217;ve found a motor complete with gearbox, (made by Crouzet in France), that has an extremely small amount of backlash at the output shaft and even when driving a 300mm beam at the centre, there is only about 0.012&#8221; at the ends of the beam and I can live with that. If anyone is interested the motor came out of a thermostatically controlled shower; it was used to drive the mixer valve that controls the water temperature.
 
Top