a switch and 2LED


Closed Thread
Results 1 to 5 of 5
  1. #1
    win_832001's Avatar
    win_832001 Guest

    Default a switch and 2LED

    Hello there..Here I need a solution regarding to my programming..It seems like have no problem with the programming flow.but it is still does not working as I want. Here the description and sequence that I want:
    1) PIC circuit have a switch and 2 LED
    2) Initially, the redLED flashed
    3) After click pressed button the green LED will be flashed,
    4) After that we pressed again the red LED will be flashed again.
    5) The procees will continue according to the button is pressed.


    <code>
    GreenLED VAR PORTB.3
    RedLEd VAR PORTB.4
    emergency VAR PORTB.5 'switch pin


    main :

    IF emergency = 1 Then ' switch pin = 5V(high)
    GoSub onn
    Else ' switch pin = 0V(low)
    GoSub offf
    EndIF
    GoSub main

    onn:
    High GreenLED
    Low RedLED
    Return

    offf:
    Low GreenLED
    High RedLED
    Return
    </code>

  2. #2
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    @ I gave an answer here for another topic. sorry
    Last edited by mat janssen; - 11th October 2006 at 09:03.

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Assuming that you already set your pins as inputs & outputs; a code as below should work.
    edit : Also, is it a push-pull button or a holder switch?


    Code:
    OUTPUT PORTB.3      'make pin an output pin
    OUTPUT PORTB.4      'make pin an output pin
    INPUT  PORTB.5      'make pin an input  pin
    
    GreenLED    VAR PORTB.3     'assign a name to the pin
    RedLEd      VAR PORTB.4     'assign a name to the pin
    Emergency   VAR PORTB.5     'switch pin
    
    Begin:
    GreenLED = 0   'Initially OFF.
    RedLEd = 1     'Initially ON.
    
    
    main :
    
    IF Emergency = 1 Then            'Someone pushed the button.
       WHILE Emergency = 1 : wend    'Wait for finger release. 
       TOGGLE GreenLED    'Change the status.
       TOGGLE RedLEd      'Change the status.
    ENDIF
    
    
    GOTO main
    Last edited by sayzer; - 11th October 2006 at 16:16.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i'm not 100% sure of what you need but try this one..
    Code:
    TRISB=%00100000
    emergency       VAR PORTB.5 'switch pin 
    WichPORTB_Bit   var byte
    Loop            var word
    LedState        var bit
    
    wichPORTB_Bit=4
    ledstate=1
    
    Start:
        For loop=0 to 500
            if emergency=1 then 
               PORTB.0(wichportb_bit)=0
               wichportb_bit=wichportb_bit ^7
               while emergency : wend
               pause 20
               endif
            PORTB.0(wichportb_bit)=ledstate
            pause 1
            next
        ledstate=ledstate^1
        goto start
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    3) After click pressed button the green LED will be flashed,
    4) After that we pressed again the red LED will be flashed again.
    This is a bit vague. As sayzer asks what kind of switch? A standard pushbutton doesn’t latch, so it only indicates a “1” while pressed, and “0” when released.

    A pushbutton that latches will cost more then a button that doesn’t remember it’s state.

    So.. to implement the cheaper switch you’ll need to A) debounce it and 2) remember the previous state.

    Debounce simply means read it several times to insure you have a stable state. Typically a switch will only bounce in one direction, meaning it bounces ( rapidly closes and opens several times) when pressed but breaks cleanly; however, this may change over a lot or over the lifetime of the device, so best to debounce both ways.

    I usually debounce a switch by reading it every 5 ms for 10 times. If I get the same reading every time, I accept the reading. If I get a different reading in any sample, I throw them all away and start over. This has worked well for me for the most part, the switch still responds quickly (typ 50 ms or so) so the person pressing it gets good feedback, and slow enough to get around any bounces.

    Remembering the previous state just means saving it in a variable.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts