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

Discussion in 'B4R Questions' started by mark35at, Jul 18, 2018.

  1. mark35at

    mark35at Well-Known Member Licensed 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:

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

    Cableguy Expert Licensed 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
     
    mark35at and BillMeyer like this.
  3. mark35at

    mark35at Well-Known Member Licensed 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.

    Code:
    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: Jul 18, 2018
    tigrot, Johan Hormaza and Cableguy like this.
  4. tigrot

    tigrot Well-Known Member Licensed User

    I was thinking about this post. Why not simply use btn_StateChanged to switch motor on and off? On state=on activate relay , on Off deactivate. It's an event and this doesn't block thread.
     
  5. mark35at

    mark35at Well-Known Member Licensed User

    The button controls a stepper motor not a relay. I need the timer to switch the coils but theoreticall you are right. :D
     
  6. Cableguy

    Cableguy Expert Licensed 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.
     
  7. mark35at

    mark35at Well-Known Member Licensed User

    I will find out next week when my hardware comes and I can try the code.
     
  8. tigrot

    tigrot Well-Known Member Licensed User

    For this a 100ms delay is more than enough. But how can a micro GPIO line control a stepper motor directly?
     
  9. Cableguy

    Cableguy Expert Licensed User

    Usually it doesn't. It controls a stepper driver.
     
    tigrot likes this.
  10. mark35at

    mark35at Well-Known Member Licensed 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.
     
    tigrot likes this.
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