B4R Tutorial Button Example

Erel

Administrator
Staff member
Licensed User


B4X:
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(13, led.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
 

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:
B4X:
Sub btn_StateChanged (State As Boolean)
   Delay(20) 'wait for 20 milliseconds
 ...
End Sub
 

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
 

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:
B4X:
led2.Initialize(13, led.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.
 

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.
 

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:

B4X:
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(7, led.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:

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?
 

klaus

Expert
Licensed User
B4X:
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
 

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.
 

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 ?
 

Erel

Administrator
Staff member
Licensed User
You should use Arduino IDE v1.6.11 or 1.6.13. Not 1.6.12.
 

BaGRoS

Member
Licensed User
Hi.
I want waiting for press and release button

B4X:
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
 

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

Member
Licensed User
I changed for this:

B4X:
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
 

embedded

Member
Licensed User
Actually Pin13 LED of Uno is Connected to +5V Supply....Just like pull up mode....Whenever Pin13 set as Output....by Default it set to low....So led glow whenever output is low...if you dont want to glow it....set pin 13 to high..
 
Top