B4R Library rAccelStepper

It is a (partly) wrap for this Arduino Github project. Best would be to work through the examples in the Github posting to apply methods in this library correctly. I have just done a simple project with the library.

The Fritzing project is in attached AccelStepper.zip
Library files including xml is in rAccelStepper.zip
Sample B4R project attached

Library can amongst others accelerate and decelerate the 28BYJ-48 Stepper motor (using a ULN2003 driver board) and can drive the motor "very fast".

Sample code:
B4X:
'Uno Pin 8 to IN1 of ULN2003
'Uno pin 9 to IN2 of ULN2003
'Uno pin 10 to IN3 of ULN2003
'Uno pin 11 to IN4 of ULN2003

'Note the sequencing of the pins i.e 8, 10, 9, 11
'acc.Initialize(acc.FULL4WIRE, 8, 10, 9, 11, True)


#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

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
   
    Dim acc As AccelStepper

    Dim p4, p5, p6 As Pin                              'got buttons connected to these pins
   
    Dim p4click, p5click, p6click As Boolean = False
   
    Dim t As Timer
   
    Dim numberOfSteps As Long = 0
    Dim prevNumberOfSteps As Long = 0
   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    t.Initialize("t_tick", 20)
    t.Enabled = True
   
    p4.Initialize(4, p4.MODE_INPUT_PULLUP)  'button to nudge the shaft clockwise forward in "nudgesteps" - see value assigned to nudgesteps above
    p5.Initialize(5, p5.MODE_INPUT_PULLUP)  'button that creates a random number and move the motor shaft accordingly (from shaft current position)
    p6.Initialize(6, p6.MODE_INPUT_PULLUP)  'button to move motor shaft to home position via the shortest route (smallest travel angle)
   
    p4.AddListener("p4_StateChanged")       'add a listener for the button
    p5.AddListener("p5_StateChanged")       'add a listener for the button
    p6.AddListener("p6_StateChanged")       'add a listener for the button
   
    'Note the sequencing of the pins i.e 8, 10, 9, 11
    acc.Initialize(acc.FULL4WIRE, 8, 10, 9, 11, True)
   
    acc.CurrentPosition = 0
    acc.MaxSpeed = 800
    acc.Speed = 800
    acc.Acceleration = 100
    AddLooper("myLooper")

   
End Sub

Sub myLooper
       
    acc.run

End Sub

Sub p4_StateChanged                                      
    If acc.distanceToGo = 0 Then
        If p4click = False Then                              
            Log("state 4 changed")
            p4click = Not(p4click)
            acc.move(4096)
        Else
            p4click = False
        End If
    End If  

End Sub

Sub p5_StateChanged                                      
   
    If acc.distanceToGo = 0 Then
        If p5click = False Then                              
            Log("state 5 changed")
            p5click = Not(p5click)
            acc.moveTo(0)
        Else
            p5click = False
        End If
    End If  

End Sub

Sub p6_StateChanged                                      

    If acc.distanceToGo = 0 Then
        If p6click = False Then                              
            Log("state 6 changed")
            p6click = Not(p6click)
            acc.move(-512)
        Else
            p6click = False
        End If
    End If  

End Sub

Sub t_tick
   
    numberOfSteps = acc.GetCurrentPosition
    If numberOfSteps <> prevNumberOfSteps Then
        prevNumberOfSteps = numberOfSteps
        Log("NOS = ", numberOfSteps)
    End If
   
   
End Sub

AccelStepper_bb.png




Take note of this:
B4X:
'Uno Pin 8 to IN1 of ULN2003
'Uno pin 9 to IN2 of ULN2003
'Uno pin 10 to IN3 of ULN2003
'Uno pin 11 to IN4 of ULN2003

'Note the sequencing of the pins i.e 8, 10, 9, 11
'acc.Initialize(acc.FULL4WIRE, 8, 10, 9, 11, True)
 

Attachments

  • b4rAccelStepper.zip
    1.6 KB · Views: 446
  • rAccelStepper.zip
    178.8 KB · Views: 443
  • AccelStepper.zip
    51.7 KB · Views: 418
Last edited:

jwgf

Member
Hello, how could I use this library to handle a pololu-type driver with an A4988, which has step input and direction, thanks, Jorge
 

ilan

Expert
Licensed User
Longtime User
thanx a lot @Johan Schoeman

i have a question please, according to the documentation you have 2 ways to initialize the motor

in your code you do it by passing 6 parameters although in this example that uses the same library only 3 parameters are passed.


B4X:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

how can i use your lib but initialize the motor as in this example since i am using the TB6600 driver.


thanx :)
 

Johan Schoeman

Expert
Licensed User
Longtime User
thanx a lot @Johan Schoeman

i have a question please, according to the documentation you have 2 ways to initialize the motor

in your code you do it by passing 6 parameters although in this example that uses the same library only 3 parameters are passed.


B4X:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

how can i use your lib but initialize the motor as in this example since i am using the TB6600 driver.


thanx :)
Hello @ilan

Been a very long time since I messed around with B4R and especially B4R libs. I hope someone that are more clued up than me will be able to assist you with the lib that you require or by modifying this lib.
 
Top