B4R Question (Solved)Addlistener problem...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
I am trying to count the number of state changes (positive going edges) on one of the pins of Wemos using addlistner method. For testing purposes, I am using another pin of Wemos to generate the pulses , naturally shorting both the pins. Expecting that one timer generates the pulses another timer displays the count. But this does not seem to work. My test code is as follows:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private ip,op As Pin
   Private wemos As D1Pins
   Dim count As Int
   Dim timer1 As Timer
   Dim timer2 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   ip.Initialize (wemos.D5, ip.MODE_INPUT)
    op.Initialize (wemos.D6, op.MODE_OUTPUT)
   ip.AddListener("Change_state")
   timer1.Initialize("timer1_tick",2000)
   timer2.Initialize("timer2_Tick",1000)
timer1.Enabled=True
timer2.Enabled=True
End Sub

Sub timer1_tick
   
    Log(count)
    count=0
End Sub
Sub timer2_tick
    op.DigitalWrite(True)
    Delay(100)
    op.DigitalWrite(False)
    Delay(100)
End Sub
Sub Change_state (State As Boolean)

       
  Select Case State
      Case True
        Log("state: Detected")
        count=count+1   
    Case False
        Log("state: Not Detected")
  End Select
End Sub

Note that the timers are initialised such that the generating timer is faster than the displaying timer.
 

klaus

Expert
Licensed User
Longtime User
I used this code to test.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private ip, op As Pin
    Private wemos As D1Pins
    Dim count As Int
    Dim timer1 As Timer
    Dim timer2 As Timer
      
    Dim Set = False As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ip.Initialize (wemos.D5, ip.MODE_INPUT)
    op.Initialize (wemos.D6, op.MODE_OUTPUT)
    op.AddListener("Change_state")
    timer1.Initialize("timer1_tick",2000)
    timer2.Initialize("timer2_Tick",200)
    timer1.Enabled=True
    timer2.Enabled=True
End Sub

Sub timer1_tick  
    Log(count)
    count=0
    Set = False
End Sub

Sub timer2_tick
    Set = Not(Set)
    op.DigitalWrite(Set)
End Sub

Sub Change_state (State As Boolean)
    Select State
    Case True
        Log("state: Detected")
        count=count+1  
    Case False
        Log("state: Not Detected")
    End Select
End Sub
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
Thanks a lot. But I have a naive question! I am expecting to count the ip pin( pin configured as input) states (connected to a device like a flowmeter , which generates pulses) and the code by Klaus seems to count the op pin state. I am confused as to where do we connect the flowmeter output! The code tracks the state of the pin internally, it seems.

Update:
The following code solved the problem! Thanks to Erel and Klaus!
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private ip, op As Pin
    Private wemos As D1Pins
    Dim count As Int
    Dim timer1 As Timer
    Dim timer2 As Timer
     
    Dim Set = False As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ip.Initialize (wemos.D5, ip.MODE_INPUT)
    op.Initialize (wemos.D6, op.MODE_OUTPUT)
    ip.AddListener("Change_state")
    timer1.Initialize("timer1_tick",1000)
    timer2.Initialize("timer2_Tick",1)
    timer1.Enabled=True
    timer2.Enabled=True
End Sub

Sub timer1_tick 
    Log(count)
    count=0
    Set = False
End Sub

Sub timer2_tick
    Set = Not(Set)
    op.DigitalWrite(Set)
End Sub

Sub Change_state (State As Boolean)
    Select State
    Case True
        'Log("state: Detected")
        count=count+1 
    Case False
        'Log("state: Not Detected")
    End Select
End Sub

Just changed the code given by Klaus a little bit as I wanted to track the changes on ip pin!
 
Last edited:
Upvote 0
Top