' Dual Function Push Button ' Original code by: ' Tassyjim on The Back Shed Forum ' DIM integer bt dim integer SwPin = 14 ' Pin number for the push button input SETPIN SwPin,INTB,subB,PULLUP DO ' Main program goes here IF bt = 1 THEN sub1 IF bt = 2 THEN sub2 LOOP SUB subB STATIC integer buttontimer LOCAL integer b IF PIN(SwPin) = 0 THEN ' button down buttontimer = TIMER ELSE 'button up b = TIMER - buttontimer ' this is how long the button was down IF b < 100 THEN ' too short, must be contact bounce bt = 0'do nothing ELSEIF b < 2000 THEN ' short press bt = 1 ' call first sub ELSE ' longer than ~two seconds bt = 2 ' call second sub ENDIF ENDIF END SUB SUB sub1 PRINT "Switch on for less than 2 seconds" ' Funtion 1 code goes here bt = 0 END SUB SUB sub2 PRINT "Switch on for more than 2 seconds" ' Function 2 code goes here bt = 0 END SUB
' Dual Function Push Button version 2 ' Original code by: ' Tassyjim on The Back Shed Forum ' Uses the STATIC command (added in MMBasic V5.04.09) ' DIM integer bt dim integer SwPin = 14 ' Pin number for the push button input SETPIN SwPin,INTB,subB,PULLUP DO ' Main program goes here IF bt = 1 THEN sub1 IF bt = 2 THEN sub2 LOOP SUB subB STATIC buttontimer LOCAL integer b IF PIN(SwPin) = 0 THEN ' button down buttontimer = TIMER ELSE 'button up b = TIMER - buttontimer ' this is how long the button was down IF b < 100 THEN ' too short, must be contact bounce bt = 0'do nothing ELSEIF b < 2000 THEN ' short press bt = 1 ' call first sub ELSE ' longer than ~two seconds bt = 2 ' call second sub ENDIF ENDIF END SUB SUB sub1 PRINT "Switch on for less than 2 seconds" ' Funtion 1 code goes here bt = 0 END SUB SUB sub2 PRINT "Switch on for more than 2 seconds" ' Function 2 code goes here bt = 0 END SUB