B4R Question Delay Problem

Mostez

Well-Known Member
Licensed User
Longtime User
Hi,
this code for reading RFID card and send reading to serial port, It was working very well, I added new sub to generate audible confirmation, but I have got incorrect output. any ideas?

B4X:
private Sub ReadRFID(Buffer() As Byte)
    Dim tmpBuffer () As Byte
    Dim Str_HexNumber As String
    Dim Int_Number As ULong
   
    If BC.ArrayCompare(Buffer,BufferLast) = 0 Then 'compare last read card to recent one
        Return
    End If
   
    tmpBuffer= BC.SubString2(Buffer,1,11) 'get card bytes only, get ride of checksum and start byte
    Str_HexNumber = BC.StringFromBytes(tmpBuffer)
    Int_Number = Bit.ParseInt(Str_HexNumber, 16) 'convert hex to decimal string
    Log(NumberFormat (Int_Number,10,0))

    BC.ArrayCopy(Buffer,BufferLast)                    'copy last read to recent one, for next comparison               
    NoReadingTimerTicks = 0 'reset timer
    BeepConfirm 'errors came after adding this
End Sub

private Sub NoReadingTimer_Tick()
    NoReadingTimerTicks = NoReadingTimerTicks + 1 'avoid reading same card withing 5 seconds
    Select NoReadingTimerTicks
        Case 5                                       'reset comparison array
            Dim m As Byte
            For m = 0 To 13
                BufferLast(m) = 0
            Next
            NoReadingTimerTicks = 0
    End Select
End Sub

private Sub BeepConfirm()
Buzzer.DigitalWrite (True)
Delay(50)
Buzzer.Digitalwrite(False)

Delay(25)

Buzzer.DigitalWrite (True)
Delay(50)
Buzzer.Digitalwrite(False)
End Sub

Thanks
 

Mostez

Well-Known Member
Licensed User
Longtime User
Hi Erel,

B4X:
Astream.Initialize(RFID.Stream ,"ReadRFID","RFIDerr")

I mean that "RFIDerr" error handler is fired and I get "0" in output, but sometimes i get card id after then, in other words the right card id mixed with errors, it is not stable

B4X:
private Sub RFIDerr() ' if error, log 0
    Log("0")
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use delay. One possible solution:
B4X:
Sub BeepConfirm
 Buzzer.Digitalwrite(True)
 CallSubPlus("B44pConfirm2", 50, 0)
End Sub


Sub BeepConfirm2 (u As Byte)
 Buzzer.Digitalwrite(False)
 CallSubPlus("B44pConfirm3", 25, 0)
End Sub

Sub BeepConfirm3 (u As Byte)
 Buzzer.Digitalwrite(True)
 CallSubPlus("B44pConfirm4", 50, 0)
End Sub

Sub BeepConfirm4 (u As Byte)
 Buzzer.Digitalwrite(False)
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
it works, Thanks, here are some little modifications, these 4 subs handle buzzer and delay as you suggested

B4X:
Sub Beep()
Buzzer.Digitalwrite(True)
CallSubPlus("BeepPause",BeepDuration1, 0)
End Sub

Sub BeepPause (u As Byte)
Buzzer.Digitalwrite(False)
CallSubPlus("Beep2", BeepPauseMs, 0)
End Sub

Sub Beep2 (u As Byte)
Buzzer.Digitalwrite(True)
CallSubPlus("BeepStop", BeepDuration2, 0)
End Sub

Sub BeepStop (u As Byte)
Buzzer.Digitalwrite(False)
End Sub

if you we want to beep ok or click (single short beep), confirm(two short beeps with short pause) and error(two long beeps with long pause) we call these subs, we may change timing to get desired beeps. i don't have buzzer right now, i tested it with LED, cant wait to here it sounds :)

B4X:
private Sub BeepClick()
    BeepDuration1 = 70
    BeepPauseMs = 0
    BeepDuration2= 0
    Beep
End Sub

private Sub BeepConfirm()
    BeepDuration1 = 50
    BeepPauseMs = 25
    BeepDuration2= 50
    Beep
End Sub

private Sub BeepError()
    BeepDuration1 = 150
    BeepPauseMs = 200
    BeepDuration2= 150
    Beep
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Hi, Erel
sorry for taking some of your time, i tested the code with three different types, first with no display at all, second with lcd shield and third with Adafruit OLED. tests 1 & 2 worked ok, but 3 keeps starting over in infinite loop! and buzzer beeps forever! don't know may be timers conflict!

B4X:
Private Sub AppStart   
    NoReadingTimer.Initialize("NoReadingTimer_Tick",1000) 'prevent read same card within 5 seconds
    RandomDisplayTimer.Initialize("RandomDisplayTimer_Tick",1000) 'display hello at random positions if no activity within 30 seconds
   
    NoReadingTimer.Enabled = True 
    RandomDisplayTimer.Enabled = True
   
    RFID.Initialize(9600,2,3)
    Buzzer.Initialize(4,Buzzer.MODE_OUTPUT)      'changed to pin 8 and 11, nothing changed
    Astream.Initialize(RFID.Stream ,"ReadRFID","RFIDerr")
    Serial1.Initialize(9600)
    ssd.InitializeI2C(5, 0x3c)
                DisplayGreetings  '>>> loop from here, even if i remove delay
                     Delay(2000)
                    DisplayStarting
                    Delay(1000)
                    DisplayPleaseProximate
               Delay(1000)  '>>> to here
    BeepClick      ' <<<buzzer beebs forever,  if i comment this line and beep subs it works ok, but with no beeps
End Sub
 
Upvote 0
Top