B4R Question Breaks between stepper motor

IVR

Member
Licensed User
Hi, I have a problem with the library stepper, when I start two stepper motors I breaks speed between an motor and the other. Thanks for your help.
 

Cableguy

Expert
Licensed User
Longtime User
How are you connecting the steppers?
1 stepper driver for both motors or 1 per motor? Or no driver at all?
The best solution is to each motor to have it's own driver, although there are drivers capable of driving 2 motors having independent control pins.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Then it's not a circuit thing, it's a code thing....
Can you post your code?
 
Upvote 0

IVR

Member
Licensed User
Ok.
Sub Process_Globals
Public Serial1 As Serial
Private SerialNative1 As Stream
Private astream As AsyncStreams
Private pins(2) As Pin
Private StepsPerRev As Int = 3200 'Steps Per Revolution
Private stpMotTemp As Stepper
Private stpMotFumi As Stepper
Private stpMotCloc As Stepper
Public Interval As Int
Public DHT22sensor As dht
Public DHT22pin As Pin
Public FC01pin As Pin 'Sensore di fiamma
Public SFUMIpin As Pin 'Sensore di Fumi
Public dblFiamma,dblFumi As Double
Public tempAmb As Double 'DHT22 readings Temperature ambiente
Public progMotTempAmb As ULong = 5 'Calcolo da effettuare Range((MaxMotorTempAmb - MaxMotorTempAmb) / (tempmax - tempMin))
Public progMotFumi As ULong = 5 'Calcolo da effettuare Range((MaxMotorFumi - MaxMotorFumi) / (fumimax - fumiMin))
Public memoTempAmb As Double = 30.00
Public memoFumi As Double = 40.00
Public MinMotorTempAmb As ULong = 10
Public MaxMotorTempAmb As ULong = 100
Public MinMotorFumi As ULong = 10
Public MaxMotorFumi As ULong = 100
Public MinMotorCloc As ULong = 10
Public MaxMotorCloc As ULong = 100
End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("appstart")
Interval=5 '5 sec Timer Interval
'Using the hardware serial named Serial1 (Arduino Due)
'A SoftwareSerial will also work.
RunNative("SerialNative1", Null)
astream.Initialize(SerialNative1, "astream_NewData", Null)
stpMotTemp.Initialize(StepsPerRev, 9, 24)
stpMotFumi.Initialize(StepsPerRev, 6, 25)
stpMotCloc.Initialize(StepsPerRev, 10, 26)
DHT22pin.Initialize(30, DHT22pin.MODE_INPUT) 'Initialize at NodeMCU Pin D2 (SDA,DATA)
SFUMIpin.Initialize(0, SFUMIpin.AnalogRead)
FC01pin.Initialize(1, FC01pin.AnalogRead)
'Must be pwm pins as we are using AnalogWrite
pins(0).Initialize(5, pins(0).MODE_OUTPUT)
pins(1).Initialize(6, pins(1).MODE_OUTPUT)
pins(1).DigitalWrite(True)
Sensor(0)
MotTemp(0)
MotFumi(0)
SendParam(0)
End Sub

#if C
void SerialNative1(B4R::Object* unused) {
::Serial1.begin(9600);
b4r_main::_serialnative1->wrappedStream = &::Serial1;
}
#end if
Private Sub Sensor(unused As Byte)
'Sensore Temperatura
DHT22sensor.Read22(DHT22pin.PinNumber)
tempAmb = DHT22sensor.GetTemperature
'Log("Temperatura =",NumberFormat(tempAmb,0,2), " DegC")
'Sensore Fiamma
'dblFiamma = FC01pin.AnalogRead
'Log("Fiamma = ",NumberFormat(dblFiamma,0,2))
'Sensore Fumi
dblFumi = SFUMIpin.AnalogRead
'Log("Fumi = ",NumberFormat(dblFumi,0,2))
CallSubPlus("Sensor", 0, 0)
End Sub

Private Sub MotTemp(unused As Byte)
Dim current_speed_mta As ULong = 0
Dim current_speed_cloc As ULong = 0
If (tempAmb >= memoTempAmb) Then
current_speed_mta = MinMotorTempAmb
Else
current_speed_mta = ((memoTempAmb - tempAmb) * progMotTempAmb)
End If
stpMotTemp.SetSpeed(current_speed_mta)
stpMotTemp.Step(StepsPerRev)
CallSubPlus("MotTemp", 0, 0)
End Sub

Private Sub MotFumi(unused As Byte)
Dim current_speed_fumi As ULong = 0
If (dblFumi >= memoFumi) Then
current_speed_fumi = MinMotorFumi
Else
current_speed_fumi = ((memoFumi - dblFumi) * progMotFumi)
End If
stpMotFumi.SetSpeed(current_speed_fumi)
stpMotFumi.Step(StepsPerRev)
CallSubPlus("MotFumi", 0, 0)
End Sub

Private Sub SendParam(unused As Byte)
'Trasmette a B4a
'astream.Write("Millis here: ".GetBytes)
astream.Write(NumberFormat(tempAmb, 0, 2).GetBytes)
astream.Write(":")
astream.Write(NumberFormat(dblFiamma, 0, 2).GetBytes)
astream.Write(":")
astream.Write(NumberFormat(dblFumi, 0, 2).GetBytes)
astream.Write(":")
astream.Write(Array As Byte(10)) 'end of line character. AsyncStreamsText will cut the message here
End Sub

Sub astream_NewData (Buffer() As Byte)
'Riceve da B4a
Dim strBuffer() As String
For k = 0 To Buffer.Length Step 1
strBuffer(k) = Buffer(k)
Next
Log(" strBuffer = ", strBuffer(0))
'Log(" pinIndex = ",Buffer.Length)
'For i = 0 To Buffer.Length - 2 Step 2
'Dim pinIndex As Byte = Buffer(i)
'Dim pinValue As Byte = Buffer(i + 1)
'using constrain to avoid non allowed values
'Log(" pinIndex = ",NumberFormat(pinIndex,0,2), " %", " pinValue =",NumberFormat(pinValue,0,2), " DegC")
'pins(Constrain(pinIndex, 0, 1)).AnalogWrite(pinValue)
'Next
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Stepper.Step is a blocking method. It will only continue after the steps completed. Moving both motors at the same time is more complicated.

You can add two global variables with the number of steps that should be moved.
Create a sub that is responsible for calling Step for each of the steppers when needed.

Use AddLooper to run this sub on each cycle (better than calling CallSubPlus with interval of 0).

In this sub check whether more steps are required and call Step:
B4X:
If StepsForMotor1 > 0 Then
 Motor1.Step(1)
 StepsForMotor1 = StepsForMotor1 - 1
End If
If StepsForMotor2 > 0 Then
 Motor2.Step(1)
 StepsForMotor2 = StepsForMotor2 - 1
End If
 
Upvote 0
Top