Transform an LCD screen into a clock and calendar

Marc DANIEL

Well-Known Member
Licensed User
2 more B4R applications by B4A: Transform an LCD screen into a clock and calendar with a single click on your Smartphone

LCD_Clock_MiniFritzing.jpg


Calendar-Clock.jpg





B4A:
Sub CLOCK_Click
    Msgbox2Async("Be careful, you will turn the LCD screen into a clock and it will remain in this state until a RESET !","Do you confirm ?", "Yes", "", "No", Null, True)
    Wait For Msgbox_Result (Result As Int)
    If Result=DialogResponse.POSITIVE Then
        Dim jour As Int = DateTime.GetDayOfWeek(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(jour))
        Sleep(500)
        Dim day As Int = DateTime.GetDayofMonth(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(day))
        Sleep(500)
        Dim month As Int = DateTime.GetMonth(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(month))
        Sleep(500)
        Dim year As Int = DateTime.GetYear(DateTime.Now)
        AN = NumberFormat(year,0,0)
        ThirdDigit = AN.SubString2(3,4)
        FourthDigit = AN.SubString2(4,5)
        B4XPages.MainPage.BTA.Write(Array As Byte(ThirdDigit))
        Sleep(500)
        B4XPages.MainPage.BTA.Write(Array As Byte(FourthDigit))
        Sleep(500)
        Dim hours As Int = DateTime.GetHour(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(hours))
        Sleep(500)
        Dim minutes As Int = DateTime.GetMinute(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(minutes))
        Sleep(500)
        Dim seconds As Int = DateTime.GetSecond(DateTime.Now)
        B4XPages.MainPage.BTA.Write(Array As Byte(seconds))
    End If
End Sub
B4R:
#Region Project Attributes
    #AutoFlushLogs: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private SoftwareSerial1 As SoftwareSerial
    Private astream As AsyncStreams
    Private LCD As LiquidCrystal_I2C
    Private LCD As LiquidCrystal_I2C
    Public h, m, s,  L, X, fois=0 As UInt
End Sub
    

Private Sub AppStart
    Serial1.Initialize(115200)
    SoftwareSerial1.Initialize(9600,11,12)
    Log("Démarrage")
    astream.Initialize(SoftwareSerial1.Stream, "astream_NewData", Null)
    LCD.Initialize(0x27, 20,4) ' Initialisation de l'écran LCD avec 4 lignes de 20 caractères
    LCD.Backlight = True
    LCD.Write("   LCD SCREEN 20x4  ")
    LCD.SetCursor(0,1)
    LCD.Write( "  BLUETOOTH  READY" )
    LCD.SetCursor(0,2)
    LCD.Write("     WAITING FOR ")
    LCD.SetCursor(0,3)
    LCD.Write(" ANDROID CONNECTION")
End Sub

Sub AStream_NewData (Buffer() As Byte)
    L= Buffer.Length
    If L = 1 Then X = Buffer(0)
    If L = 2 Then X = Buffer(0)
    If L = 5 Then X = Buffer(4)
    Log("Buffer: ", L ," octets")
    Log("Valeur de  X: ", X)
    fois = fois +1
    
    Select fois
        Case 1
            LCD.Clear
            Select X
                Case 1
                    LCD.SetCursor(0,0)
                    LCD.Write(" SUNDAY ")
                Case 2
                    LCD.Write(" MONDAY ")
                Case 3
                    LCD.Write(" TUESDAY ")
                Case 4
                    LCD.Write("WEDNESDAY ")
                Case 5
                    LCD.Write(" THURSDAY ")
                Case 6
                    LCD.Write(" FRIDAY ")
                Case 7
                    LCD.Write("SATURDAY ")
            End Select
        Case 2
            LCD.Write(X)
        
        Case 3
            Select X
                Case 1
                    LCD.Write(" JANUARY")
                Case 2
                    LCD.Write(" FEBRUARY")
                Case 3
                    LCD.Write(" MARCH")
                Case 4
                    LCD.Write(" APRIL")
                Case 5
                    LCD.Write(" MAY")
                Case 6
                    LCD.Write(" JUNY")
                Case 7
                    LCD.Write(" JULY")
                Case 8
                    LCD.Write(" AUGUST")
                Case 9
                    LCD.Write(" SEPTEMBER")
                Case 10
                    LCD.Write(" OCTOBER")
                Case 11
                    LCD.Write(" NOVEMBER")
                Case 12
                    LCD.Write(" DECEMBER")                               
            End Select
        Case 4 'YEAR - 3rd digit out of 4
            LCD.SetCursor(8,1)
            LCD.Write ("20")
            LCD.Write(X)
        Case 5
            'YEAR - 4th digit out of 4
            LCD.Write(X)   
        Case 6 'HOURS
            h = X
        Case 7 'MINUTES
            m = X
        Case 8 'SECONDS
            s = X
            Affichage
    End Select
End Sub


Private Sub Affichage
    'When you have transformed the LCD screen into a clock,
    ' this module runs in a loop every second and you can no longer act
    ' from your android device unless you press the RESET button on the ARDUINO board
    LCD.setcursor(0,3)
    LCD.Write("                    ")
    LCD.SetCursor(6,3)
    s = s + 1
    If s = 60 Then
        s = 0
        m = m + 1
    End If
    If m = 60 Then
        m = 0
        h = h + 1
    End If
    If h = 24 Then h = 0
    LCD.Write(NumberFormat(h, 2, 0))
    LCD.Write(":")
    LCD.Write(NumberFormat(m, 2, 0))
    LCD.Write(":")
    LCD.Write(NumberFormat(s, 2, 0))
    Delay(1000)
    CallSubPlus("Affichage",0,0)   
End Sub
 
Last edited:
Top