3 BUTTON MOTOR BUTTON HELP
gdibarra
Posts: 10
in BASIC Stamp
Were trying to work a motor to work with 3 buttons. in the BASIC STAMP2 manual, there is an example coding for 2 button in which each button will spin the motto a different direction. so were trying to do a 3 button combination in which every button combination will spin it one direction, and only one combination will spin it the opposing combination. kinda of like the lock combination to the safe. here is our code so far but were still kinda stuck. PLEASE HELP ' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
Motor PIN 14
'according to the input of 3 switches
swx PIN 3
swy PIN 4
swz PIN 11
'Which follows the truth table of 3 variables (x & y & z) reading either 1 or 0
DO
'So x=0 & y=0 & z=0
IF (swx=0) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 = motor does nothing
'SO x=0 & y=0 &z=1
IF (swx=0) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 = motor does nothing
'So x=0 & y=1 & z=0
IF (swx=0) AND (swy=1) AND (swz=0) THEN IF duration > 500 THEN duration = duration -25 'x+y+z=1 motor rotates -25 degrees
ENDIF
'So x=0 & y=1 & z=1
IF (swx=0) AND (swy=1) AND (swz=1) THEN IF duration < 1000 THEN duration = duration +25 'x+y+z=1 motor rotates +25 degrees
ENDIF
'So x=1 & y=0 & z=0
IF (swx=1) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 =motor does nothing
'So x=1 & y=0 & z=1
IF (swx=1) AND (swy=0) AND (swz=1) THEN LOW motor 'x+y+z=0 = motor does nothing
'So x=1 & y=1 & z=0
IF (swx=1) AND (swy=1) AND (swz=0) THEN IF duration > 500 THEN duration = duration -25 'x+y+z=1 motor rotates -25 degrees
ENDIF
'So x=1 & y=1 & z=1
IF (swx=1) AND (swy=1) AND (swz=1) THEN IF duration < 1000 THEN duration = duration +25 'x+y+z=1 motor rotates +25 degrees
ENDIF
PULSOUT 14, duration
PAUSE 10
LOOP
' {$PBASIC 2.5}
duration VAR Word
Motor PIN 14
'according to the input of 3 switches
swx PIN 3
swy PIN 4
swz PIN 11
'Which follows the truth table of 3 variables (x & y & z) reading either 1 or 0
DO
'So x=0 & y=0 & z=0
IF (swx=0) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 = motor does nothing
'SO x=0 & y=0 &z=1
IF (swx=0) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 = motor does nothing
'So x=0 & y=1 & z=0
IF (swx=0) AND (swy=1) AND (swz=0) THEN IF duration > 500 THEN duration = duration -25 'x+y+z=1 motor rotates -25 degrees
ENDIF
'So x=0 & y=1 & z=1
IF (swx=0) AND (swy=1) AND (swz=1) THEN IF duration < 1000 THEN duration = duration +25 'x+y+z=1 motor rotates +25 degrees
ENDIF
'So x=1 & y=0 & z=0
IF (swx=1) AND (swy=0) AND (swz=0) THEN LOW motor 'x+y+z=0 =motor does nothing
'So x=1 & y=0 & z=1
IF (swx=1) AND (swy=0) AND (swz=1) THEN LOW motor 'x+y+z=0 = motor does nothing
'So x=1 & y=1 & z=0
IF (swx=1) AND (swy=1) AND (swz=0) THEN IF duration > 500 THEN duration = duration -25 'x+y+z=1 motor rotates -25 degrees
ENDIF
'So x=1 & y=1 & z=1
IF (swx=1) AND (swy=1) AND (swz=1) THEN IF duration < 1000 THEN duration = duration +25 'x+y+z=1 motor rotates +25 degrees
ENDIF
PULSOUT 14, duration
PAUSE 10
LOOP
Comments
Once you have the two button program working you are adding the third pushbutton.
I would write down a truth table.
Is that what you had in mind? It doesn't match your description.
I would replace the string of IFs with a SELECT...CASE:
I think you will be pleased at how much easier it is to read (and modify) the program.
Finally, I noticed that you are not initializing duration.
Edit: I fixed a bug in my SELECT:CASE. EndEdit