Shop OBEX P1 Docs P2 Docs Learn Events
Four Switch Truth Table Have A Question about how to get it to work — Parallax Forums

Four Switch Truth Table Have A Question about how to get it to work

sam_sam_samsam_sam_sam Posts: 2,286
edited 2009-11-06 03:39 in General Discussion
What· I want to do is to have a way of LOCK- ing access to a· home made key board

I have try-ed a few ways of doing it but can not get it·to work like the Truth Table show below


Truth Table is as Follows

···············This is the right order························ ········ · These button are not·push in the right order

switch1 = 1··<....... 1····· ··^······switch1=·0···<.......4···^·· ···· =· 1··<.......·1····^·····=·1· <.......1··· ·^····=·1· <......3
switch2 = 1··<........2····· ··^······switch2=·1···<.......3···^······ ·=· 0··<.......·4····^··· ·=·1··<.......2·····^··· =·1· <......1···......> ·And so on
switch3 = 1··<........3······ ·^······switch3=·1···<.......2···^········=· 1··<.......·3····^·· · =·0··<.......4·····^··· = 1· <......2
switch4 = 1··<........4······ ·^······switch4=·1···<.......1···^········= ·1· <.......·2····^·· · =·1··<.......3·····^··· =·0· <......4

·I want to use this type of routine or something like it
N=0
DO
  xbit = IN0
  xx = xbit ^ x0 & x0
  x0 = xbit
  N = N + xx
 DEBUG HOME, DEC ? xbit,
             DEC ? xx,
             DEC ? x0,
             DEC ? N
PAUSE 100
LOOP UNTIL N = XX                           '  XX = Some Value

Dose any one have any idea.gif·s on how to do this type of routine

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 11/3/2009 2:42:33 AM GMT

