Shop OBEX P1 Docs P2 Docs Learn Events
Please,,,Need help ,, with button — Parallax Forums

Please,,,Need help ,, with button

diggingdigging Posts: 3
edited 2011-07-25 21:37 in Propeller 1
i have a question, which is how to control LED on/off with a single button. so by clicking the button first time will turn LED on for the second time will turn the LED off !!
i know it is really basics but i have tried many ways to do it but i couldn't figure out how it works

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-07-25 09:04
    digging,

    Welcome to the forums. Have you taken a look at the Propeller Education Kit labs? They have an assortment of button examples.

    http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/PELabsFunBook-v1.1.pdf

    http://forums.parallax.com/showthread.php?89958-Propeller-Education-Kit-Labs-Tools-and-Applications

    It's best to start at the beginning with that free downloadable book and work your way through the examples.

    Look at pages 34-35 in the lab book mentioned above. Look at how the REPEAT loop handles the pushing of a button and how the ! operator changes the state of a pin.

    hope that helps.
  • diggingdigging Posts: 3
    edited 2011-07-25 18:56
    thank you
    i know how to do that but, what i would like to do is if i click button1 one time the LED will start blinking ( i know how it makes the LED blinking ) and if i click the same button1 for the second time , the LED will stop , so it like to function with one button
    thank you in advance
  • kwinnkwinn Posts: 8,697
    edited 2011-07-25 20:11
    Use a variable that is set to 0 or 1 for the led not blinking/blinking. When the button is pressed set the variable to 0 if it is 1, and 1 if it is 0. Use the variable to blink the led only if it is 1.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-07-25 20:36
    digging wrote: »
    thank you
    i know how to do that but, what i would like to do is if i click button1 one time the LED will start blinking ( i know how it makes the LED blinking ) and if i click the same button1 for the second time , the LED will stop , so it like to function with one button
    thank you in advance

    digging,
    I haven't tested this code. In fact, I didn't even write it on the SPIN editor, so I hope the indentation is correct on the IF statements, etc..
    It's probably not the most elegant way to do this, but I think it might work.

    CON
    
      _clkmode        = xtal1 + pll16x           ' Feedback and PLL multiplier
      _xinfreq        = 5_000_000                ' External oscillator = 5 MHz
    
      LEDpin     = 0                        ' Pin that controls the LED.
      PUSHBUTTON      = 1           ' Pushbutton Input Pin
    
    VAR
    LONG LEDstatus
    
    PUB Main      ' Main method
    
      {Initialize LED status as not blinking...}
      LEDstatus := 0
    
      dira[LEDpin] := %1             ' Set LED pin to output.
      dira[PUSHBUTTON] := %0  'Set PUSHBUTTON to input.
    
    Repeat    ' Endless loop
      
       {If the button has been pushed AND the LED is not blinking...}
        IF (ina[PUSHBUTTON] == 1) AND (LEDstatus == 0)
          LEDstatus := 1       
          waitcnt(clkfreq/2 + cnt)             ' Wait half a second to debounce the button.
    
       {If the button has been pushed AND the LED is blinking...}   
        IF (ina[PUSHBUTTON] == 1) AND (LEDstatus == 1 )
            LEDstatus := 0 
       
        IF (LEDstatus == 1)  'Make the LED blink on each loop of the repeat...
          ! outa[LEDpin]            
           waitcnt(clkfreq / 4 + cnt)             ' Wait 1/4 second
    
        IF (LEDstatus == 0)  'Make the LED stop blinking...
         outa[LEDpin]  := %0              
    
    
  • markaericmarkaeric Posts: 282
    edited 2011-07-25 20:54
    Digging,

    It would certainly be wise to go through some of the beginner lessons listed above, because there are some that are similar to what you are trying to do, and will give you the knowledge you need to work it out on your own. Otherwise, you might be skipping over some of the most important concepts.
  • diggingdigging Posts: 3
    edited 2011-07-25 21:07
    i really appreciated your help
    i have tested the code but i have to hold the button to make the LED stop flashing
    what i would like to do is a single click
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-07-25 21:37
    digging wrote: »
    i really appreciated your help
    i have tested the code but i have to hold the button to make the LED stop flashing
    what i would like to do is a single click

    digging,
    then you want to change the value of the debounce, which in my original code was very long. Try changing it to the following, or perhaps try 1/100th of a second to see if that makes it feel better for you.
    waitcnt(clkfreq/10 + cnt)             ' Wait a tenth of a second to debounce the button.
    
    

    And I agree with markaeric that you should take some time to go through the labs. All of this will be a cinch if you do. What I've provided is pretty rudimentary, so to get a functionality that is more crisp feeling, you might need to employ cogs, etc. But that's the kind of stuff you learn if you work through the labs.

    Happy button pushing!
Sign In or Register to comment.