Shop OBEX P1 Docs P2 Docs Learn Events
One-Shot Command in Spin? — Parallax Forums

One-Shot Command in Spin?

JomsJoms Posts: 279
edited 2010-05-09 21:21 in Propeller 1
Is there a 'One-Shot' type command in spin? I would like to send a message only one time if a button is pressed, but have the programming continue if the button is held down. Currently I am doing the following:

       If ina[noparse][[/noparse]0] == 0                                   'Check if PIN 0 is LOW
         If b1 == 0                                     'if memory byte is 0, display 'pressed'
           Debug.str(string("BUTTON PRESSED"))      
           b1 := 1                                      'set memory byte to 1
       Else
         b1 := 0                                        'if PIN 0 is High, set memory byte to 0
         Debug.str(string("BUTTON NOT PRESSED"))        'display 'not pressed'




Variable b1 is just a local byte for keeping track of the button state...

The problem is, now I am displaying 'button not pressed' every time the program loops, so I would need to do this second section of code again to prevent this from consistantly updating the screen with 'not pressed'.· I can not use a 'repeat while' loop here because I want to continue with the program.

Is there a way to clean this up into a one or two line command? Just seems like a lot for 4 buttons...

Post Edited (Joms) : 5/9/2010 8:52:51 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-09 21:00
    How about changing the logic a little ...
    if ina[noparse][[/noparse] 0 ] == 0   ' check if pin is low
       ifnot b1~~   ' succeed if b1 is zero and always set to ones after testing
          debug.str(string("Button Pressed"))
    


    This won't display a "Button not pressed", but it's not clear just what you want.
    If you do want "Button not pressed" to be displayed, do you want it displayed on only the first time through the IF statement or do you want it displayed the first time the button is not pressed in the loop or what?
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-05-09 21:10
    if not ( ina[noparse][[/noparse]0] or b1 )
      Debug.str( ... )
      b1 := 1
     
    if ina[noparse][[/noparse]0] and b1
      Debug.str( string("Button released!") )
      b1 := 0
     
    

    Is this what you want?

    The first if is doing an 'or' which means whenever one of both, the input pin or the variable is one, the result is true. This result is negated. In other words, only if both are 0 the if is true.

    The second is checking the reverse condition. Only if input and b1 are true, the condition is true.

    The only thing you have to take care of is bouncing. A bouncing swicht could cause wrong release messages. So, it's a good idea to wait a while until you do execute the second if. So, when you say you want the loop to continue I suppose you do some other stuff in your loop. Putting the first if tho the beginning and the second if to the end of the loop might already good enough for debouncing.

    If not you could add a capacitor for debouncing. Otherwise the if's will get even more complex ;o)
  • JomsJoms Posts: 279
    edited 2010-05-09 21:21
    OK, I think I understand both examples. Thanks a ton for helping me clean that up.

    Sorry if I wasn't clear about exactly what I was asking, sometimes it gets hard for me to convey my questions. Basically, as an example of what I am trying to do, take a button and display either pressed or not pressed depending on the condition. However, I do not what to send the message of the buttons state multiple times. In the final program, I will not be just debugging it, but sending the command to the PINK, and don't want to flood the PINK with the same update of a single variable over and over.

    Thanks for the help on this!
Sign In or Register to comment.