B4R Question Button - Am I doing something wrong?

Discussion in 'B4R Questions' started by hatzisn, Jul 16, 2019.

  1. hatzisn

    hatzisn Active Member Licensed User

    Hi everyone,

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

    Code:
    #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
     
  2. janderkan

    janderkan Active Member Licensed 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.
     
  3. hatzisn

    hatzisn Active Member Licensed User

    This is the schematic:

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

    I changed the corresponding line in to this:

    Code:
    buttonout.Initialize(35, buttonout.MODE_INPUT_PULLUP)
    and I am still getting the same results.
     
  4. janderkan

    janderkan Active Member Licensed User

    You must connect the blue line to ground.
     
    KMatle likes this.
  5. hatzisn

    hatzisn Active Member Licensed 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:

    Code:
    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)

    Code:
    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice