B4R Question Pass a variable to Timer event

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi:

I'm trying to control an Arduino Car (Arduino UNO + 2 wheels (DC motors)+ Bluetooth interface) with B4J/B4A application.

I've already coded the B4J and B4R part but I've a question that I can't solve; at B4J application, I've the stick (for car movements) and the gas pedal (for speed)

When interfacing with Arduino car, I send via Bluetooth the position (up,down, left, right, and the acceleration (gas pedal); in B4R code, I simulated the gas with a more or less delay between sending off/on command to the motors (there're four pins involved to control the motor wheels, two for each wheel that control the directions (A for forward, B for Backward)

For example, when sending Up form B4J app, I control the direction setting the correct pin on and of a time depending of the gas pedal value sended


B4X:
Sub AStream_NewData (Buffer() As Byte)
    Log("Received ",Buffer)
    Dim bc As ByteConverter
    Dim b As Byte=Buffer(0)
    Dim st As String = bc.StringFromBytes(Array As Byte(b))
      
    Select Case st
       
        Case "J"
            Log("joystick ", Buffer)
            MoveCar(Buffer)
        Case Else
            Log("Other functions ", Buffer)
    End Select

End Sub

Sub Movecar(buffer)
    Dim bc As ByteConverter
    Dim b1 As Byte=buf(1)
    Dim b2 As Byte=buf(2)
    Dim b3 As Byte=buf(3)            'Power of joystick
    Public delaymovement As Byte=10
    
    Dim Powerjoy As Byte=(b3-48)*50
    
    Dim side As String = bc.StringFromBytes(Array As Byte(b1))
       
    Select Case side
       
        Case "S"   'Stop
            PinA1A.DigitalWrite(False) 
            PinB1A.DigitalWrite(False) 
            PinA1B.DigitalWrite(False) 
            PinB1B.DigitalWrite(False) 
       
       
Case "U"
            PinA1B.DigitalWrite(False)     'Wheel A -- Backward
            PinB1B.DigitalWrite(False)     'Wheel B -- Backward
            PinA1A.DigitalWrite(True)      'Wheel A -- forward
            PinB1A.DigitalWrite(True)      'Wheel B -- forward
            Delay(delaymovement*Powerjoy)   'If more gas pedal value sended, more delay added=motor runs more time
            Log("Delay= ", delaymovement*Powerjoy)
            PinA1A.DigitalWrite(False)
            PinB1A.DigitalWrite(False)

The problem is that I'm delaying at MoveCar Sub called from astream_Event, and I can't receive any other sended value while the code is delayed.

I've tried to manage this moving MoveCarSub to a Timer, initializating the Timer on astream_event with delay proportional to gas pedal value but I can't pass the buffer value (3 bytes) to Timer and get the buffer values on it because B4R/Arduino can't have a global variable; this is what I tried:
B4X:
Sub Process_Globals
Dim BufferBT() as Byte
and copy the contents of buffer astream on it
B4X:
BufferBT=buffer
but B4R doesn't permit this option ("Cannot Set a Const variable (global non-primitive variables are always constants)

Someone has an idea of how to implement it?
 

derez

Expert
Licensed User
Longtime User
Why do you use DigitalWrite ?
See this, I control the heading putting more power to one wheel and can reduce speed by using less then "Full" which is ~ 230 (calibrated per motor):
B4X:
Sub forward
    red.DigitalWrite(False)
  
    L2.AnalogWrite(0)
    R2.AnalogWrite(0)
    If Abs(yaw - 180.0) < 3 Then
        L1.AnalogWrite(fullL) 'fwd left  ful
        R1.AnalogWrite(fullR) 'fwd right  full
    else if yaw - 180 > 3 Then
        L1.AnalogWrite(fullL-40)
        R1.AnalogWrite(fullR)
    Else
        L1.AnalogWrite(fullL)
        R1.AnalogWrite(fullR-40)
    End If
  
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
For the speed, you should use the pen features of arduino... As for passing the needed arguments, it's a perfect use for the new serializer lib
 
Upvote 0
Top