B4J Question RPi - Time delay with stepper motors

Mark Read

Well-Known Member
Licensed User
Longtime User
I am using B4J to control a stepper motor on a rover. The whole project is working and I was able to test the rover. It works without a load. Unfortunately I need a load of 2 kg. I tried placing a weight on my rover and it stopped - the motor is not strong enough. I have two options:

1. A single stronger motor - means just a mechanical rebuild, not a problem.
2. A second motor.

With the second option, I have a question. The activation of the motor coils is via a timer with a delay of 50ms or 100ms (depending on forward or fast forward). If I control two motors, will I get a problem with the speed of the code interfereng with the timer? I hope this is clear.

Many thanks.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
The tick event activates one coil per tick. In the case of two motors it would activate motor 1, coil 1 then motor 2 coil 1. I suppose my question is simply how fast can 8 lines of code be (changing the states of 8 outputs)?

B4X:
'move motor
Private Sub Timer1_Tick
    If CW=False Then
           currentStateIndex=currentStateIndex-1
        If currentStateIndex=-1 Then currentStateIndex = states.Size-1
    Else
        currentStateIndex = (currentStateIndex + 1) mod states.Size
    End If

    Dim st As State = states.Get(currentStateIndex)
    ' Move motor 1
    m1A.State = st.MotorOutput1
    m1C.State = st.MotorOutput2
    m1F.State = st.MotorOutput3
    m1D.state = st.MotorOutput4

    ' Move motor 2
    m2A.State = st.MotorOutput1
    m2C.State = st.MotorOutput2
    m2F.State = st.MotorOutput3
    m2D.state = st.MotorOutput4
 
    Feedback="step " & CW
    Feedback=Feedback.ToUpperCase
    Log(Feedback)
 
    astream.Write(Feedback.GetBytes("UTF8"))        'we have moved a step, send to tablet
End Sub

One point I have noticed is that when the "step" is sent to the tablet via BT, there is a delay. But I can live with this.
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Okay, I have just ordered two motors. Have to wait a week to find out.
 
Upvote 0
Top