[CCC DEV] The three button problem - alternative approach.

Steve Pretty steve.g.pretty at btinternet.com
Mon Feb 21 22:19:52 GMT 2011


I recently looked at allowing "Plumbing" to deal with more than  two 
buttons.  I came up with an approach using the PCINTs.

Another approach would be to retain the external interrupt approach for 
arduino pins 2 and 3, and offer a timed poll alternative for additional 
pins.

The PCINT approach is complex, as a PCINT deals with a whole 8 bit port. 
It seems likely that the User Interface for plumbing processes handling 
PCINT based inputs would be rather more complex than the current 
external interrupt driven routines. The timed poll approach can be 
implemented without any change to the interface. It offers the 
additional advantage of implementing debounce "for free".

Down sides are variable latency (though on average half the latency of a 
debounced interrupt using the same period for debounce) and increased 
CPU overhead.

Here is a version of digital.input that provides external interrupt 
drivers for arduino pins 2 and 3, and timed poll for additional ports. 
This seems to be working well.

PROC digital.input (VAL INT board.pin, CHAN LEVEL out!)
   INITIAL INT avr.pin IS board.to.chip(board.pin):
   INITIAL INT vintr IS (- 1):
   LEVEL level, old:

   IF
     (board.pin = 2) OR (board.pin = 3)
       SEQ
         #PRAGMA DEFINED vintr
         set.interrups (avr.pin, vintr)
         WHILE TRUE
           SEQ
             INT any:
             wait.for.interrupt (vintr, any)
             digital.read (board.pin, level)
             out ! level
     TRUE
       SEQ
         digital.read (board.pin, level) -- read initial pin value
         out ! level
         old := level
         WHILE TRUE
           SEQ
             delay (50)
             digital.read (board.pin, level)
             IF
               level <> old
                 SEQ
                   out ! level
                   old := level
               TRUE
                 SKIP
:


And here is the test case  (I have buttons on arduino pins 2, 3, 4 and 
5, LEDs on pins 9, 10, 11 and 12):

#INCLUDE "plumbing.module"

-- Test case Updated version of plumbing digital.input
-- Where arduino pins 2 and 3 are handled by interrupt and
-- Other pins are handled by timed poll.

PROC main ()
   CHAN LEVEL w, x, y, z:
   PAR
     digital.input (2, w!)
     digital.output (9, w?)

     digital.input (3, x!)
     digital.output (10, x?)

     digital.input (4, y!)
     digital.output (11, y?)

     digital.input (5, z!)
     digital.output (12, z?)
:





More information about the developers mailing list