Shop OBEX P1 Docs P2 Docs Learn Events
Using WaitPEQ for more than one pin? — Parallax Forums

Using WaitPEQ for more than one pin?

Hi

I'm currently using WaitPEQ in the following manner:
Waitpeq (|<BUTN1, |<BUTN1, 0) 'Pause program execution until button BUTN1 is pressed and held.
This works to monitor BUTN1.
....
If possible, I would like to monitor two different buttons so that the following word description works correctly.
Waitpeq until either BUTN1 or BUTN2 are pressed.
.....
I've tried many different ways to write this, but none of them will compile without error message.
I'm certain that either this is not possible using Waitpeq OR
There is something very fundamental that I do not understand.
I'm using Propeller Tool and Spin.

Any words of wisdom concerning this would be greatly appreciated.

Thanks
GaryG

Comments

  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-02-11 03:44
    Just OR them together with "|". Also, use waitpne (wait pin not equal) instead of waitpeq, so that it stops waiting when either button is pressed, instead of waiting until both are pressed.

    (EDIT: This line is wrong, see below.)
    waitpne((|<BUTN1) | (|<BUTN2), 0, 0)
    

    This waits until ina & ((|<BUTN1) | (|<BUTN2)) <> 0, or until any of those buttons are pushed.

    You can OR as many values together as you like. For example, you could use waitpne(%11111111, 0, 0) waitpne(0, %11111111, 0) to wait for any of pins 0-7 to go high.

    EDIT:
    As Jason Dorie pointed out, that should be:
    waitpne(0, (|<BUTN1) | (|<BUTN2), 0)
    
  • JasonDorieJasonDorie Posts: 1,930
    edited 2016-02-11 00:47
    Try this:
      mask := (|<BUTN1) | (|<BUTN2)  'OR the two button masks together into a single value
      waitpne(  0 , mask , 0 )       'wait for either pin to be set
    
  • waitpne((|<BUTN1) | (|<BUTN2), 0, 0)
    

    I think your state and mask values are backwards:

    waitpne( state , mask , port ) 'port is always 0 on Prop1
  • Thanks guys.

    I believe I would not have come up with those solutions on my own.
    I tried Electrodude's solution 1st and it compiled without error.
    I'm not sure as yet if it will run correctly yet.

    Jason
    If I'm understanding correctly, then maybe this also would work?

    mask := (|<BUTN1) | (|<BUTN2) | (|<BUTN3) 'OR the 3 button masks together into a single value
    waitpne( 0 , mask , 0 ) 'wait for either pin to be set


    Thanks again for all your help
    Garyg
  • Yup - you've got it.

    Perhaps slightly less cryptic:
    mask := (1 << BUTN1)  |  (1 << BUTN2)  |  (1 << BUTN3)
    

    I know the |< operator exists, but I find the above a little easier to read. If your BUTNx values are constants, you could speed up your code a little by doing this:
    mask := constant( (1 << BUTN1)  |  (1 << BUTN2)  |  (1 << BUTN3) )
    

    constant( x ) tells the Propeller tool to evaluate the function at compile time instead of at run time. Not a huge deal, but it'll save some instructions and some time, IF and only if those BUTN values are constant too.
  • JasonDorie wrote: »
    waitpne((|<BUTN1) | (|<BUTN2), 0, 0)
    

    I think your state and mask values are backwards:

    waitpne( state , mask , port ) 'port is always 0 on Prop1

    Thanks. Something in the back of my mind was telling me I should probably consult the Propeller Manual before giving help about an instruction I rarely use...

  • 'Missed the coaxial part. What is the cable impedance, and how is it terminated?

    -Phil
Sign In or Register to comment.