![]() |
Forum Index : Microcontroller and PC projects : DIN and CINT and INTL
Author | Message | ||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 ZealandPosts: 9610 |
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. ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
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: AustraliaPosts: 6283 |
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 ZealandPosts: 9610 |
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: AustraliaPosts: 1642 |
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? Thanks Jim Bill Keep safe. Live long and prosper. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
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: AustraliaPosts: 1642 |
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. Do mind if I put this in the Library? Bill Keep safe. Live long and prosper. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Please do. Jim VK7JH MMedit |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
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): Geoff Graham - http://geoffg.net |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
@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: AustraliaPosts: 6283 |
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: AustraliaPosts: 1642 |
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 KingdomPosts: 1702 |
Thank you everyone ![]() |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
I just realised that my version will also need your fixes: Geoff Graham - http://geoffg.net |
||||
paceman Guru ![]() Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
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 Greg |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1642 |
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. Keep safe. Live long and prosper. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |