B4R Question Button - Am I doing something wrong?

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi everyone,

continuing my tests I test a button. This is the code of the program:

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

Sub Process_Globals

    Public Serial1 As Serial

    Dim tm As Timer
    Private buttonin, buttonout As Pin
  
End Sub

Private Sub AppStart

    Serial1.Initialize(115200)
    Log("AppStart")


    buttonin.Initialize(34, buttonin.MODE_OUTPUT)
    buttonout.Initialize(35, buttonout.MODE_INPUT)
  
  
    tm.Initialize("tm_Tick", 1000) '1000ms = 1 second
  
    buttonout.AddListener("buttonout_StateChanged")
    buttonout.DigitalWrite(False)
  

    'Enable trigger with Timer interval

 
End Sub

Private Sub buttonout_StateChanged(State As Boolean)
    Delay (300)
    Log(State)
  
  
    tm.Enabled = State
  
  
    If State = True Then
        Log("Button Pressed")
    Else
        Log("Button Not Pressed")
    End If
  
End Sub


Sub tm_Tick
    Log(Millis)
End Sub


And these are the logs:

AppStart
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0
Button Not Pressed
1
Button Pressed
0


In these logs I 've never pressed the button.

When I press it it stays in :
0
Button not Pressed

and never see any millis. Am I doing something wrong or is it poor quality of the button?

Thanks in advance
 

janderkan

Well-Known Member
Licensed User
Longtime User
You have to provide a schematic so we can see your wiring.

You get statechanged event because your input is floating, you must hold it high or low with a resistor.
An input can be normally high and then you should pull it to ground with the button or
it is low and the you pull it high with the button.
If you dont use a resistor you can initialize with MODE_INPUT_PULLUP and use the built in resistor and pull down with the button.

The delay in statechanged event is compensating for a bad button but 10 millis should be enough.
You dont see the millis because when you enable the timer the tick event appears after 1000 millis
and before that the button is released and the timer is disabled again.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
This is the schematic:

upload_2019-7-16_18-15-51.png


I changed the corresponding line in to this:

B4X:
buttonout.Initialize(35, buttonout.MODE_INPUT_PULLUP)

and I am still getting the same results.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Got that working. There were two errors apparent.
1) First blue wire goes to GND
2) I had to remove this line of code:

B4X:
buttonout.DigitalWrite(False)

And another thing I had to do was to handle the bouncing voltage with this code as described in the b4r manual (v1.1)

B4X:
Private Sub buttonout_StateChanged(State As Boolean)
        
    If State = False Then
        If Millis - BounceMillis < BounceDelay Then
            Return
        Else
            Log(CRLF)
            Log(Millis)
            Log("Button Pressed")
            Log("Checking distance")
            CheckDistance
            BounceMillis = Millis
        End If
    End If
    
End Sub
 
Upvote 0
Top