B4R Question [Solved]Test setting speed motor convert C code to B4R

Theera

Expert
Licensed User
Longtime User
I've tried to learn B4R programming the first time.I have the code in C ,I would like to change to be B4R Code.
But I don't know that the code is correct,or not. Please guide me to correct it. (The objective is to familiarize them with the B4R language).
B4X:
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
    
    'Define the motor pins
    Private motor_1A As Pin
    Private motor_2A As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'Set motor pin 1 as output
    motor_1A.Initialize(26,motor_1A.MODE_OUTPUT)
    'Set motor pin 2 as output
    motor_2A.Initialize(25,motor_2A.MODE_OUTPUT)
    'Set motor speed (0-255)
    motor_1A.AnalogWrite(255)
    motor_2A.AnalogWrite(0)
    Delay(3000)
    motor_1A.AnalogWrite(0)
    motor_2A.AnalogWrite(0)
End Sub
 

Attachments

  • setSpeedmotor.jpg
    setSpeedmotor.jpg
    97.6 KB · Views: 77
Solution
Nothing special

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    'Define the motor pins
    Private motor_1A, motor_2A As Pin
    'Pin numbers
    Private const motor_1A_port As Byte = 26
    Private const motor_2A_port As Byte = 25
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    motor_1A.Initialize(motor_1A_port, motor_1A.MODE_OUTPUT)    'Set motor pin 1 as output
    motor_2A.Initialize(motor_2A_port, motor_2A.MODE_OUTPUT)    'Set motor pin 2 as output
    ...

Just note for any programming: the thing that depends on the external conditions (user, hardware circuit diagram, MCU model, OS kind, settings... so it is variable) is to be declared as ... a variable. Or constant value like here...

peacemaker

Expert
Licensed User
Longtime User
Why do you doubt ? Looks OK.
But why 26 and 25 constants are not declared like in Arduino C code...
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Why do you doubt ? Looks OK.
But why 26 and 25 constants are not declared like in Arduino C code...
I don't know how to code.I have just read Klaus'booklets , but there is not anything about const.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Nothing special

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    'Define the motor pins
    Private motor_1A, motor_2A As Pin
    'Pin numbers
    Private const motor_1A_port As Byte = 26
    Private const motor_2A_port As Byte = 25
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    motor_1A.Initialize(motor_1A_port, motor_1A.MODE_OUTPUT)    'Set motor pin 1 as output
    motor_2A.Initialize(motor_2A_port, motor_2A.MODE_OUTPUT)    'Set motor pin 2 as output
    ...

Just note for any programming: the thing that depends on the external conditions (user, hardware circuit diagram, MCU model, OS kind, settings... so it is variable) is to be declared as ... a variable. Or constant value like here.
For easy reading\searching\updating the project.

It's not required, but it's a very important rule. The more complex project is, the more important it is.
 
Last edited:
Upvote 0
Solution

Theera

Expert
Licensed User
Longtime User
Nothing special

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    'Define the motor pins
    Private motor_1A, motor_2A As Pin
    'Pin numbers
    Private const motor_1A_port As Byte = 26
    Private const motor_2A_port As Byte = 25
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    motor_1A.Initialize(motor_1A_port, motor_1A.MODE_OUTPUT)    'Set motor pin 1 as output
    motor_2A.Initialize(motor_2A_port, motor_2A.MODE_OUTPUT)    'Set motor pin 2 as output
    ...

Just note for any programming: the thing that depends on the external conditions (user, hardware circuit diagram, MCU model, OS kind, settings... so it is variable) is to be declared as ... a variable. Or constant value like here.
For easy reading\searching\updating the project.

It's not required, but it's very important rule. The more complex project is, the much important.
Why to use byte instead of int?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Could we can code like this.

Private const motor_1A_port As(pin) As
Byte = 26
Private const motor_2A_port As(Pin) As
Byte = 25
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
When you invent the programming language allowing such declaration - it'll be OK.
Just try to code, and see what the IDE suggests.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I 've added RunNative stratgy,too
B4X:
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
    
    'Define the motor pins
    Private motor_1A As Pin
    Private motor_2A As Pin
    'Pin numbers
    Private const motor_1A_Port As Byte=26
    Private const motor_2A_Port As Byte=25
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'Stragy1: Using Non-NativeCoding
    
    'Set motor pin 1 as output
    motor_1A.Initialize(motor_1A_Port,motor_1A.MODE_OUTPUT)
    'Set motor pin 2 as output
    motor_2A.Initialize(motor_2A_Port,motor_2A.MODE_OUTPUT)
    'Set motor speed (0-255)
    motor_1A.AnalogWrite(255)
    motor_2A.AnalogWrite(0)
    Delay(3000)
    motor_1A.AnalogWrite(0)
    motor_2A.AnalogWrite(0)
    
    'Strate2: Using NativeCoding
    
    'RunNative ("SetSpeedMotors",Null)
    
End Sub


#If C
 Void SetSpeedMotors(B4R::Object*o) {  //<--Add this line instead of the line which is deleted
 Const int motorB_1A=26;
 Const int motorB_2A=25;
 //Void setup(){ //<-- Delete this line
   pinmode(motor_1A,output);
   pinmode(motor_2A,output);
   analogWrite(motorB_1A,255);
   analogWrite(motorB_2A,0);
   delay(3000)
   analogWrite(motorB_1A,0);
   analogWrite(motorB_2A,0);
 }
     
#End If
 
Upvote 0
Top