Shop OBEX P1 Docs P2 Docs Learn Events
Switches << i need some suggestions>> — Parallax Forums

Switches << i need some suggestions>>

kbampkbamp Posts: 4
edited 2008-08-06 04:01 in BASIC Stamp
I·need some suggestions. Here is my situation, i connected up 3 switches. switch1,2,3. Below is my code, it don't seem to be working according to what i want. i want to make it such that,


initial value of result = 10.

when:

switch1 = logic 1 input,
switch 2 = logic 0 input,
switch 3 = logic 0 input,
result = 5.

switch1 = logic·0 input,
switch 2 = logic 1 input,
switch 3 = logic·0 input,
result = 6.

switch1 = logic·0 input,
switch 2 = logic·0 input,
switch 3 = logic·1 input,
result·= 7.

switch1 = logic·0 input,
switch 2 = logic·1 input,
switch 3 = logic·1 input,
result = 7.

switch1 = logic·1 input,
switch 2 = logic·1 input,
switch 3 = logic·1 input,
result = 7.

switch1 = logic·1 input,
switch 2 = logic·1 input,
switch 3 = logic·0 input,
result = 6.

however, with the below code, i obtain results of 6 even if there were no input logic.however if i were to remove the part on

IF switchPin2 <> switchPrev2 THEN switchChange
IF switchPin3 <> switchPrev3 THEN switchChange

leaving jus switchpin(p11) , it works perfectly fine. i need some suggestions on how to get the other 2 switches working.
also.

Also i had a problem with p12.


·i had it tried on P11,12,14,15.


by changing

switchPin VAR IN11························ 'set variable to input pin 1 state
switchPrev VAR Bit· 'bit for keeping track of previous pin state

to

switchPin VAR IN12························ 'set variable to input pin 1 state
switchPrev VAR Bit· 'bit for keeping track of previous pin state

and to

switchPin VAR IN14························ 'set variable to input pin 1 state
switchPrev VAR Bit· 'bit for keeping track of previous pin state

and to

switchPin VAR IN15························ 'set variable to input pin 1 state
switchPrev VAR Bit· 'bit for keeping track of previous pin state

as for p12, it does not work correct, i have no connections to p12, therefore initial should be logic 0 input, however, when i run it, result were 5/10. could be either, which is different from what was expected of having a 10 always.


As·for p14,15
i had to write it

IF switchPin =·0 THEN result = 7


in order for result = 10.which is different from P11.



' {$STAMP BS2}
' Flexiforce Simple.bs2
' {$PBASIC 2.5}
'
[noparse][[/noparse] Declarations ]

result VAR Word
counting VAR Word

switchPin VAR IN11························ 'set variable to input pin 1 state
switchPrev VAR Bit· 'bit for keeping track of previous pin state

switchPin2 VAR IN14························ 'set variable to input pin 1 state
switchPrev2 VAR Bit· 'bit for keeping track of previous pin state

switchPin3 VAR IN15························ 'set variable to input pin 1 state
switchPrev3 VAR Bit· 'bit for keeping track of previous pin state

Initialize:
LOW rw
OUTS = %0000000000000000
DIRS = %0000001111111111

Main:
counting = 0
result = 10
PAUSE 100
IF switchPin <> switchPrev THEN switchChange
IF switchPin2 <> switchPrev2 THEN switchChange
IF switchPin3 <> switchPrev3 THEN switchChange

switchChange:
switchPrev = switchPin
IF switchPin = 1 THEN result = 5


switchChange2:
switchPrev2 = switchPin2
IF switchPin2 = 1 THEN result = 6

switchChange3:
switchPrev3 = switchPin3
IF switchPin3 = 1 THEN result = 7

