Shop OBEX P1 Docs P2 Docs Learn Events
Interrupt possible? — Parallax Forums

Interrupt possible?

Ruben JacobsRuben Jacobs Posts: 4
edited 2005-01-31 20:43 in BASIC Stamp
I am using the Basic Stamp 2 for a cleaning robot project. I would like to know if a interrupt is possible of some other method of being able to detect quickly if one of 5 microswitches are detected and perform a specific routine. I would also like to be able to give priority to specific buttons. If this is not possible I have to resort to a PIC microcontroller...

Rubenes

Post Edited (Ruben Jacobs) : 1/31/2005 6:19:59 PM GMT

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-31 18:24
    Depends on what you mean by "quickly".

    The BS2 has no hardware interrupts, and in general its software is single-tasking. However, Jon can show you a technique to implement a form of multi-tasking in the PBasic environment. This requires 'sampling' the signals of interest -- the BS2 can implement a sampling algorithm with a latency of less than 10 mSecs. Latencies of less than 1 mSec can be done with the BS2, but less than 100 uSec would be impossible.

    Basically, the approach has a 'main loop' which samples the signals of interest. Based on the samples, the 'main loop' calls subroutines to do the actual work, and the 'main loop' can maintain a state_variable to run a state machine in code.
  • Ruben JacobsRuben Jacobs Posts: 4
    edited 2005-01-31 18:36
    I mean by quickly within 0.5 seconds, otherwise the expensive robot would crash down the stairs. But that system would not detect if it was busy doing a routine, e.x. it bumped into a wall, turned, went forward and crashed down the stairs.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-31 18:48
    Interrupts are not possible with the BASIC Stamp.· Some have rigged circuits to reset the BASIC Stamp as a form of interrupt, but this is destructive to the current state of variables.· What you can do is craft you code such that you're able to scan the switches at a rate that will allow you to catch a problem.· The form I frequently recommend looks like this:

    Main:
      GOSUB Scan_Switches
      ON task GOSUB Task_0, Task_1, Task_2, Task_3
      GOTO Main
    


    As you can see, the program is broken into small tasks.· Between each task the Scan_Switches subroutine is called and can be dealt with.· Each task takes care up setting the next task, letting the program run in order, or if needed, in a new order to handle events that come up.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Ruben JacobsRuben Jacobs Posts: 4
    edited 2005-01-31 18:55
    There is only 1 little problem. Your system is great when you know which microswitch will activate, but not when. Which switch will activate in my system totally depends on the room etc. So maybe your system does not work.

    P.S.: Do you have any circuits to debounce switches? The BUTTON instruction is not very flexible.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-31 19:45
    This method does work well, I used it to create an alarm system that had to monitor and process simultaneous inputs.· My suggestion is that the Scan_Switches subroutine scan and debounce all five inputs and return a single value that you can deal with.· Here's how I do it; you'll have to adjust for your inputs:

    Switches    VAR    INL            ' switch inputs on P0 - P4, active-high  
     
    swStatus    VAR    Byte
    idx         VAR    Nib
     
    ...
     
     
    Scan_Switches:
      swStatus = %11111
      FOR idx = 1 TO 5
        swStatus = swStatus & Switches
        PAUSE 5
      NEXT
      RETURN 
    


    This routine scans all the switch inputs and if any stays active for the entire cycle (~25 ms), then it will be reported as active.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-01-31 19:59
    Good answers, Jon! To the OP: You can do what you are proposing with PIC processors (and for that matter, check out the SX/B. It's a 50 MIPS Uber-PIC with interrupts, assembly, and now a Basic compiler)

    However, the BS2 solution will be simpler and easier to code and debug. It's up to you at this point. 1/2 second is 500 mSecs, which is a Long time to the BS2.
  • Ruben JacobsRuben Jacobs Posts: 4
    edited 2005-01-31 20:36
    I do not have any money left, so I can't buy an SX/B.

    Thank you. I will see if I can get some software programmed.
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-31 20:43
    From what you've indicated, your program can be easily implemented in a BS2 using the strategies I've outlined above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
Sign In or Register to comment.