B4R Tutorial Control servo and relays using Nextion lcd display

I'm new in b4r but thanks to the help of the forum i made some progress.
So i decided to share my project with all, especialy the newbies like me.
My project is a simple example on how to control 2 relays and a servo motor, using Arduino Uno and Nextion lcd display.
Due to the limitation on arduino uno and nano (only one serial) i use the rSoftwareSerial library to comunicate with the display.
The connections are very simple.
+5V and ground for the lcd and servo
pins 10, 11 as RX, TX for the lcd
pins 12, 13 for the relays
pin 4 as control for the servo.

ard_Nextion.jpg


In Order to program the lcd display you need Nextion Editor.
https://nextion.itead.cc/resource/download/nextion-editor/

In the attached Nextion.zip you can find the .HMI and the pictures you need for the project

Finally here is my b4r code. as i said i'm new, so feel free to comment any error

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

Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private softserial As SoftwareSerial
    Private Relay1 As Pin
    Private Relay2 As Pin
    Public servo1 As Servo
    Public pinservo As Pin

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("appstart")
    Relay1.Initialize(12, Relay1.MODE_OUTPUT)
    Relay2.Initialize(13, Relay2.MODE_OUTPUT)
    Relay1.DigitalWrite(False)
    Relay2.DigitalWrite(False)
    softserial.Initialize(9600, 10, 11)
    astream.Initialize(softserial.Stream, "astream_newdata", Null)
    pinservo.Initialize (4,pinservo.MODE_OUTPUT)    'connect servo to pin 4 of Arduino
    servo1.Attach(pinservo.PinNumber)                'assign servo to device on pin servo

End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim bc As ByteConverter
    Dim input As String
    input=bc.StringFromBytes(Buffer)
    If IsNumber(input) Then
        servo1.Write(input)
        'Log(input)
    Else
        If input="1,ON" Then
            Relay1.DigitalWrite(True)
        End If
        If input="1,OFF" Then
            Relay1.DigitalWrite(False)
        End If
        If input="3,ON" Then
            Relay2.DigitalWrite(True)
        End If
        If input="3,OFF" Then
            Relay2.DigitalWrite(False)
        End If
    End If
        'Log(Buffer)
End Sub
 

Attachments

  • Nextion.zip
    417 KB · Views: 964
Top