Shop OBEX P1 Docs P2 Docs Learn Events
How do I get a LED to turn ON and OFF using this code — Parallax Forums

How do I get a LED to turn ON and OFF using this code

sam_sam_samsam_sam_sam Posts: 2,286
edited 2011-02-17 15:12 in Propeller 1
This might be a stupid Question But I am very new to this
The reason why there are two post on the same thing is because the first one disappeared then reappeared
pub wait_press(pin, ms) | debounce, t

'' waits for (active-high) button press of ms milliseconds

  dira[pin] := 0                                                ' set pin to input mode

  debounce := 0                                                 ' clear debounce timer

  t := cnt                                                      ' sync with system cnt
  repeat until (debounce == ms)                                 ' wait specified db time
    waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
    debounce := ++debounce * ina[pin]                           ' scan input

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-16 11:12
    It's pretty easy -- this should get you started:
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
        wait_press(BTN, 100)                                        ' wait for 100ms button press
        outa[LED]                                                   ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                  ' 1/2s delay
    


    Please don't call me out in the thread subject line; I'm not your personal consultant and it's rude to the others (that stopped reading after seeing my name) that could/would help you.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-16 11:53
    JonnyMac

    The only reason that I put your name in the subject line was that you wrote the code that i was asking the question I am sorry if this is a no,no I know this next time but how would I use what I had posted I wanted to see how this code worked
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-16 12:32
    Tell if this is working right as long as I do not push the button it continues to flash and when I push the button it stops flashing

    What would I have to change to get it to work the other way to flash when I push the button
    pub main (LED)
    
      LED := 6
      outa[LED] := 0                                                  ' off
      dira[LED] := 1
    
      repeat
        wait_press( ina[9], 100)                                     ' wait for 100ms button press
        !outa[LED]                                                   ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                   ' 1/2s delay
     
    pub wait_press(pin, ms) | debounce, t
    
    '' waits for (active-high) button press of ms milliseconds
    
    
     pin := 9 
    dira[pin] := 0                                                ' set pin to input mode
    
    debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        debounce := ++debounce * ina[pin]                           ' scan input
    
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-16 13:53
    I don't understand your willingness to break code that works -- especially when you acknowledge being new at this. Here's working code, using your IO definitions, that I copied right out of my editor; the button behaves as I want: press on, press off. If you hold the button it will toggle every 600ms.
    con
    
      BTN = 9
      LED = 6
    
    
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
        wait_press(BTN, 100)                                        ' wait for 100ms press
        !outa[LED]                                                  ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                  ' 1/2s delay 
    
    
    pub wait_press(pin, ms) | debounce, t
    
    '' waits for (active-high) button press of ms milliseconds
    
      dira[pin] := 0                                                ' set pin to input mode
    
      debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        debounce := ++debounce * ina[pin]                           ' scan input
    

    Note how the debounce method is used exactly as I first presented it. The ability to pass parameters to methods is what allows use to write code that can be re-used. Embedding pin constants, as you have done, is a very bad idea.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-16 13:56
    Copy the code of wait_press, rename it to wait_release and change one line of code in that:

    instead of
    debounce := ++debounce * ina[pin]

    write
    debounce := ++debounce * (1 - ina[pin] )



    By the way ... if you change the second parameter of wait_press or wait_release to 500 you can simply remove the waitcnt in the main loop.


    PS: Guess the buttons you both have are connected differently. One having a pushup, the other one having a pulldown resistor.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 05:14
    I don't understand your willingness to break code that works -- especially when you acknowledge being new at this.

    I just start working through..... Propeller Education Lab ver 1.2 I ....was using the example that I have seen so far

    Embedding pin constants, as you have done, is a very bad idea.

    I Know that I Still have a LOT to learn about How to write Spin Code

    I am very great full for the help and welcome being corrected when it is need

    Thank You


    Guess the buttons you both have are connected differently. One having a pushup, the other one having a pulldown resistor.

    I am using the Propeller Professional Development Board and I had to use this one for it to work the same way

    debounce := ++debounce * (1 - ina[pin] )

    Thank you for your help
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-17 08:03
    Details are important, and knowing if a circuit is active-high (as my original code supported) or active low (as you have) is critical. With just a tiny bit of work that code can be converted to work in either case. The update method requires the button's active state. I just ran this code on my PDB.
    con
    
      BTN = 9
      LED = 6
    
    
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
        wait_press(BTN, 100, 0)                                     ' wait for 100ms press
        !outa[LED]                                                  ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                  ' 1/2s delay 
    
    
    pub wait_press(pin, ms, state) | debounce, t
    
    '' waits for button press of ms milliseconds
    '' -- state: 1 = active-high, 0 = active-low
    
      dira[pin] := 0                                                ' set pin to input mode
    
      state &= 1                                                    ' limit to bit0
      debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        if (state == 0)
          debounce := ++debounce * (!ina[pin] & 1)                  ' scan for low input
        else
          debounce := ++debounce * ina[pin]                         ' scan for high input
    
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 10:11
    I hate to ask what might seem a stupidly simple question but how would you writes this to have the led stay ON / OFF instead of toggle LED

    Now if I was writting this for a Basic Stamp I know how to write this

    I can write this and it will keep the LED ON all the time
    con
    
      led = 6
    
    pub LED_on  (pin)
    
    dira[led] := 1
    !outa[led] 
    
    repeat
    


    For this
    con
    
      BTN = 9
      LED = 6
    
    
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
        wait_press(BTN, 100, 0)                                     ' wait for 100ms press
        !outa[LED]                                                  ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                  ' 1/2s delay 
    
    
    pub wait_press(pin, ms, state) | debounce, t
    
    '' waits for button press of ms milliseconds
    '' -- state: 1 = active-high, 0 = active-low
    
      dira[pin] := 0                                                ' set pin to input mode
    
      state &= 1                                                    ' limit to bit0
      debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        if (state == 0)
    
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2011-02-17 10:15
    Sam,

    have you worked out why it is bad to embed constants like pin numbers in your code? If not then imagine your program was very large and used this constant a lot.

    Graham
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-17 10:16
    The question isn't stupid, it's just that the phasing is unclear. What do you want? Your thread states that you want to turn an LED on and off with one button -- toggling is your only option unless you add a second button. If that's what you want then the debounce code would be different.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 10:23
    toggling is your only option unless you add a second button

    I did not know that this was the only option for this code could you show me the other code for this

    unless you add a second button. If that's what you want then the debounce code would be different.

    Could you show me what this code would look like
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-17 10:49
    Did you create the second function(wait_release) ?

    You'd simply do a
    repeat
      wait_press(BTN,200)
      !outa[ LED ]
      wait_release(BTN,200)
    

    This code waits until the button has been pressed for at least 200ms. If so, the LED is toggled, which means if the LED was on it will be switched off, if it was off it will be switched on. After that the wait_release simply makes sure that the button has been released. So, a long push won't toggle again. It's always toggled with the next push.
    Maybe that's what you want?!

    (BTW toggeling is not the only way you can switch an LED on/off with only one button - you could for example measure the time and a short push will switch it on, a long push will switch it off)
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 11:14
    I try this and it seem to do what I want it to do I am not sure that this is written correctly or not
    con
    
      BTN = 9
      LED = 6
    
    
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
        wait_press(BTN,100, 0)                                     ' wait for 100ms/ 500ms press
        !outa[LED]                                                       ' ON  LED
        wait_press(BTN,100, 1)                                    ' wait for 100ms/ 500ms press       
        outa[LED]~                                                     ' OFF LED
    
    pub wait_press(pin, ms, state) | debounce, t
    
    '' waits for button press of ms milliseconds
    '' -- state: 1 = active-high, 0 = active-low
    
      dira[pin] := 0                                                ' set pin to input mode
    
      state &= 1                                                    ' limit to bit0
      debounce := 0                                                 ' clear debounce timer
    
      t := cnt                                                      ' sync with system cnt
      repeat until (debounce == ms)                                 ' wait specified db time
        waitcnt(t += clkfreq/1_000)                                 ' hold 1ms
        if (state == 0)
          debounce := ++debounce * (!ina[pin] & 1)                  ' scan for low input
        else
          debounce := ++debounce * ina[pin]                         ' scan for high input
    
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-02-17 12:14
    The original code does what you want. You don't need separate commands unless you're using separate buttons for on and off.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 13:26
    Yes it dose how ever

    I also want some thing that when you push the button it wait for some amout of time before that statement become true and turn ON the LED
    Then when you push the again same thing again but instead it turn the LED OFF

    Can this be done with one button or dose this have to be done with two buttons

    toggling is your only option unless you add a second button
    I did not know that this was the only option for this code could you show me the other code for this

    unless you add a second button. If that's what you want then the debounce code would be different.
    Could you show me what this code would look like
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-17 14:21
    simply add a waitcnt after wait_press
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-02-17 15:01
    Hi Graham,

    I would like to know what you exactly mean by
    have you worked out why it is bad to embed constants like pin numbers in your code? If not then imagine your program was very large and used this constant a lot.

    maybe I got tomatos on my eyes - but - I would state the oposite: in most cases it is a good idea to use constants.
    But I assume that you are a well experienced programmer and that you have good reasons to write this.
    I'm really interested to know and understand the reasons.

    After thinking about it I still can only guess:
    - constant names that say to less about their meaning?
    - everything is meant ironic????

    best regards

    Stefan
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-02-17 15:12
    This is what I had first

    Embedding pin constants, as you have done, is a very bad idea. this is what JonnyMac was talking about
    [B]pub main (LED)
    
      [I]LED := 6[/I] 
     outa[LED] := 0                                                  ' off
      dira[LED] := 1
    
      repeat
        wait_press( ina[9], 100)                                     ' wait for 100ms button press
        !outa[LED]                                                   ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                   ' 1/2s delay[/B]
    

    This the way it should have been
    [B]con
    
     [I] BTN = 9
      LED = 6[/I]
    
    pub main
    
      outa[LED] := 0                                                ' off
      dira[LED] := 1
    
      repeat
       [I] wait_press(BTN, 100, 0)       [/I]                              ' wait for 100ms press
        !outa[LED]                                                  ' toggle LED
        waitcnt(clkfreq / 2 + cnt)                                  ' 1/2s delay   [/B]
    

    I know what Graham talking about I have had very long Basic Stamp code routine before and if you do not have CON define pins it hard to follow the code that is written

    If not then imagine your program was very large and used this constant a lot.
Sign In or Register to comment.