B4R Question How to implement a Do-while for a Pushbutton?

Mark Read

Well-Known Member
Licensed User
Longtime User
A simple question but I cannot find an answer in the forum. Using the Arduino Mega and a simple pushbutton or switch.

I need to add a pushbutton to my project and code it like this:

B4X:
While Pushbutton is being pressed

'Do this
turn on motor and move accordingly

end

Releasing the button would then stop the motor. I think I have to be carefull not to block the main thread.

The point of this is to move a horizontal arm slowly downwards to set the position of a mechanical end switch, clients request.

How can I code this? Suggestions please.

Thank you.
 

Cableguy

Expert
Licensed User
Longtime User
In the pushbutton event (true) Set a timer to true and to false when the push button is released.
Then in the timer tick do the needed ops. Timers do not block the main thread in b4r
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Great, thank you Cableguy. Simple question, simple answer.

Stealing a bit of code from Erel and tested on an Arduino Uno with a piece of wire.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private btn As Pin
    Private timer1 As Timer
    Private i As UInt=0
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
    btn.AddListener("Btn_StateChanged")
    timer1.Initialize("timer1_Tick",1000)
    Log("Waiting....")
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.
    If State=False Then
        timer1.Enabled=True
        Log("Button pressed")
    Else
        timer1.Enabled=False
        Log("Button released")
        i=0
    End If
End Sub

Sub timer1_Tick
    'turn on motor till button is release or whatever
    i=i+1
    Log("i= ",i)
End Sub
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
The button controls a stepper motor not a relay. I need the timer to switch the coils but theoreticall you are right. :D
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
One issue that came to mind is button de-bouncing! Doing it directly on the button event may give origin to erroneous behaviour. By setting a timer (the first tick will only happen after the interval set) this de-bouncing is automatically done... unless of course you do it the hard (ware) way.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I will find out next week when my hardware comes and I can try the code.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
The motor is controlled via a L293 chip, not directly of course. Normally the motor is controlled with a timer which has varied times, ie. the speed will be controlled in mm/s. The purpose of the switch is to override the automatic setting and operate the motor at a slow speed continuously while the button is being pressed. This is required by the client in order to setup vaious end switches before automatic operation.
 
Upvote 0
Top