Comments

  • VIRANDVIRAND Posts: 656
    edited 2009-11-03 01:32
    If there are 4 regular switches then it's too easy with S1 AND S2 AND S3 AND S4 = 1111

    But it looks like you have 4 pole switches so you have either combinations or permutations.
    It depends if you can only switch one switch to one number or not, how to count.
    If 1111 is the first combination then you just have to count in base 4 to get to 4444 (not using zero)
    I think there are 256 combinations then.
    If 1234 is the first combination (use each number only once) then 4321 will be the last and you'd count like
    1243 1324 1342 1423 1432 4123 4132 4213 4231 4312 4321.
    (There are a few ways to count that way, at least one should be easy to figure out.)
    I think there are only 12 combinations then so the regular switches would be a little more secure
    because there are 16 on-off combinations.
    edit[noparse]:o[/noparse]ops I goofed, and didn't use 2 and 3 as the first number. There are 1x2x3x4=24 combinations.

    Post Edited (VIRAND) : 11/3/2009 1:37:15 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-03 01:58
    VIRAND

    If there are 4 regular switches then it's too easy with S1 AND S2 AND S3 AND S4 = 1111

    Thank You for your reply

    They are 4 regular switches· but how I want to·do it not easy·
    S1 AND S2 AND S3 AND S4 = 1111· This is true

    ·One· note .....>>>> I am ·Not Yelling......>>>>··This is what I do not want in this truth table that I am working on

    This is NOT true in the type of Truth Table I want and·will NOT·WORK and all other combinations are NOT TRUE

    S2·AND S1·AND S3 AND S4········ = 1111··Yes the switch Truth Table is true
    S4 AND S3 AND S2 AND S1········ = 1111
    ·......>>>>>
    ........
    .......
    .......····· And· ALL other combinations are NOT true because the button's are not· PUSHed in the·right ORDER
    .......
    .......
    .......······································ <<<<<<<·
    ·

    _____________________________________________________________________________________________________________

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 11/3/2009 2:43:26 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2009-11-03 04:04
    I'm having trouble with the colored text you use, sorry.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • kwinnkwinn Posts: 8,697
    edited 2009-11-03 14:52
    @sam_sam_sam

    If these are 4 switches rather than push buttons then what you want to do is fairly difficult. The micro needs to know when you have finished setting up each combination so it can read it. The only way I can see of doing that is to:
    - start with a fixed input (say 0000)
    - read the setting after a fixed time passes (say 2 seconds)
    - see if it matches the first combination
    - repeat for as many combinations as you want

    If you already have a keyboard connected why not use it to enter a sequence of key presses?
  • VIRANDVIRAND Posts: 656
    edited 2009-11-04 12:49
    I'm sorry I didn't understand how your lock works, I wasn't yelling either, I just use other systems
    that use big letters for the logic function "&" as "AND".

    I think if I got a clue from the other comments,
    that you might want to consider using a gray code, or state machine,
    (or maybe you would be as long as you switched only one at a time).
    As long as one switch is switched at a time you can most easily
    have a list of states in order, which maybe you called a truth table,
    and if I'm wrong don't mind me.

    I think the states would proceed like this then, with any mistake making a silent exit (start over)
    from accepting the lock code.
    Version 1:Turn on and off in order, like push buttons.
    S1 S2 S3 S4
    0000 (First expected switch state)
    1000
    0000
    0100
    0000
    0010
    0000
    0001
    0000 Lock/Unlock it (finished)

    Version 2:Turn on in right order only then turn off switches,
    0000
    1000
    1100
    1110
    1111 Lock/Unlock it (finished)
    0000 ... This changes too many switches but the lock was done. It could mean reset the lock if you want.

    I'm a little confused because you used the ^ symbol which means XOR, both in the code and in the
    unwanted combinations. It's not clear to me why you are using it. I suppose it could encrypt the
    switch order if that's what you intended and would make more sense about needing a truth table
    but I don't know exactly what you are thinking.

    However you program the lock, you could use at least one temporary LED to light up while you
    are testing it to recognize the switch activity as correct, but as soon as it is working you should
    remove it of course because it would make picking the lock very easy.

    Remember to go back to the beginning of waiting for the first right switch if the wrong one is used.
    My state numbers are the order of switches being switched that it waits for before going to wait for
    the next one, and eventually the switching is done and the lock is controlled. If you do it this way
    then be sure to debounce switches (Repeat 100000 for a delay) so the same switch doesn't look like
    the wrong one while it is still being used.
    Gray code just means a sequence where only one bit (or switch) changes at a time to get to the next step.
    State machine means a sequence of events is followed in an order and may determine what action is
    taken. The switch lists are a sequence of events which activate lock only on the last one, and
    if a wrong event (switch) happens then it should restart waiting for the first event instead of stay waiting
    for the correct one, or else even a baby will quickly pass all the correct and wrong ones.
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-04 16:07
    Hi folks,

    Interesting conversation.

    Suppose you had a 4-postion switch. Each position would be connected to an input.

    Also have a single push-button switch connected to an input.

    The procedure would be to turn the selector switch to a position then push and release the button to tell the micro to read which input was on.

    The micro stores the number and the operator moves the switch to another postion and hits the push button again. The micro stores the second input.

    This would continue until either the right combination is entered or a wrong digit is entered and the micro resets the numbers. Quite flexible in that the code can be of any length.
    A simple If statement would complete when the right sequence of inputs is seen.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • kwinnkwinn Posts: 8,697
    edited 2009-11-04 16:31
    The method VIRAND and stamptrol suggest would both work. Keep in mind that in either case the switches (and button) must be debounced first or false codes will be read. The gray code idea suggested by VIRAND has the advantage of not requiring any extra hardware.
  • mikedivmikediv Posts: 825
    edited 2009-11-04 20:45
    stamptrol that's an excellent idea I was building somehting similar and was reading this with interest if you don't mind I am going to use your idea its quite elegant and would save the trouble of constantly polling the switches , I really like your idea of using push button to let MCU know its ready to read the switch config thumbs up man .
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-11-05 00:23
    I want to thank you both for your replies

    Tom Sisk

    Interesting conversation.

    Suppose you had a 4-position switch. Each position would be connected to an input.

    Also have a single push-button switch connected to an input.

    The procedure would be to turn the selector switch to a position then push and release the button to tell the micro to read which input was on.

    The micro stores the number and the operator moves the switch to another position and hits the push button again. The micro stores the second input.

    This would continue until either the right combination is entered or a wrong digit is entered and the micro resets the numbers. Quite flexible in that the code can be of any length.
    A simple If statement would complete when the right sequence of inputs is seen.



    This is an Interesting· idea.gif·Could you give me an example of how you would do this·in a·code routine

    ___________________________________________________________________________________________________________________________



    VIRAND
    This is what I was looking for.....>>>>
    I think the states would proceed like this then, .......>>>>>with any mistake making a silent exit (start over)
    ·from accepting the lock code.
    Version 1:Turn on and off in order, like push buttons.
    S1 S2 S3 S4
    0000 (First expected switch state)
    1000
    0000
    0100
    0000
    0010
    0000
    0001
    0000 Lock/Unlock it (finished)

    How would I write a routine to do this

    I'm a little confused because you used the ^ symbol which means XOR, both in the code and in the
    unwanted combinations. It's not clear to me why you are using it.

    I· was using this to mean ^ UP I am sorry if I made it·a little confusing for you or any one else
    to show the truth table I want to use

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 11/5/2009 12:32:33 AM GMT
  • stamptrolstamptrol Posts: 1,731
    edited 2009-11-05 12:55
    Sam,

    ·· Tied up finishing off another project at the moment.

    ·I'll see if I can put something together·over the next couple of days.

    ·But, its not too complicated:

    ·· Set up an array to hold the attempts.

    ··Set up a loop which will go through as many times as you want the code to have digits.

    ·· Perhaps have an LED to blink to show that its waiting for the first attempt.

    ··· Set the selector switch. Push the button. Compare the value to the first digit of the real code. Use the INA, INB, etc to read selctor sw.If good, loop and wait for next attempt. If bad, erase all attempts and go back to beginning.

    ··· Use the BUTTON command to debounce the push button input. Selector switch inputs don't need debounce.

    ··· Use IF try(0) and try(1) and try(2) and try(3) to confirm the right code.

    ·Cheers,


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com


    Post Edited (stamptrol) : 11/5/2009 1:01:36 PM GMT
  • kwinnkwinn Posts: 8,697
    edited 2009-11-05 14:11
    You don't really need an array to hold the attempts. All that is required is counter that starts by pointing to the first switch state (index into the array). Each time a correct entry is made the pointer is incremented to point at the next entry. If an incorrect entry is made it is reset to the first switch state in the array. When the last entry is made correctly the keypad is unlocked.
  • PropabilityPropability Posts: 142
    edited 2009-11-05 19:25
    If the system resets to the begining on a bad attempt then it becomes pretty easy to figure out the sequence. Would be better to let it go through the whole sequence so as not to alert when a bad attempt was made just that only the·SET of attempts was wrong.

    ·Maybe assign a value to the 4 keys 0,1,2,3·· binary 00,01,10,11.

    Then assign a place(shifting and adding)·for each press in a byte xx_xx_xx_xx (or word for multiple presses of the keys)

    Then test the byte. 11_01_00_10 (or word )

    One key may get pressed more than once and one key may not get pressed at all if you desire.



    Post Edited (Propability) : 11/5/2009 8:17:15 PM GMT
  • kwinnkwinn Posts: 8,697
    edited 2009-11-06 03:39
    Propability has a point. Once an attempt has been started there should not be any error indication if an individual keypress/switch setting is wrong.
Sign In or Register to comment.