Menu

Switches

Help
2007-01-13
2013-05-30
  • Nobody/Anonymous

    Hello,
    I have been toying with cool LED patterns. I really want to add a switch so if I hold it for lets say 3 seconds, the whole unit will shut off.
    Or if I hold it for 2 seconds, the LEDs will blink in a different fashion.
    I am wondering, how would I do this? I am using a switch that is usually closed so when I press, the circuit would be closed.

    My chip will be 16F628A / 16F684.
    Thanks,
    Omar

     
    • Hugh Considine

      Hugh Considine - 2007-01-14

      Some code like this would do the trick:

      #define button PORTA.5
      #define pressed off
      dir button in

      'Code to time how long the button is held down
      ButtonTime = 0
      do while button pressed
      Wait 50 ms
      ButtonTime += 1
      loop

      'code for 2 seconds
      If ButtonTime >= 40 and ButtonTime < 60 Then
      'blink code here

      end if

      'code for 3 seconds
      If ButtonTime >= 60 Then
      'code to turn off here

      end if

      ButtonTime is used to count how long the button has been held for, and is incremented every 1/20 of a second. You might need to change the constants at the start of the program to match your circuit.

       
    • Nobody/Anonymous

      Oh I see, thank you very much.
      So basically I am connecting a switch from the pin (input) to ground. When I press, current will pass, and there is something being inputted. Thus, the code detects this and increments a variable, and if it falls between a set of numbers the patterns appear? Cool. I think that is very clever.

      Thanks,
      Omar

       
    • Russ Hensel

      Russ Hensel - 2007-01-16

      I think your switch is a normally open switch ( you actually implied it was closed in both positions ).  I think the circuit you want is to connect the port to V+ with a pull up resistor, typically around 20 K.  Then connect it to ground thru the switch.  Do not leave an input port connected to nothing ( so called floating ).  See piclist.com for much more info on connecting pins

      Russ

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.