Shop OBEX P1 Docs P2 Docs Learn Events
Counting 2 Button Presses in order to toggle LEDs — Parallax Forums

Counting 2 Button Presses in order to toggle LEDs

mrdontaymrdontay Posts: 3
edited 2007-06-27 20:27 in BASIC Stamp
How would I adapt this code below to respond to the following events:
The assumption is that I have 2 buttons, let's say b1 and b2:

b1 pressed·1 time·and b2 pressed once = led blinks
b1 presssed·1 time·and b2 pressed twice =led blinks

b1 pressed two times and b2 pressed once = led blinks

and so on ......

Do I need two counters? if so, how do I do it?

I have been trying to adapt the code below to do what I want without success. Can someone please help a rookie. Thanks

counter VAR Byte

Main:
counter = 0
DO
DEBUG HOME
DEBUG ? counter
IF IN3 =1 THEN
counter = counter + 1
HIGH 15
PAUSE 500
LOW 15
PAUSE 500
ENDIF
LOOP UNTIL counter = 8
'At this point, counter will be 3
'So the IF counter=3 will ALWAYS be true
HIGH 13
PAUSE 1500
LOW 13
PAUSE 500







·

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-06-27 19:06
    Hi mrdontay, if the number of button presses is always·3 and the number of buttons 2 you could accumulate their values until they equal 3. At a value of 3 combine the 2 accumulated values into a byte, button1 as the high nibble and button2 as the low nibble. Jump to a subroutine and process the result, after processing initialize the values back to zero and repeat. The drawback in this particular example does not keep track the order in which each press is made. Debug statements display accumulated values and result value.

    Button1 VAR Nib
    Button2 VAR Nib
    result VAR Byte
    main:
    DO WHILE IN4=0 AND IN5=0

    LOOP
    IF IN4=1 THEN
    Button1=Button1+1
    DEBUG ? Button1 ,CR
    ELSEIF IN5=1 THEN
    Button2=Button2+1
    DEBUG ? Button2 ,CR
    ENDIF
    PAUSE 500

    IF· Button1+Button2>2 THEN
    result=Button1<<4
    result=result+Button2
    GOSUB decode
    ENDIF
    GOTO main

    decode:
    DEBUG ? result,CR
    button1=0
    button2=0
    RETURN

    Jeff T
  • mrdontaymrdontay Posts: 3
    edited 2007-06-27 19:50
    Awesome! Where do I put the commands to toggle the LEDs I want
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-06-27 20:27
    Hi mrdontay, process your results inside the decode sub. Use Select Case or·If Then to filter the value of result and set the required led.

    Jeff T
Sign In or Register to comment.