Edit: see post #5 for an updated version.
This application is controlling a stepper motor which can be turned by a wanted angle and speed. I use this motor:
http://www.aliexpress.com/item/Smar...ith-ULN2003-Driver-Board-for/32314913056.html
The motor board is connected to power and ground and to 4 pins.
I use such a motor to turn a remote-control camera.
The code - Main:
Stepper code module:
This application is controlling a stepper motor which can be turned by a wanted angle and speed. I use this motor:
http://www.aliexpress.com/item/Smar...ith-ULN2003-Driver-Board-for/32314913056.html
The motor board is connected to power and ground and to 4 pins.
I use such a motor to turn a remote-control camera.
The code - Main:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private direction As Boolean = True
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
'define the 4 pin numbers and the time delay for the stepper
Stepper.Initialize(10,11,12,13,10)
Stepper.turn(True,90)
End Sub
public Sub turndone(Text As String)
'change turn direction
direction = Not(direction)
Stepper.turn(direction,90)
End Sub
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Private tmr As Timer
Private stopcount, endcount As Int
Private direction As Boolean
Private state As Int
Private p1,p2,p3,p4 As Pin
End Sub
Public Sub Initialize(dpin1 As Byte _
,dpin2 As Byte _
,dpin3 As Byte _
,dpin4 As Byte _
,TimeDelay As ULong )
tmr.Initialize("tmr_Tick",TimeDelay)
p1.Initialize(dpin1,p1.MODE_OUTPUT)
p2.Initialize(dpin2,p2.MODE_OUTPUT)
p3.Initialize(dpin3,p3.MODE_OUTPUT)
p4.Initialize(dpin4,p4.MODE_OUTPUT)
End Sub
public Sub turn(forward As Boolean, angle As Float)
stopcount = 0
endcount = angle * 2048 /360
direction = forward
state = 0
tmr.Enabled = True
End Sub
Private Sub tmr_Tick
If direction Then
rotateRight
Else
rotateLeft
End If
state = (state + 1) mod 4
If stopcount >= endcount Then
tmr.Enabled = False
zeroall
Main.turndone("done")
End If
stopcount = stopcount + 1
End Sub
Private Sub rotateLeft
Select state
Case 0
p1.DigitalWrite(True)
p4.DigitalWrite(False)
Case 1
p2.DigitalWrite(True)
p1.DigitalWrite(False)
Case 2
p3.DigitalWrite(True)
p2.DigitalWrite(False)
Case 3
p4.DigitalWrite(True)
p3.DigitalWrite(False)
End Select
End Sub
Private Sub rotateRight
Select state
Case 0
p1.DigitalWrite(True)
p2.DigitalWrite(False)
Case 1
p4.DigitalWrite(True)
p1.DigitalWrite(False)
Case 2
p3.DigitalWrite(True)
p4.DigitalWrite(False)
Case 3
p2.DigitalWrite(True)
p3.DigitalWrite(False)
End Select
End Sub
Private Sub zeroall
p1.DigitalWrite(False)
p2.DigitalWrite(False)
p3.DigitalWrite(False)
p4.DigitalWrite(False)
End Sub
Last edited: