Menu

Help using a switch

Help
Chris
2010-02-03
2013-05-30
  • Chris

    Chris - 2010-02-03

    I'm new to the whole microcontroller world, and have learned quite a lot in the last few days.  I'm trying to take a momentary push button, and have it start a subroutine when pressed for a short period of time, and possibly another subroutine when it's pushed for a long period of time.

    A quick and dirty snippet and explanation would be greatly appreciated.

     
  • Hugh Considine

    Hugh Considine - 2010-02-06

    I had to write a section of code to do just that the other day:

    '''Measure button A hold time
    '''@param MaxTime Maximum number of milliseconds to wait
    Function TimeA (Optional In MaxTime As word = 65535) As word
        TimeA = 0
        Wait Until BUTTON_A = 1
        Do Until BUTTON_A = 0
            Wait 1 ms
            If TimeA < MaxTime Then
                TimeA = TimeA + 1
            Else
                Exit Sub
            End If
        Loop
    End Function
    

    In this code, BUTTON_A is a constant referring to an I/O pin. To run a different section of code, you'd need something like this:

    Dim ButtonTime As Word
    ButtonTime = TimeA
    Select Case ButtonTime
        'Pressed for less than 500 ms
        Case < 500
            SubA
        'Pressed for 500 to 1000 ms
        Case < 1000
            SubB
    End Select
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.