stepper motor code help please

Jerry Hat Trick

New Member
My setup is a Picaxe 18x via ULN2003A driver to ASTROSYN Y129-5.A program called FWD with interrupt and PTM switch runs the motor forward OK and a 2nd program REV with interupt also runs the motor in reverse OK, but when I combined the 2 above programs to get the motor to run fwd and rev in one program I've had no success after many attempts and code variations. I have tried using if..thens and sub routines but they just cause the motor to stutter around even with 0.01ms delay(motr runs smoothly using the rev & fwd prgms.) please see attached codes.
[

'stepper fwd with interupt

symbol delay = b0 'define the variable

let delay =10 'set delay to .001s

setint %00000000,%10000000 'activate interupt when input 7 goes low

main: let pins = %00001010 'first step

pause delay 'pause for delay

let pins = %00001001 'next step

pause delay 'pause for delay

let pins = %00000101 'next step

pause delay 'pause for delay

let pins = %00000110 'next step

pause delay 'pause for delay

goto main 'loop forever


interrupt:
setint %00000000,%10000000

intwait:
if pin7 = 0 then intwait
setint %00000000,%10000000
return]

[/ 'stepper rev with interupt



symbol delay = b0 'define the variable

let delay =10 'set delay to .001s

setint %00000000,%01000000 'activate interupt when input 6 goes low

main:
let pins = %00000110 'first step

pause delay 'pause for delay

let pins = %00000101 'next step

pause delay 'pause for delay

let pins = %00001001 'next step

pause delay 'pause for delay

let pins = %00001010 'next step

pause delay 'pause for delay

goto main 'loop forever


interrupt:
setint %00000000,%01000000

intwait:
if pin6 = 0 then intwait

setint %00000000,%01000000

return/]
[/'stepper fwd & rev interupt
main:
if pin7=1 then fwd
if pin6=1 then rev



symbol delay = b0 'define the variable

let delay =10 'set delay to .001s

setint %00000000,%10000000 'activate interupt when input 7 goes low

fwd: let pins = %00001010 'first step

pause delay 'pause for delay

let pins = %00001001 'next step

pause delay 'pause for delay

let pins = %00000101 'next step

pause delay 'pause for delay

let pins = %00000110 'next step

pause delay 'pause for delay

return 'loop forever


interrupt:
setint %00000000,%10000000

intwait:
if pin7 = 0 then intwait
setint %00000000,%10000000
return




let delay =10 'set delay to .001s

setint %00000000,%01000000 'activate interupt when input 6 goes low

rev: let pins = %00001010 'first step

pause delay 'pause for delay

let pins = %00001001 'next step

pause delay 'pause for delay

let pins = %00000101 'next step

pause delay 'pause for delay

let pins = %00000110 'next step

pause delay 'pause for delay

return 'loop forever


setint %00000000,%01000000

if pin6 = 0 then intwait
setint %00000000,%01000000
return /]
I look forward to any advice with stepper picaxe basic code, I've checked the picaxe manuals,postings and David Lincoln's 'programming the picaxe' but no luck.





 

BeanieBots

Moderator
Wow.. all those setint's and duplicate names. Looks like you have literally taken several programs and simply stuck them together. It doesn't quite work like that.
First off, what should it do?
Is it something like, push button A to make it go forwards, then push button B to make it go backwards?
You can only use a label once within the same program and you can only have one interrupt routine. That one interrupt routine must handle whatever events are used to trigger it.
I find the easiest way to do that is to wire all the inputs that are needed to trigger the interrupt onto one pin via diodes such that any of the inputs involved will activate the one input. The first thing to do in the interrupt routine is to then read all the inputs to see which one caused the trigger. Then you can make a descision in code on what to do next.
 

Jerry Hat Trick

New Member
Thanks Beanie bots for your posting. Yes i do want to control the motor with 2 push switches to make the motor move left & right through 180 degs from a central reference point(90deg left to 90degs right,it is to be part of a camera pan & tilt mechanism. I have tried a simpler prgram without interupts on the input pins but the motor moved excessively slowly and very steppy. I'll try your suggested solution. thanks again for your help and advice.
 

BeanieBots

Moderator
I've no idea how you have wired up the motor but your sequence looks a little strange which might explain the 'lumpyness'.
A more normal sequence would be
0011
0010
0110
0100
1100
1001
0001

You could define the sequence in EEPROM and then use variable to address it. Use the interrupt routine to decide if the address should inc or dec.
 

SD2100

New Member
<code><pre><font size=2 face='Courier'>
I made this up some time ago to pan a camera, I just added the part for the center position,
it uses an 08M and a ULN2003, the motor is connected as shown on page 15 of the Picaxe
interfacing circuits manual. Pins 0 &amp; 1 of the 08M are connected to pins 2 &amp; 3 of the ULN2003,
the <i>LEFT, RIGHT &amp; HOME </i> inputs on the 08M are pulled to 0v with 10k resistors
and go high when a switch is pressed. The motor will turn left or right and will stop
when the switch is released, if the home button is pressed the motor will return to
it's starting position, the home switch dosn't have to be held as once it goes into the
loop it stays there until center is reached.If the home switch is held high the motor
will return to it's start position when the left or right switch is released.
Also I've found that running the stepper on a lower voltage tends to reduce the jerky movement
a bit at a cost of reduced power, another way is to use reduction gears to smooth it out or just
use a small DC motor and a worm drive. I think the only problem with the code is if power is lost
then the value in the variable center is lost, so if it's not in center at power up then it thinks
it's in center wherever it is, you could get around this by adding a center position input switch
and have the camera drive left and right at power up to find the center position or driving the
camera fully one way until it hits a physical stop then step the motor back a known number of pulses
to the center or just save the current position in eeprom. Hope this is some help.


#picaxe 08m

symbol Left = pin2
symbol Right = pin3
symbol Home = pin4
symbol Center= b0

Center=100
main:
if Left=1 then
dec Center
gosub PanLeft
elseif Right=1 then
inc Center
gosub PanRight
elseif Home=1 then
if Center&lt;100 then
do
inc Center
gosub PanRight
loop until Center=100
elseif Center&gt;100 then
do
dec Center
gosub PanLeft
loop until Center=100
end if
end if
goto main

PanLeft:
toggle 1
pause 15
toggle 0
pause 15
return

PanRight:
toggle 0
pause 15
toggle 1
pause 15
return
</font></pre></code>


Edited by - Phil75 on 17/11/2006 07:00:11
 
Top