|
Forum Index : Microcontroller and PC projects : Help needed for INTERRUPT
| Author | Message | ||||
| Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 979 |
Hi to all, I have a Micromite mainprogram which calls a SUB with a LOOP and a long PAUSE (25 seconds). I would like to leave this subroutine prematurely by pressing a key (SetPin nButton_ESC, DIN, INTL, ESCAPE, PULLUP) - how can I do this? I am grateful for any help... Frank |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3466 |
Maybe without an interrupt, loop through a bunch of short pauses (e.g., 100ms) and break out upon detecting keypress (or character available in input buffer). PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
just as lizby says. instead of one long pause of 25 seconds, do several smaller pauses in a loop and check for your exit condition i.e. don't do this... Pause 25000 this is better... do:loop until inkey$="" ' flush the key buffer to stop false exits. for n=1 to 250 if inkey$<>"" then exit for pause 100 next with this approach, you might need to balance the loop because your processing will take some time so on a long pause, you might find you need to loop for 245 instead of 250. Remember also that any other stuff that happens while you are in this loop will add to the pause time - i.e. an interrupt every second taking 100ms will add another 2.5 secs to your pause! If accuracy/time is reasonably important, consider doing something like this as your timing loop. this is better still... t=timer do pause 100 loop while (timer-t)<25000 and inkey$="" this way it doesn't really matter how long your processing takes (or what other interrupts occur while you are waiting), as soon as you have been in it for 25secs (plus a little bit) you drop out - or if a key arrives in the console. hth |
||||
MicroBlocks![]() Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
It is also best to have no pauses in subroutines at all. You do that in main. Without knowing what the rest of the program does it is difficult to give an example. I often use 'state machine' logic that is very suited if you need stuff to keep on running while waiting for other stuff to happen (like a key press). Do a search for 'discrete time state machine' to get some theory and examples. Once you get this way of programming, live gets a lot easier as often we have to program something monitoring multiple inputs, check timings and generate the correct outputs. Microblocks. Build with logic. |
||||
| Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 979 |
Thank you for your ideas! I thought it would be better with an interrupt solution than polling the key... A motor is moved in one direction for 25 seconds and then in the other direction for 25 seconds for an endurance test. The SUB includes only this test. To exit this test manually, I would need this function with the ESC key... Frank |
||||
| Tinine Guru Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
I couldn't agree more. It's a great way to pseudo multitask. My programs are never held-up with delays. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |