Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:56 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : DIN and CINT and INTL

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:22pm 01 Jan 2019
Copy link to clipboard 
Print this post

I want to use a pin connected to a pushbutton switch
If I press the button once the program jumps to a sub
if I hold the button down for say more than 2 seconds then it jumps to a different sub
Would I use DIN, CINT or INTL to do this?
I know I would normally use something like setpin 33, intl, sub1, pullup 'Button pressed once
but I'm not sure how to include a timer to count if the button is pressed longer than the set time
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 10:13pm 01 Jan 2019
Copy link to clipboard 
Print this post


SETPIN 33,INTL,SUB1,PULLUP



SUB SUB1
'Do your immediate button push stuff here
TIMER=0 'Ensure timer is zero before we start timing
DO
IF PIN(33) THEN 'Button released
FLAG=0 'Clear flag
EXIT DO 'Hop out of this loop
ENDIF
FLAG=1 'Set flag if button NOT released
LOOP UNTIL TIMER=2000 'End loop after two seconds
IF FLAG THEN 'We get here cos the button was held down for two seconds...
'Do your two-second code here
ENDIF
END SUB


Untested, but it should work.
I will try to set this up on an E28 later to confirm it, but if you plop this into your code, you will probably be able to test it faster then I can right now.

EDIT: Technically, this is a little naughty, as you are not supposed to really do any running of code inside an interrupt.

However, if it worked, you could then just set a flag and exit, then deal with the two second press stuff as part of your main loop. But as you HAVE to hang around for at least two seconds in the interrupt to monitor the button(unless it has been released), then.... It would probably still be a good idea to just set another flag after the two second period, then exit - and catch that flag in your main loop when the interrupt returns. I can show you how to do that, but as practise, you might like to do that yourself. Edited by Grogster 2019-01-03
Smoke makes things work. When the smoke gets out, it stops!
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 10:58pm 01 Jan 2019
Copy link to clipboard 
Print this post

Your too fast for me Grogster,

This is the way I have done it in the past but I like your use of the Timer, that did not occur to me.

SETPIN 14, INTL, sub1, PULLUP 'Button pressed once
'
DO
' do something
LOOP

SUB sub1
LOCAL cnt% = 0
DO
PAUSE 100
IF PIN(14) = 1 THEN EXIT DO
cnt% = cnt% + 1
LOOP UNTIL cnt% = 25
'
IF cnt% < 21 THEN
sub2
ELSE
sub3
END IF
END SUB

SUB sub2
PRINT "Switch on for less than 2 seconds"
END SUB

SUB sub3
PRINT "Switch on for more than 2 seconds"
END SUB


Bill
Keep safe. Live long and prosper.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:07pm 01 Jan 2019
Copy link to clipboard 
Print this post

  Quote   DIM buttontimer, bt

SETPIN 33,INTB,subB,PULLUP

DO
IF bt = 1 THEN sub1
IF bt = 2 THEN sub2
LOOP


SUB subB
LOCAL b
IF PIN(33) = 0 THEN ' button down
buttontimer = TIMER()
ELSE 'button up
b = TIMER() - buttontimer ' this is how long the button was down
IF b < 100 ' too short, must be contact bounce
bt = 0'do nothing
ELSEIF b < 2000 ' short press
bt = 1 ' call first sub
ELSE ' longer than ~two seconds
bt = 2 ' call second sub
ENDIF
END SUB


Interrupt on both high and low but decide what to do on button release.
Interrupt kept short and flag set ready for main loop.

Jim
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 11:11pm 01 Jan 2019
Copy link to clipboard 
Print this post

Elegant.

Better then my example. Use Jim's code.
Smoke makes things work. When the smoke gets out, it stops!
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 12:12am 02 Jan 2019
Copy link to clipboard 
Print this post

Yes, very elegant. I couldn't wait to try it but had to make some changes to make it work (apart from the pin number). I'll add it to my collection of useful routines. Should it go into the Library?

  Quote  DIM buttontimer, bt

SETPIN 14,INTB,subB,PULLUP

DO
IF bt = 1 THEN sub1
IF bt = 2 THEN sub2
bt =
0
LOOP


SUB subB
LOCAL b
IF PIN(14) = 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"
END SUB

SUB sub2
PRINT "Switch on for more than 2 seconds"
END SUB


Thanks Jim

Bill

Keep safe. Live long and prosper.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:54am 02 Jan 2019
Copy link to clipboard 
Print this post

I see you added the BT = 0 line.
I thought of that later. I usually have that sort of thing in the sub but in this case, where you put it is ideal.

I would still put a de-bounce capacitor of ~0.001uF across the switch contacts.

Jim
VK7JH
MMedit
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 02:22am 02 Jan 2019
Copy link to clipboard 
Print this post

Hi Jim,

There was also a missing ENDIF and brackets () after timer. I did some more playing and found that I did put the:

bt = 0

instruction in the wrong place - depending on when the interrupt happened bt could be set to zero before being tested and sub1 or sub2 not being called - making the operation intermittent. I have put it at the end of both sub1 and sub2.

  Quote  ' Dual Function Push Button
' Original code by
' Tassyjim
'
DIM INTEGER buttontimer, 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
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"
bt =
0
END SUB

SUB sub2
PRINT "Switch on for more than 2 seconds"
bt =
0
END SUB



Do mind if I put this in the Library?

Bill
Keep safe. Live long and prosper.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 03:13am 02 Jan 2019
Copy link to clipboard 
Print this post

  Turbo46 said  
Do mind if I put this in the Library?

Bill


Please do.

Jim
VK7JH
MMedit
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 05:00am 02 Jan 2019
Copy link to clipboard 
Print this post

Jim's code works well and has the great advantages of not messing with the TIMER value and not hanging around in an interrupt.

As a slight improvement this would be a good opportunity to use the new STATIC variables (introduced in Ver 5.04.09) for buttontimer (as follows):

  Quote   DIM bt

SETPIN 33,INTB,subB,PULLUP

DO
IF bt = 1 THEN sub1
IF bt = 2 THEN sub2
LOOP


SUB subB
STATIC buttontimer
LOCAL b
IF PIN(33) = 0 THEN ' button down
buttontimer = TIMER()
ELSE 'button up
b = TIMER() - buttontimer ' this is how long the button was down
IF b < 100 ' too short, must be contact bounce
bt = 0'do nothing
ELSEIF b < 2000 ' short press
bt = 1 ' call first sub
ELSE ' longer than ~two seconds
bt = 2 ' call second sub
ENDIF
END SUB

Geoff Graham - http://geoffg.net
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 06:01am 02 Jan 2019
Copy link to clipboard 
Print this post

@Geoff Yes, a good demo of the reason you added it. It won't work on the Maximite (yet) though.

@TassyJim My copy of MMEdit is the latest version but it doesn't know about the STATIC instruction. I'm using the Micromite_MK2_V_5.3 syntax and that is the latest I have (I think). I Know the versions numbers have changed for some reason. Is there a later version of the .dta file somewhere>

Bill
Keep safe. Live long and prosper.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 06:26am 02 Jan 2019
Copy link to clipboard 
Print this post

  Turbo46 said  
@TassyJim My copy of MMEdit is the latest version but it doesn't know about the STATIC instruction. I'm using the Micromite_MK2_V_5.3 syntax and that is the latest I have (I think). I Know the versions numbers have changed for some reason. Is there a later version of the .dta file somewhere>

Bill

Not yet.
There is a lot of typing involved and I am not looking forward to it.
Or, to be more accurate, I am just slack.

I will probably change the way I do the syntax and syntax help files to make it easier with the ever growing range of devices to support.

Jim
VK7JH
MMedit
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 07:28am 02 Jan 2019
Copy link to clipboard 
Print this post

The program is now in the library see here.

I've not tested a version using the STATIC command I'll add that later.

Bill
Keep safe. Live long and prosper.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:32am 02 Jan 2019
Copy link to clipboard 
Print this post

Thank you everyone
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 09:33am 02 Jan 2019
Copy link to clipboard 
Print this post

I just realised that my version will also need your fixes:

  Quote  a missing ENDIF, brackets () after TIMER and reposition the bt = 0 instruction

Geoff Graham - http://geoffg.net
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 10:37am 02 Jan 2019
Copy link to clipboard 
Print this post

Hi Bill,
had a look at your entry in the "Fruitoftheshed" library and there seems to be a small typo. You've entered "....and/or a 100nF (.001uF) connected across the switch contacts." I think you mean 1nF not 100nF. Jim's suggestion was
  TassyJim said   I would still put a de-bounce capacitor of ~0.001uF across the switch contacts.


Greg
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 10:57am 02 Jan 2019
Copy link to clipboard 
Print this post

Thanks Greg,

It is a typo (or brain fade) but I did mean 100nF and that should be 0.1uF NOT .001uF. This is the value that I use and also what Geoff recommends in his "Getting Started with the Micromite" document in the "Switch Inputs" section. I will fix it.

Thanks again
Bill

Edit: Fixed.
Second Edit: Tested using the STATIC command as per Geoff's suggestion and added to the Library.Edited by Turbo46 2019-01-04
Keep safe. Live long and prosper.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025