BOOLEAN EXPRESSION HELP?
gdibarra
Posts: 10
in BASIC Stamp
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
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
"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.
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
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
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
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
Perhaps you could tell us how the switches and LED(s) are hooked up.
This is basically the same as what Hal Albach wrote in 1 line:
IF swx = swy THEN LOW led1 ELSE HIGH led1
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
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!