Shop OBEX P1 Docs P2 Docs Learn Events
simple led — Parallax Forums

simple led

if i push a button i want a led to start glowing for 5 sec until it goes dark again even if i keep holding the button. does anyone have any tips?

Comments

  • Which controller are you using and if Propeller are you using Spin or C or Forth or PASM?
  • i just have a button, led and a resistor, basic stamp 2, pbasic 2 i think. my friend was the one who asked me for help and i tried to give him some very easy and small codes but i don't know if they'll work since i've never used basic stamp and don't have any near me that i can try out.
  • Hal AlbachHal Albach Posts: 747
    edited 2015-11-04 15:27
    Here is something I cobbled together. The 500 value in the for-next loop may need adjusting down if you need a more precise 5 minute interval.
    img
  • (deleted due to sarcasm)

    But for goodness sake, download basic 2.5 so you have the PIN statement!
  • JonnyMacJonnyMac Posts: 9,156
    edited 2015-11-05 03:04
    Maybe I'm missing something, but this seems pretty easy.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Trigger         PIN     0
    LED             PIN     1
    
    Main:
      DO WHILE (Trigger = 0)
        ' wait for button press
      LOOP
    
      HIGH LED
      PAUSE 5000
      LOW LED
    
      DO WHILE (Trigger = 1)
        ' wait for release
      LOOP
    
      GOTO Main
    

    If you want early off but 5s max you can do it like this:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Trigger         PIN     0
    LED             PIN     1
    
    timer           VAR     Word
    
    Main:
      DO WHILE (Trigger = 0)
        ' wait for button press
      LOOP
    
      HIGH LED
    
      FOR timer = 1 TO 500
        PAUSE 10
        IF (Trigger = 0) THEN Exit
      NEXT
    
      LOW LED
    
      DO WHILE (Trigger = 1)
        ' wait for release
      LOOP
    
      GOTO Main
    
  • Adding to what JonnyMac said, here is the same thing except the state of the button is defined as a constant. Now the code can be transferred to the Professional Development board more easily.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    trigger     PIN     0
    LED         PIN     1
    
    buttonPressed CON 0             'state of trigger when pressed
    
    Main:
      DO WHILE (trigger <> buttonPressed)  'wait for button
      LOOP
    
      HIGH LED                     'turn it on
      PAUSE 5000                   'five seconds
      LOW LED                      'then off
    
      DO WHILE (trigger = buttonPressed)   'wait for release
      LOOP
    
      GOTO Main
    
Sign In or Register to comment.