Shop OBEX P1 Docs P2 Docs Learn Events
3 BUTTON MOTOR BUTTON HELP — Parallax Forums

3 BUTTON MOTOR BUTTON HELP

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

Comments

  • tomcrawfordtomcrawford Posts: 1,129
    edited 2015-12-01 23:01
    I'm guessing you are modifying ServoControlWithPushbuttons.bs2 on page 132ff in What's a Microcontroller. When you build the circuit and run that program, does everything work correctly? If not, why not?

    Once you have the two button program working you are adding the third pushbutton.

    I would write down a truth table.
    
    000   no change
    001   no change
    010   decrease duration
    011   increase duration
    100   no change
    101   no change
    110   decrease duration
    111   increase duration
    
    

    Is that what you had in mind? It doesn't match your description.
    I would replace the string of IFs with a SELECT...CASE:
    switches = (swx << 2) | (swy << 1) | (swz)
    SELECT switches
       CASE = 2
          decrease
       CASE = 6
          decrease
    
    and so on
    
    

    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
Sign In or Register to comment.