Share My Creation Arduino to PC

Arduino Uno/Nano inputs outputs connected via USB controlled by PC/Android using B4J/B4A app .

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private AStream As AsyncStreams

    Public out8 As Pin
    Public out9 As Pin
    Public out10 As Pin
    Public out11 As Pin
    Public in2 As Pin
    Public in3 As Pin
    Public in4 As Pin
    Public in5 As Pin
    Public anaA0 As Pin
    Public anaA1 As Pin
    Public ana6 As Pin
    Private Timer1 As Timer
 
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    AStream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    out8.Initialize(8, out8.MODE_OUTPUT)
    out9.Initialize(9, out9.MODE_OUTPUT)
    out10.Initialize(10, out10.MODE_OUTPUT)
    out11.Initialize(11, out11.MODE_OUTPUT)
    in2.Initialize(2, in2.MODE_INPUT_PULLUP)
    in3.Initialize(3, in3.MODE_INPUT_PULLUP)
    in4.Initialize(4, in4.MODE_INPUT_PULLUP)
    in5.Initialize(5, in5.MODE_INPUT_PULLUP)
    anaA0.Initialize(anaA0.A0, anaA0.MODE_INPUT)
    anaA1.Initialize(anaA1.A1, anaA1.MODE_INPUT)
    ana6.Initialize(6, ana6.MODE_OUTPUT)
 
    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick
    Private ps(4), ana1(2), ana2(2) As Byte
 
    ana1(0)=anaA0.AnalogRead
    ana1(1)=anaA0.AnalogRead / 256
    ana2(0)=anaA1.AnalogRead
    ana2(1)=anaA1.AnalogRead / 256
 
    If in2.DigitalRead Then ps(0)=1 Else ps(0)=0
    If in3.DigitalRead Then ps(1)=1 Else ps(1)=0
    If in4.DigitalRead Then ps(2)=1 Else ps(2)=0
    If in5.DigitalRead Then ps(3)=1 Else ps(3)=0
    AStream.Write(ps)
    AStream.Write(ana1)
    AStream.Write(ana2)
 
End Sub

Sub Astream_NewData (Buffer() As Byte)
 
    If Buffer.Length > 1 Then
        If Bit.And(1,Buffer(0))=1 Then out8.DigitalWrite(True) Else out8.DigitalWrite(False)
        If Bit.And(2,Buffer(0))=2 Then out9.DigitalWrite(True) Else out9.DigitalWrite(False)
        If Bit.And(4,Buffer(0))=4 Then out10.DigitalWrite(True) Else out10.DigitalWrite(False)
        If Bit.And(8,Buffer(0))=8 Then out11.DigitalWrite(True) Else out11.DigitalWrite(False)
     
        ana6.AnalogWrite(Buffer(1) * 3)
     
    End If
End Sub

Sub AStream_Error
    Log("error")
End Sub

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private sp As Serial
    Private astream As AsyncStreams
 
    Private cmb1 As ComboBox
    Private btnOpen As Button
    Private bar1 As ProgressBar
    Private Label1 As Label
    Private in1 As Pane
    Private in2 As Pane
    Private in3 As Pane
    Private in4 As Pane
    Private analog2 As TextField
    Private analog1 As TextField
    Private out1, out2, out3, out4 As Byte
    Private check1 As CheckBox
    Private check2 As CheckBox
    Private check4 As CheckBox
    Private check3 As CheckBox
    Private bar2 As ProgressBar

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    sp.Initialize("")
    cmb1.Items.AddAll(sp.ListPorts)

End Sub

Sub cmb1_SelectedIndexChanged(Index As Int, Value As Object)
    btnOpen.Enabled = Index > -1 'enable the button if there is a selected item
End Sub

Sub btnOpen_Action
    sp.Open(cmb1.Value)
    sp.SetParams(9600,8,1,0)
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    btnOpen.Enabled = False
    Label1.Text = "Port Opened"

End Sub

Sub MainForm_Closed
    'sp.Close
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim a1, a2 As Int
    Dim outB(2) As Byte
    Dim an1, an2 As Double
 
    If Buffer.Length > 7 Then
        If Buffer(0) = 1 Then in1.Alpha=1 Else in1.Alpha=0
        If Buffer(1) = 1 Then in2.Alpha=1 Else in2.Alpha=0
        If Buffer(2) = 1 Then in3.Alpha=1 Else in3.Alpha=0
        If Buffer(3) = 1 Then in4.Alpha=1 Else in4.Alpha=0
     
        If Buffer(4) < 0 Then a1=Buffer(4)+255 Else a1=Buffer(4)    'convert to unsigned byte
        If Buffer(6) < 0 Then a2=Buffer(6)+255 Else a2=Buffer(6)    'convert to unsigned
        an1=(a1+Buffer(5)*256)/1024
        an2=(a2+Buffer(7)*256)/1024
        analog1.Text = NumberFormat(an1*5,1,3) & " V"
        analog2.Text = NumberFormat(an2*5,1,3) & " V"
        bar1.Progress=an1
        bar2.Progress=an2
    End If
    outB(0)=out1 + out2*2 + out3*4 + out4*8
    outB(1)=127
    astream.Write(outB)
End Sub

Sub check1_CheckedChange(Checked As Boolean)
    If Checked Then out1=1 Else out1=0
End Sub

Sub check2_CheckedChange(Checked As Boolean)
    If Checked Then out2=1 Else out2=0
End Sub

Sub check3_CheckedChange(Checked As Boolean)
    If Checked Then out3=1 Else out3=0
End Sub

Sub check4_CheckedChange(Checked As Boolean)
    If Checked Then out4=1 Else out4=0
End Sub
 

Attachments

  • arduino_b4j.zip
    3.9 KB · Views: 91
  • arduino_io_b4r.zip
    1.2 KB · Views: 91
  • io_b4a.zip
    6.7 KB · Views: 84
  • arduino4x3.jpg
    arduino4x3.jpg
    23.5 KB · Views: 280
  • arduino.gif
    arduino.gif
    13.9 KB · Views: 23
  • android-io.jpg
    android-io.jpg
    24.3 KB · Views: 23
Last edited:
Top