Share My Creation Controlled DC Motor Car with BlueTooth using L298N motor Driver [B4A and B4R]

Hi,

This is my first creation for B4R and B4A. Thanks for the community on this forum which I learn much from. I know this is not first creation/stuff that people built, many of controlled DC motor car with bluetooth that already built out there. But the interesting part is I'm using B4A and B4R.

PART
Car framing/chasis complete with tire and DC Motor Gearbox can be purchased from commerce web (see picture below)

1585611_66d89ef3-e8a6-47f9-84cd-2f46b1b62a0f_686_686.jpg
1585611_b0a7bbd0-9834-486a-aee4-947b30282ebc_680_680.jpg


To run the DC Motor I'm using L298N motor drive :

224603_b764095a-8b6e-49ec-9851-9550f0c5d15f.jpg

To control the CAR itself I'm using bluetooth HC-06 module :
841472_8ce14106-abd6-49c6-b543-94711fb1ca1b.jpg

SKETCH

The sketch for installation controlled DC Motor Car as below :
sketch-2.jpg

INSTALLATION
The installation as per picture below :
DCMOtor2.jpg


There are many examples and some explanations out there how to power the DC Motor. We can power it from external battery or from internal Arduino. On this case, the power to run the DC Motor is from Arduino.

PROGRAMMING
I did some modification of erel tutorial about bluetooth https://www.b4x.com/android/forum/threads/hc-05-classic-bluetooth.66677/ and from klaus booklets B4R examples by adding 4 buttons command at B4A to control DC Motor.

Screenshot_2021-10-18-19-52-43-825_b4a.HC05LightDimmer-ok.png


B4A Code :
B4X:
#Region  Project Attributes 
    #ApplicationLabel: DC_Motor_B4A_BlueTooth
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private tbtLED As ToggleButton
    Private lblStatus As Label
    Private btnConnect, btnUp, btnDown, btnRight, btnLeft As Button
    Private Up, Down, Right, Left As Reflector
    Private lblMessage As Label
    Private ProgressBar1 As ProgressBar
    
    Private Status As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    Up.Target = btnUp
    Up.SetOnTouchListener("Up_OnTouch")
    Down.Target = btnDown
    Down.SetOnTouchListener("Down_OnTouch")
    Right.Target = btnRight
    Right.SetOnTouchListener("Right_OnTouch")
    Left.Target = btnLeft
    Left.SetOnTouchListener("Left_OnTouch")
End Sub

Sub Activity_Resume
    SetState
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True And Status = "connected" Then
        CallSub2(Starter, "SendMessage", Array As Byte(0))
    End If
End Sub

Public Sub SetState
    tbtLED.Enabled = Starter.connected
    btnConnect.Enabled = Not(Starter.connected)
    ProgressBar1.Visible = Starter.connecting
    If Starter.Connected Then
        Status = "connected"
    Else If Starter.TryToConnect Then
        Status = "trying to connect..."
    Else If Starter.Connecting Then
        Status = "HC-06 found connecting..."
    Else
        Status = "disconnected"
    End If
    lblStatus.Text = $"Status: ${Status}"$
End Sub

Public Sub MessageFromDevice(msg As String)
    lblMessage.Text = msg
End Sub

Sub tbtLED_CheckedChange(Checked As Boolean)
    Dim b As Byte
    If Checked = True Then
        b = 1
    Else
        b = 0
    End If
    CallSub2(Starter, "SendMessage", Array As Byte(b))
End Sub

Sub btnConnect_Click
    CallSub(Starter, "Connect")
End Sub



