Use a switch by change not on/off status.


Closed Thread
Results 1 to 17 of 17
  1. #1
    Join Date
    Oct 2014
    Posts
    2

    Default Use a switch by change not on/off status.

    Dear all,

    I have a problem with a toggle switch where the output must change on a status on a switch going from 1 to 0 or vice versa.
    To read a 0 or 1 from a switch is no problem. I need to read the flank, like a flipflop does. Is the right way an interrupt?
    The output must remain 0 on startup, regardless the on or off status of the switch.
    Any help appreciated.

    Thanks,
    Koert

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Interrupt sure is one way. Maybe PulsIn is another, to read the edge of the pulse.

    Ioannis

  3. #3
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Quote Originally Posted by courtec View Post
    The output must remain 0 on startup, regardless the on or off status of the switch.
    What exactly do you mean by that? Do you mean a logical 0 in the program algorithm or 0 volts at the switch? There are a few ways to do it depending on what you want.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Off the top of my head in pseudo-code:

    Code:
    SwitchIn VAR PortB.0
    OldState VAR BIT
    SwitchState VAR BIT
    
    OldState = SwitchIn
    SwitchState = 0
    
    Main:
      IF SwitchIn not = OldState THEN
        SwitchState = SwitchState ^ %1  ' Switch has been toggled
        OldState = SwitchIn
      ENF-IF
    GOTO Main
    Robert

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I would use interrupt on change. ISR just changes output port status. Set port status in start up code to " your safe off status" set interrupts off until pic is stable then enable interrupts. Whatever status toggle switch is in initally is irrelevant as changing it triggers the interrupt.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Interrupt on change is more efficient. But this is Koert's first post and he just joined, he hasn't given any indication to his level of PBP expertise..

    Assuming he is a beginner, he's better off trying logic instead of interrupts to get the ball started, then switch to interrupts later as a challenge.

    Koert, search for Darrel Taylor's Instant Interrupts in FAQ section. Unless you only do super simple programming, they help A LOT when using USB, timers, interrupts, and many more features.

    Robert

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I agree with Robert contact bounce and such can really confuse an interrupt based solution ,unless you have some experience behind you. slow changing inputs can easily be read (and debounced) by using polling style solutions

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    contact bounce can be marginalized with a short pause. IOC would trigger going to either logic state and his output would always start out at what it is initialized to. If he is not too worried about latency then even ON INTERRUPT would work, though I prefer Darrel's asm interrupts.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  9. #9
    Join Date
    Oct 2014
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Thank you all for the input so far. And indeed, I am new with pic programming. Knowledge of basic language and electronics for many years. This kind of switching solution was done by a 4027 JK flipflop. But it's time to move on and use a micro controller. And more of a challenge then i expected!
    Anyway, to be more precise about the question asked:
    a micro switch in a machine is standard closed by a magnet.
    When the switch opens, so goes from 0 to 1 (logical) a relais must be activated. When the magnet returns and the switch goes back to 0, doe nothing
    Again when the switch opens again toggle the relais. All with sufficient code to avoid bouncing.
    '
    Quite simpel i think but several programs i tried don't work as good as a 4027. "Probably" due to the fact that i am a new-be....

    I'l look into the two solutions mentioned and let you know. Thanks again.

    Koert.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Try this. I have not tested though, but is based on Roberts idea to work as you requested. Roberts idea will toggle the output everytime the switch will change state. Not on 0->1 only.

    Code:
    SwitchIn VAR PortB.0
    OldState VAR BIT
    SwitchState VAR BIT
    
    OldState = SwitchIn
    SwitchState = 0
    
    Main:
      IF SwitchIn not = OldState THEN
        IF SwitchIn THEN
          SwitchState = SwitchState ^ %1  ' Switch has been toggled
        ENDIF
      ENDIF
    OldState = SwitchIn
    GOTO Main
    Ioannis

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    You can tweak a PAUSE 50 before GOTO as simple debounce.

    Robert

  12. #12
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I think debounce is not necessary since he wants to see the edge from 0->1 and toggle the output. Then do nothing with 1->0 until a new 0->1 edge comes.

    Ioannis

  13. #13
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I know, I was just suggesting minimal debounce as he requested.
    All with sufficient code to avoid bouncing.
    Robert

  14. #14
    killdill045's Avatar
    killdill045 Guest


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Set port status in start up code to " your safe off status" set interrupts off until pic is stable then enable interrupts. Whatever status toggle switch is in initally is irrelevant as changing it triggers the interrupt.

  15. #15


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Just a thought. I did something like this for a LDR to act as a motion detector. It should work for a switch.

    checkswitch var bit
    compare var bit
    clear 'bits are 0
    low portb.0 'output pin off

    start:
    let checkswitch = porta.0 'toggle switch
    pause 50
    let compare = porta.0
    if compare <> checkswitch then toggle portb.0 : clear: goto start
    goto start

  16. #16
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    I have used the following on a few occasions quite successfully:

    Lets say the switch is on portc.3

    SW VAR PORTC.3




    WHILE SW = 0 : WEND 'THIS WILL CONTINUALLY LOOP AS LONG AS THE INPUT IS LOW, MAKE IT SW = 1 TO DETECT
    'GOING LOW.
    ' ASSUMES THAT ALL YOU ARE DOING IS WAITING FOR A SWITCH CHANGE
    'ACCORDING TO THE MANUAL THIS LOOP CHECKS ABOUT 50 TIMES A SECOND
    'SO IT SHOULD SEE THE CHANGE WITHIN 1/50 OF A SECOND.

    DO SOMETHING HERE 'THIS IS WHAT YOU WANT TO DO AT THE SWITCH CHANGE
    Last edited by muddy0409; - 22nd November 2014 at 13:16.
    Peter Moritz.
    Up the bush, Western Plains,
    New South Wales,
    Australia.

  17. #17
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Use a switch by change not on/off status.

    Ok, a JK FlipFlop is a 2 input one output device.
    I am guessing you are using a latching relay which
    changes states every time it is energised, is
    this correct?

    If so you need to read 2 pins to check switch state.
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    DEFINE OSC 4
    TrisA = %00000011
    
    TRISC = %00000000  
    PortC = %00000000
    
    ANSEL=0
    ANSELH=0
    ADCON0 = 0
    ADCON1 = 0
    Option_Reg = %00000000 ; enable RABPU
    WPUA = %00000011  ;Set RABPU
    Main:
    If PortA.0 = 0 then 
    portc.2 = 1
    pause 500  ; 1/2 seconds of power out
    portc.2 = 0
    else
    goto main
    endif
    goto Impatient  ; wait for microswitch to zero
    
    Impatient:
    while PortA.1 = 1
    portc.3 = 1
    pause 100
    portc.3 = 0
    pause 100
    wend
    if PortA.1 = 0 then goto main
    
    end
    Tested with 16F690 demo board. Weak pullups did not work, had to use real resistors, likely because the PICKit2 was still connected. LEDs were enabled to verify operation
    Last edited by Archangel; - 23rd November 2014 at 12:43.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Toggle switch (latching switch) state
    By craigwb in forum General
    Replies: 4
    Last Post: - 6th November 2011, 15:18
  2. Switch sensor vs. switch PIC pin
    By fratello in forum Schematics
    Replies: 10
    Last Post: - 4th August 2011, 12:17
  3. Interrupt status?
    By circuitpro in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 28th February 2011, 02:27
  4. Replies: 6
    Last Post: - 9th January 2011, 16:26

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