Shop OBEX P1 Docs P2 Docs Learn Events
BOOLEAN EXPRESSION HELP? — Parallax Forums

BOOLEAN EXPRESSION HELP?

I have basic stamp kit 2.5.

This other guy and I are taking an independent study class revolving around the basic STAMP. So our teacher assigns us projects to do with the kit and we present them to him. However, he's never used the kit himself so he's useless for questions or help.

Our latest project, he wants us to program 1 LED to light up according to the input of 2 switches which should follow a truth table of 2 variables (x & y) reading either 1 or 0.

So x=1 & y=0
x+y = 1 = true = LED turns on

So x=0 & y=1
x+y = 1 = true = LED turns on

So x=1 & y=1
x+y = 2 = false = LED stays off

So x=0 & y=0
x+y = 0 = false = LED stays off

For the life of us we CAN NOT understand how to make this into a PBASIC program

Every time we ask and try telling him we don't think its possible with PBASIC, he tells us to use AND & OR gates but I'm not sure how those would help here either? . Point being we've stuck for awhile and we need help so please

HELP

Thank you :)

Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2015-11-03 13:25
    Look up the Exclusive Or operator, XOR.
  • The truth table you describe above is XOR (^). The BS2 has an XOR operator, but you can indeed also do this with AND and OR operators:

    "x XOR y" is the same as "(x AND NOT y) OR (y AND NOT x)"

    The PBASIC reference contain a number of small code snippets to help you out. Here are some pertinent links to help get your started:

    * DO...LOOP for your main loop.
    * IF...THEN for your conditional test.
    * OUTPUT for controlling the LED.
    * INPUT for reading the switches.

  • I've stayed up all night trying to figure it out. heres what I've come up with:

    x VAR BYTE
    y VAR BYTE
    z VAR BYTE

    DO
    x = 0
    y = 0
    z = 0

    HIGH 13
    PAUSE 500
    LOW 13
    PAUSE 500



    IF ( IN3 = 1) AND ( IN4 = 1) THEN

    DO
    HIGH 0
    LOW 0
    PAUSE 00
    LOW 0

    z = z + 1
    LOOP UNTIL (z = 10)
    ENDIF

    IF (IN4 = 1) AND (IN3 = 0) THEN

    DO
    HIGH 14
    PAUSE 50
    LOW 14
    PAUSE 50

    x = x + 1
    LOOP UNTIL (x = 10)
    ENDIF

    IF (IN4 = 1) AND (IN3 = 0) THEN

    DO
    HIGH 15
    PAUSE 20
    LOW 15
    PAUSE 20

    y = y + 1
    LOOP UNTIL (y = 10)

    ENDIF
    LOOP
  • How about...

    IF IN3 = IN4 THEN LOW 1 ELSE HIGH 1

    IN3 could be switch X and IN4 could be switch Y. If both switches are in 0 or 1 position pin 1 is set low. If one of the switches is at 0 and the other is at 1 then pin 1 is set high
  • my friend sent me the following, not sure what what he's doing but he says it should work? any comments?

    Pulse4 PIN 4 ' pulse input pin
    Pulse3 PIN 3

    #SELECT $STAMP
    #CASE BS2, BS2E, BS2PE
    Scale CON $200 ' 2.0 us per unit
    #CASE BS2SX
    Scale CON $0CD ' 0.8 us per unit
    #CASE BS2P
    Scale CON $0C0 ' 0.75 us per unit
    #CASE BS2PX
    Scale CON $0CF ' 0.81 us per unit
    #ENDSELECT

    time VAR WORD
    maybe VAR WORD


    Main:

    PULSIN Pulse4,0,time
    PULSIN pulse3,0,time
    DO WHILE (time = 0)
    DEBUG "0 bitch"

    GOTO main


    PULSIN Pulse4, 1, time ' measure positive pulse
    IF (time > 0) AND (Pulse3 = 0) THEN ' if not 0
    DEBUG HOME,
    DEC time, " units ", CLREOL ' display raw input
    time = time */ Scale ' adjust for Stamp
    DEBUG CR,
    DEC time, " us " ' display microseconds
    ELSE
    DEBUG CLS, "Out of Range" ' else error message

    ENDIF
    PAUSE 200
    GOTO Main

    PULSIN Pulse3, 1, time ' measure positive pulse
    IF (time > 0) AND (Pulse4 = 0) THEN ' if not 0
    DEBUG HOME,
    DEC time, " units ", CLREOL ' display raw input
    time = time */ Scale ' adjust for Stamp
    DEBUG CR,
    DEC time, " us " ' display microseconds
    ELSE
    DEBUG CLS, "Out of Range"
    ENDIF
    PAUSE 200
    GOTO Main

    LOOP
    END
  • @Hal ALbach

    Like this? and does it make a difference if i leave the switch as they are or whats the point of turning them into "switch" variables as suggested


    x VAR BYTE
    y VAR BYTE
    z VAR BYTE

    DO
    x = 0
    y = 0
    z = 0

    HIGH 13
    PAUSE 500
    LOW 13
    PAUSE 500

    IF IN3 = IN4 THEN

    LOW 1

    ELSE

    HIGH 1


    ENDIF

    IF (IN4 = 1) AND (IN3 = 0) THEN

    DO
    HIGH 14
    PAUSE 50
    LOW 14
    PAUSE 50

    x = x + 1
    LOOP UNTIL (x = 10)
    ENDIF

    IF (IN4 = 1) AND (IN3 = 0) THEN

    DO
    HIGH 15
    PAUSE 20
    LOW 15
    PAUSE 20

    y = y + 1
    LOOP UNTIL (y = 10)

    ENDIF
    LOOP

  • Given the original post I am (dangerously) assuming that one LED was to be on if the inputs were different and off if they were identical, which the snippet I sent does. It simply compares both switches and sets pin 1 accordingly, if you are using toggle switches. If you are using momentary pushbuttons then pin 1 will show if you have 1 or both buttons pressed or released. In your last post you are setting outputs on pins 1, 13, 14, and 15. Are there LEDs on all four pins and what is it they are supposed to indicate?
    Perhaps you could tell us how the switches and LED(s) are hooked up.
  • This should do it, using your own words:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' Our latest project, he wants us to program 1 LED to light up
    led1     PIN  1
    ' according to the input of 2 switches
    swx      PIN  2
    swy      PIN  3
    ' which should follow a truth table of 2 variables (x & y) reading either 1 or 0.
    
    DO
      ' So  x=1 & y=0
      IF (swx = 1) AND (swy = 0) THEN HIGH led1   ' x+y = 1 = true = LED turns ON
    
      ' So  x=0 & y=1
      IF (swx = 0) AND (swy = 1) THEN HIGH led1   ' x+y = 1 = true = LED turns ON
    
      ' So  x=1 & y=1
      IF (swx = 1) AND (swy = 1) THEN LOW led1    'x+y = 2 = false = LED stays off
    
      ' So  x=0 & y=0
      IF (swx = 0) AND (swy = 0) THEN LOW led1    ' x+y = 0 = false = LED stays off
    
    LOOP
    

    This is basically the same as what Hal Albach wrote in 1 line:

    IF swx = swy THEN LOW led1 ELSE HIGH led1

  • I apologize, i simply forgot to change the code from a previous project but you are absolutely correct on what I'm trying to accomplish. it didn't come with a toggle switch so we are using momentary push buttons. But i have an 8pin toggle switch. Would it make more sense to use the toggle switch?

    this is the corrected code, i think. i really appreciate your help, we have to present this in 3 hours

    DO
    x = 0
    y = 0
    z = 0



    HIGH 3
    PAUSE 500
    LOW 3
    PAUSE 500



    'IF ( IN3 = 1) AND ( IN4 = 1) THEN

    IF IN3 = IN4 THEN

    LOW 1

    ELSE

    HIGH 1


    ENDIF

    IF (IN4 = 1) AND (IN3 = 0) THEN

    DO
    HIGH 4
    PAUSE 50
    LOW 4
    PAUSE 50

    x = x + 1
    LOOP UNTIL (x = 10)
    ENDIF

    IF (IN3 = 1) AND (IN4 = 0) THEN

    DO
    HIGH 3
    PAUSE 20
    LOW 3
    PAUSE 20

    y = y + 1
    LOOP UNTIL (y = 10)

    ENDIF
    LOOP
  • Put the snippet I sent into a loop and forget all the rest. What you just posted is making your input pins into an output. You have switches on those pins. You may let the magic smoke out of your Basic Stamp.
  • IT WORKS!

    Thank you Hal Albach & 72sonett. you guys are the real MVPS :)

    The years just getting started so I'm sure ill need more help in the future. if there is any like 'thumbs up' or good rating i can give you guys let me know!
  • You're welcome!
Sign In or Register to comment.