B4R Tutorial Button Example

Discussion in 'B4R Tutorials' started by Erel, Apr 11, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private btn As Pin
       
    Private led As Pin
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    led.Initialize(13led.MODE_OUTPUT)
       btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP) 
    'Using the internal pull up resistor to prevent the pin from floating.
       btn.AddListener("Btn_StateChanged")
    End Sub

    Sub Btn_StateChanged (State As Boolean)
       
    Log("state: ", State)
       
    'state will be False when the button is clicked because of the PULLUP mode.
       led.DigitalWrite(Not(State))
    End Sub
     
  2. Martin Larsen

    Martin Larsen Member Licensed User

    Does the button object have built-in debouncing?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    There is no built-in debouncing. From my tests with physical buttons I didn't encounter any bouncing issues.

    If you do encounter such issue then just add a short delay to the StateChanged event:
    Code:
    Sub btn_StateChanged (State As Boolean)
       Delay(
    20'wait for 20 milliseconds
     ...
    End Sub
     
  4. Martin Larsen

    Martin Larsen Member Licensed User

    That will only work if btn_StateChanged can only be called one at a time. Is that the case?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  6. Beja

    Beja Expert Licensed User

    Yes.. it worked and I could successfully turn on and off the led using the button.
    The yellow wire was short I had to push the Arduino board towards the breadboard :p

    Buttonjpg.jpg
     
  7. Beja

    Beja Expert Licensed User

    I changed the pin number to pin 7 and connected it to external LED.. it worked bit the LED at pin 13 stayed on.. so I added this
    statement to turn it off on app start:
    Code:
    led2.Initialize(13led.MODE_OUTPUT)
    led2.DigitalWrite(
    False)
    That solved the problem.. I am just wondering how come there's no ClearAll function.. to flush the memory with zeros.
     
  8. klaus

    klaus Expert Licensed User

    I suspect that you left somewhere a reference to Led 13.
    You should show us your code to see what exactly you have done.
     
  9. Beja

    Beja Expert Licensed User

    Hi Klaus,
    Just reproduced it and this is what I did:

    First ran Erel's example as is and it worked fine.. LED(pin13) responded to button press.
    Then I changed the pin number to 7 and connected it to external LED. It worked but pin 13 stayed on.

    Following is the code where I changed the pin number to 7, without adding the code to clear pin13:

    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private btn As Pin
       
    Private led As Pin
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    led.Initialize(7led.MODE_OUTPUT)
       btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP) 
    'Using the internal pull up resistor to prevent the pin from floating.
       btn.AddListener("Btn_StateChanged")
    End Sub

    Sub Btn_StateChanged (State As Boolean)
       
    Log("state: ", State)
       
    'state will be False when the button is clicked because of the PULLUP mode.
       led.DigitalWrite(Not(State))
    End Sub
    P.S.
    I unplugged the power supply (USB) and plugged it back, but pin13 still on.
     
    Last edited: Jun 14, 2016
  10. klaus

    klaus Expert Licensed User

    From what I have experienced is that Led13 is ON by default.
     
    Beja likes this.
  11. Beja

    Beja Expert Licensed User

    It's as you said.. ON by default. the want to say there's an LED here :)
    In the example above, this #13 pin is age-triggered, which means it fires it's function in the rising and falling ages, resulting in ON and OFF with one button press. Is there a way to ignore the falling age?
     
  12. klaus

    klaus Expert Licensed User

    Code:
    Sub Process_Globals
        
    Public Serial1 As Serial
        
    Private pinButton As Pin            'pin for the button
        Private pinLED13 As Pin        'pin for the green Led
        Private LightOn = False As Boolean
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)

        pinButton.Initialize(pinButton.A5, pinButton.MODE_INPUT_PULLUP) 
    'Using the internal pull up resistor to prevent the pin from floating.
        pinButton.AddListener("pinButton_StateChanged")

        pinLED13.Initialize(
    13, pinLED13.MODE_OUTPUT)
        pinLED13.DigitalWrite(LightOn)  
    'sets LED13 according to LightOn
    End Sub

    Private Sub pinButton_StateChanged (State As Boolean)
        
    If State = False Then    'remember, False means button pressed.
            LightOn = Not(LightOn)
            pinLED13.DigitalWrite(LightOn)
        
    End If
    End Sub
     
    Erel and Beja like this.
  13. Beja

    Beja Expert Licensed User

    Thank you Klaus for the perfect solution.. now working like a charm.. only the switching noise that some times results in more than a click (effect) but this is solved by adding a 0.1 ╬╝F disk ceramic capacitor.
     
    Cableguy likes this.
  14. Bladimir Carrillo

    Bladimir Carrillo Member Licensed User

    I have this error when compile...

    B4R version: 1.50
    Parsing code. (0.00s)
    Compiling code. (0.03s)
    Building project (0.03s)
    Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 - COM4) Error
    Cargando configuraci├│n...
    java.lang.NullPointerException
    at processing.app.BaseNoGui.selectSerialPort(BaseNoGui.java:1088)
    at processing.app.helpers.CommandlineParser.parseArgumentsPhase1(CommandlineParser.java:143)
    at processing.app.Base.<init>(Base.java:273)
    at processing.app.Base.guardedMain(Base.java:219)
    at processing.app.Base.main(Base.java:132)

    Any idea ?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You should use Arduino IDE v1.6.11 or 1.6.13. Not 1.6.12.
     
  16. BaGRoS

    BaGRoS Member Licensed User

    Hi.
    I want waiting for press and release button

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
       
        
    Dim btn_Status As Boolean = False
          
    End Sub

    Private Sub AppStart

        btn.Initialize(btn.A5, btn.MODE_INPUT_PULLUP)
        btn.AddListener(
    "btn_StateChanged")

        btn_Wait

    End Sub

    'BTN
    '**********************
    Private Sub btn_StateChanged(State As Boolean)
        Delay(
    20)
        
    'Log("Status: ", State)
        btn_Status = Not(State)
        
    Log("Status: ",btn_Status)
           
    End Sub
    Private Sub btn_Wait
       
        
    Do While Not(btn_Status)
            
    'Log("Waiting for the press...")
            Delay(10)
        
    Loop
        
    Do While btn_Status
            
    'Log("Waiting for the release...")
            Delay(10)       
        
    Loop
    End Sub
    and not working as I want
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Don't try to hold the main thread in a loop. Events will not be raised. You should instead continue the program flow from the StateChanged event.
     
    BaGRoS likes this.
  18. BaGRoS

    BaGRoS Member Licensed User

    I changed for this:

    Code:
    Private Sub btn_StateChanged(State As Boolean)
        Delay(
    20)
        
    Log("Status: ", State)
       
        
    'if first push then False, and waiting for release
        If Not(btn_Status) Then
            
    'pushing
            btn_Status = True
        
    'if pushing already then release and going
        Else If btn_Status Then
            
    'for next check
            btn_Status = False
            progPhase = progPhase + 
    1
           
            
    Select progPhase
                
    Case 1
                    
    'start Timer for check speed motor
                    SpeedMotorTimer.Initialize("SpeedMotorTimer_Tick"10)
                    SpeedMotorTimer.Enabled = 
    True
                
    Case 2
                    
    'for future...
                    progPhase = progPhase - 1
            
    End Select
           
        
    End If
           
    End Sub
     
Loading...