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
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:
and copy the contents of buffer astream on it
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?
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
B4X:
BufferBT=buffer
Someone has an idea of how to implement it?