DEBUG HOME, "result = ", DEC result

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-08-05 11:22
    kbamp

    as for p12, it does not work correct, i have no connections to p12, therefore initial should be logic 0 input, however, when i run it, result were 5/10. could be either, which is different from what was expected of having a 10 always



    ·When you switch·have no connections· your inputs will FLOAT and you get changes in what state


    ·
    When the switch·have no connections· the Input may·ON or OFF

    Input will be in·(·No Man's Land it dose not know where it is this mean that wich ever routine is TRUE it will do)

    ......................>>>>> This will give you trouble

    The trouble will be >>>··Let say that you have a routine that say when input 1 = ON
    then do something what ever that is

    and you have routine that say that when the input = OFF

    then do something what ever that is

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

    ·
    ·
    ·
    ·
    Sam
  • Lee HarkerLee Harker Posts: 104
    edited 2008-08-05 16:07
    kbamp,

    In addition to what Sam mentioned about leaving pins open, you may find it easier to handle 3 switch inputs together as a nibble. This would be a good place to investigate the Lookup, Branch or On Gosub commands. It will make your coding a bit easier.
  • kbampkbamp Posts: 4
    edited 2008-08-06 00:32
    ' {$STAMP BS2}
    ' Flexiforce Simple.bs2
    ' {$PBASIC 2.5}
    '
    [noparse][[/noparse] Declarations ]

    result VAR Word
    counting VAR Word

    switchPin VAR IN11 'set variable to input pin 1 state
    switchPrev VAR Bit 'bit for keeping track of previous pin state

    switchPin2 VAR IN14 'set variable to input pin 1 state
    switchPrev2 VAR Bit 'bit for keeping track of previous pin state

    switchPin3 VAR IN15 'set variable to input pin 1 state
    switchPrev3 VAR Bit 'bit for keeping track of previous pin state

    Initialize:
    LOW rw
    OUTS = %0000000000000000
    DIRS = %0000001111111111

    Main:
    counting = 0
    result = 10
    PAUSE 100
    IF switchPin <> switchPrev THEN switchChange
    IF switchPin2 <> switchPrev2 THEN switchChange
    IF switchPin3 <> switchPrev3 THEN switchChange

    switchChange:
    switchPrev = switchPin
    IF switchPin = 1 THEN result = 5


    switchChange2:
    switchPrev2 = switchPin2
    IF switchPin2 = 1 THEN result = 6

    switchChange3:
    switchPrev3 = switchPin3
    IF switchPin3 = 1 THEN result = 7

    DEBUG HOME, "result = ", DEC result


    using the above code, i connected up the circuit, but it don't seem to be working fine, result was always = 7. i tried changing to IF switchPin3 = 0 THEN result = 7
    but result was still always = 7. any 1 could suggest to me how i should write my code?
  • metron9metron9 Posts: 1,100
    edited 2008-08-06 01:35
    IF switchPin <> switchPrev THEN switchChange
    IF switchPin2 <> switchPrev2 THEN switchChange
    IF switchPin3 <> switchPrev3 THEN switchChange
    
    switchChange:
    

    Your code uses IF THEN logic but falls through to switchChange even if switchPin is equal to any of the three previous values. So the if then logic could be totally eliminated and the code would run the same. That is not the logic you want.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • metron9metron9 Posts: 1,100
    edited 2008-08-06 01:45
    Try using the lookup command and use the nibble name INL with pins 1,2 and 3. Mask out the 4th bit in the nibble (pin 0) and use that number to lookup the correct value in the lookup list. Give it a go and post it.

    EDIT:

    I guess I forgot the direction of the bits. Berhaps using bits 0,1 and 2 and mask out the forth bit, bit 4

    The mask would be

    lookupvalue =· INL & %0111

    so you would have 8 values in your lookup table, number values·0-7

    You can then use a one nibble variable to store the previous value to test on the next loop iteration instead of testing each bit.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!

    Post Edited (metron9) : 8/6/2008 1:55:07 AM GMT
  • kbampkbamp Posts: 4
    edited 2008-08-06 04:01
    oops i made an error post

    i am using...

    IF switchPin <> switchPrev THEN switchChange
    IF switchPin2 <> switchPrev2 THEN switchChange2
    IF switchPin3 <> switchPrev3 THEN switchChange3

    and the results is always 7...

    instead of

    IF switchPin <> switchPrev THEN switchChange
    IF switchPin2 <> switchPrev2 THEN switchChange
    IF switchPin3 <> switchPrev3 THEN switchChange
Sign In or Register to comment.