B4R Tutorial Button Example

SS-2016-04-11_11.44.41.png


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
 

Martin Larsen

Active Member
Licensed User
Longtime User
That will only work if btn_StateChanged can only be called one at a time. Is that the case?
 

Beja

Expert
Licensed User
Longtime 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
Longtime 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.
 

Beja

Expert
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime 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
Longtime 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 ?
 

BaGRoS

Active Member
Licensed User
Longtime 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
 

BaGRoS

Active Member
Licensed User
Longtime 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

Active Member
Licensed User
Longtime 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