Sub Up_OnTouch( viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Select action
        Case Activity.ACTION_DOWN
            Dim b As String
            b="UP"
            CallSub2(Starter, "SendMessage1", b)
        Case Activity.ACTION_UP
            Dim b As String
            b="OFF"
            CallSub2(Starter, "SendMessage1", b)            
    End Select
    btnUp.Invalidate
    Return True
End Sub


Sub Down_OnTouch( viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Select action
        Case Activity.ACTION_DOWN
            Dim b As String
            b="DOWN"
            CallSub2(Starter, "SendMessage1", b)
        Case Activity.ACTION_UP
            Dim b As String
            b="OFF"
            CallSub2(Starter, "SendMessage1", b)            
    End Select
    btnDown.Invalidate
    Return True
End Sub


Sub Right_OnTouch( viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Select action
        Case Activity.ACTION_DOWN
            Dim b As String
            b="RIGHT"
            CallSub2(Starter, "SendMessage1", b)
        Case Activity.ACTION_UP
            Dim b As String
            b="OFF"
            CallSub2(Starter, "SendMessage1", b)            
    End Select
    btnRight.Invalidate
    Return True
End Sub


Sub Left_OnTouch( viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Select action
        Case Activity.ACTION_DOWN
            Dim b As String
            b="LEFT"
            CallSub2(Starter, "SendMessage1", b)
        Case Activity.ACTION_UP
            Dim b As String
            b="OFF"
            CallSub2(Starter, "SendMessage1", b)            
    End Select
    btnLeft.Invalidate
    Return True
End Sub

B4R Code :
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private SoftwareSerial1 As SoftwareSerial
    Private astream As AsyncStreams
    Private IN1, IN2, IN3, IN4 As Pin        'pin for motor
    Private YellowLED As Pin
    Private Timer1 As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    YellowLED.Initialize(7, YellowLED.MODE_OUTPUT)
    IN1.Initialize(2, IN1.MODE_OUTPUT)
    IN2.Initialize(3, IN2.MODE_OUTPUT)
    IN3.Initialize(4, IN3.MODE_OUTPUT)
    IN4.Initialize(5, IN4.MODE_OUTPUT)
    SoftwareSerial1.Initialize(9600, 11, 12) 'software serial port on pins 12 and 11
    astream.Initialize(SoftwareSerial1.Stream, "astream_NewData", Null)
    Timer1.Initialize("timer1_Tick", 1000)
    Timer1.Enabled = True
End Sub

Sub Timer1_Tick
    astream.Write("Millis here: ".GetBytes)
    astream.Write(NumberFormat(Millis, 0, 0).GetBytes)
    astream.Write(Array As Byte(10)) 'end of line character. AsyncStreamsText will cut the message here
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim bc As ByteConverter
    Dim value As String = bc.StringFromBytes(Buffer)
    
    Select value
        Case "UP"
            YellowLED.DigitalWrite(True)
            IN1.DigitalWrite(True)
            IN2.DigitalWrite(False)
            IN3.DigitalWrite(True)
            IN4.DigitalWrite(False)
        Case "DOWN"
            YellowLED.DigitalWrite(True)
            IN1.DigitalWrite(False)
            IN2.DigitalWrite(True)
            IN3.DigitalWrite(False)
            IN4.DigitalWrite(True)
            
        Case "LEFT"
            YellowLED.DigitalWrite(True)
            IN1.DigitalWrite(False)
            IN2.DigitalWrite(False)
            IN3.DigitalWrite(True)
            IN4.DigitalWrite(False)
            
        Case "RIGHT"
            YellowLED.DigitalWrite(True)
            IN1.DigitalWrite(True)
            IN2.DigitalWrite(False)
            IN3.DigitalWrite(False)
            IN4.DigitalWrite(False)
            
        Case "OFF"
            YellowLED.DigitalWrite(False)
            IN1.DigitalWrite(False)
            IN2.DigitalWrite(False)
            IN3.DigitalWrite(False)
            IN4.DigitalWrite(False)
        
    End Select
    
    
End Sub

Complete Code for B4R and B4A are in attachment

VIDEO
For Video :
 

Attachments

  • Screenshot_2021-10-18-19-52-43-825_b4a.HC05LightDimmer.png
    Screenshot_2021-10-18-19-52-43-825_b4a.HC05LightDimmer.png
    71 KB · Views: 366
  • B4A-DC Motor.zip
    498.9 KB · Views: 438
  • B4R-DC Motor.zip
    1.6 KB · Views: 360

Similar Threads

Top