Shop OBEX P1 Docs P2 Docs Learn Events
Big Problem With Trying to Control DC Motors with Pushbuttons — Parallax Forums

Big Problem With Trying to Control DC Motors with Pushbuttons

sv806sv806 Posts: 14
edited 2007-04-27 21:45 in BASIC Stamp
I'm new to the basic stamp, my group is using it to control motors for a design project at school, we would appreciate any kind of help as we are in our last week and still cant get this program to work...

So the project is to control 2 motors. We want them to do several different sets of motions...like turn motor 1 on for 1.3 seconds and motor 2 on for .5 seconds. That would be an example of 1 procedure, out of many. We are using 2 H-Bridges which have 2 pins each to control Direction and Pulse Width. There are also 6 Switches which are being used as feedback to the Basic Stamp, so it knows which procedure to do next. There are 4 Push Buttons being used to also tell the Basic Stamp which code to do next. What we want the code to do is...if Inputs are High on the Pins 1 and 4 do (something). The following is only part of the code, the real code has a process for every 2 highs among each pushbutton and each Momentary Switch.

EDITED:Some people have asked for a clearer discription, here's a short clear version..."we are monitoring 10 switches, and when any two of them are high, do a specific code for those two being high. We want the code to always be ready though, always be checking to see which two are high and then immediately do (specific program for those two)."

THE PROBLEM: It just wont work, it might do one of the random procedures, but it wont do anything perfectly, the idea of reading highs on 2 inputs which send highs on 2 outputs sounds simple to me, I didn't think there would be so many problems.

' {$STAMP BS2}
' {$PBASIC 2.5}

INPUT 0 'Pushbutton 1
INPUT 1 'Pushbutton 2
INPUT 2 'Pushbutton 3
INPUT 3 'Pushbutton 4
INPUT 4 'Momentary Switch 1
INPUT 5 'Momentary Switch 2
INPUT 6 'Momentary Switch 3
INPUT 7 'Momentary Switch 4
INPUT 8 'Momentary Switch 5
INPUT 9 'Momentary Switch 6
'Pin 10 Direction H-Bridge Vertical
'Pin 11 PWM H-Bridge Vertical
'Pin 14 Direction H-Brige Horizonral
'Pin 15 PWM H-Bridge Horizontal

DO

  IF (IN0=1) THEN 'If Pin 1 receive a voltage do the following:

    HIGH 14
    HIGH 15
    PAUSE 375
    LOW 15
    LOW 10
    HIGH 11
    PAUSE 468
    LOW 11

ENDIF

  IF (IN0=1) AND (IN4=1) THEN 'If Pins 0 and 4 receive a voltage do the following:

    HIGH 10
    HIGH 11
    PAUSE 937
    LOW 11

ENDIF

  IF IN0=1 AND IN5=1 THEN 'If Pins 0 and 5 receive a voltage do the following:

    LOW 10
    HIGH 11
    PAUSE 468
    LOW 11
    LOW 14
    HIGH 15
    PAUSE 375
    LOW 15
    LOW 10
    HIGH 11
    PAUSE 468
    LOW 11

ENDIF


Post Edited (sv806) : 4/25/2007 1:16:25 AM GMT

Comments

  • curious1curious1 Posts: 104
    edited 2007-04-24 23:16
    I did something very similar.
    Give all of your "chores" a label: and use GOTO label:·when input cond. are met in main: scan.
    I'm new and not very well versed in the language but I sometimes get it to work.
    Hope I didn't confuse ya,
    RC
  • sv806sv806 Posts: 14
    edited 2007-04-24 23:18
    umm, i understand the giving each procedure a label...but what did you mean by "main:scan" is that basic language?
  • curious1curious1 Posts: 104
    edited 2007-04-24 23:23
    main:

    ·· if this or that GOTO 1

    ·· if this or that GOTO 2

    ·· and so on.....

    ·· GOTO main

    It will continue to loop back to main: until one of the cond. is met, then it will GOTO whatever·label .

    RC
  • sv806sv806 Posts: 14
    edited 2007-04-24 23:28
    What about the Pins, in the beginning, should they be set to inputs and outputs in the beginning?

    And i assume we should add a "goto main:" after every procedure as well?
  • curious1curious1 Posts: 104
    edited 2007-04-24 23:48
    When a program begins, all of the pins are inputs.
    The HIGH and LOW commands automatically change the associated pins to OUTPUTS.
    Yes, usually goto main after each chore unless the program specifies otherwise.
    Good Luck, there are brilliant folks·in this forum·that can help you from here.
    This is about my limit for now, I just hope I got this right.
    Anyone PLEASE correct me if I'm wrong.
    RC
  • LSBLSB Posts: 175
    edited 2007-04-25 00:55
    I think I can help, but I don't understand the problem very well from your description. "There are also 6 Switches which are being used as feedback to the Basic Stamp, so it knows which procedure to do next. There are 4 Push Buttons being used to also tell the Basic Stamp which code to do next." confuses me. I read it as: "Monitor 10 switches--when any two are high, then do something." is this correct? Perhaps "Monitor the 4 switches and when any two of them are high, then do what is determined by the other six switch settings" is what you are looking for? Please help me to understand your problem better, so that I won't waste my effort--and your time.
  • sv806sv806 Posts: 14
    edited 2007-04-25 01:08
    Thank you for asking me to clear things up, and I appreciate your effort already...

    You were right with monitoring 10 switches, and when any two of them are high, do a specific code for those two being high. We want the code to always be ready though, so the looping is important.

    We were having problems with the code setting a pin to high and it not going back to low after the switch was let off...thats why I asked curious1 before about the necessity of having to set the inputs as inputs or outputs in the beginning of the program before the Main. I have attached what he has told me to do. There are six positions the motors take a solid rod to, the two motors are being used to control an x and y actuator individually...think about a manual car.
  • sv806sv806 Posts: 14
    edited 2007-04-25 01:10
    And this is an attachment of what we had initially, which wasn't working at all...it just didn't do anything in order and nothing was working as it should, two different switches would be set to high and something random would happen.
  • LSBLSB Posts: 175
    edited 2007-04-25 01:24
    Ok, I get it I think. Give me an hour or two to look at it. Meantime, someone else may jump in with an answer.
  • curious1curious1 Posts: 104
    edited 2007-04-25 01:35
    It's as well as DONE. This is one of the BRILLIANT minds I mentioned earlier.

    I also had trouble until I used pull down resistors on all of the buttons and switches on my project.

    Without them, you never know what it will do next.

    Your'e in good hands w/LSB

    RC
  • sv806sv806 Posts: 14
    edited 2007-04-25 02:02
    LSB, if there is anything you do not understand, please let me know, I will be responding immediately to every post I receive on this topic pretty much all night (4/24/07)...and thank you again for you help, my groups grade depends on this project, and this code is only part of it. I have attached a schematic to this post, and it might also help clear some things up, I am working on a bigger schematic for all the electronics right now, it should be done within the hour...

    curious1, thank you for letting me know about the pull-down resistors, i have researched them and they seem like an ideal item to integrate into my circuit, i have been using resistors...however, they these momentary switches (the 6) aren't only being used to send HIGHs to the Basic Stamp, they are also lighting up an LED when they are switched on...here is a schematic i made in PSpice. I have attached it to this post, and it might also help clear some things up, I am working on a bigger schematic for all the electronics right now, it should be done within the hour...
    1191 x 528 - 13K
  • LSBLSB Posts: 175
    edited 2007-04-25 04:19
    I'm not doing your homework for you but, I think this will help. Per the help file (under INS "The INS variable always shows the state of the I/O pins themselves, regardless of the direction of each I/O pin. We call this, "reading the pins." If a pin was set to an input mode (within DIRS) and an external circuit connected the I/O pin to ground, the corresponding bit of INS would be zero."

    Ok, so with the DIRS set as:
    DIRL = %00000000 'SET PINS 0-7 TO INPUT
    DIRH = %11110000 'SET PINS 8-12 TO INPUT, 12-15 TO OUTPUT

    Then we can write the switch pattern to a Word sized variable (we'll only be using 10 bits, but all the switches won't fit in a byte so...

    Buttons VAR Word
    Do
    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2)
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3)
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4)
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5)
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6)
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7)
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0)
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1)
    Loop

    Now, BUTTONS always shows the pattern of our switch settings in the 10 LSBs (Use DEBUG BIN16 BUTTONS to see this), so we can look at any one switch with Buttons(LP)=1 (assuming switches are wired with pullups--they should read 0 unless pressed) or any group of switches with a bit comparison like:

    IF BUTTONS= %00000000 00001001 then Gosub SHIFT_TO_FIRST 'Button 0 and 3 pressed
    IF BUTTONS= %00000000 00000101 then Gosub SHIFT_TO_SECOND 'Button 0 and 2 pressed

    This goes in the loop like:
    Buttons VAR Word
    Do
    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2)
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3)
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4)
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5)
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6)
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7)
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0)
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1)

    IF BUTTONS= %00000000 00001001 then Gosub SHIFT_TO_FIRST 'Button 0 and 3 pressed
    IF BUTTONS= %00000000 00000101 then Gosub SHIFT_TO_SECOND 'Button 0 and 2 pressed
    .
    .
    .

    Loop

    Each pattern then goes to a subroutine that moves your shifter to the proper position and then RETURNS to the loop to continue checking the buttons, or continues around the loop if no match is found.

    Also, look at the Button Command, it shows how to wire the switches for pull up (if you’ve got a meter, check that the switch output is 0 unless the button is pressed) and how to debounce. Look at Gosub it explains a subroutine.

    Oh, the reason we wrote the button values to a variable is so we could set the 4 MSBs to 0 without affecting the INS register. Use statements like: Buttons(15)=0 to do this before checking the pattern in the if statements.
  • sv806sv806 Posts: 14
    edited 2007-04-25 04:39
    That was all really new to me...but I will give it a try. I do have some questions though:


    IF BUTTONS= %00000000 00001001 'is this in order from pin 16 to pin 0, left to right?

    and is this how it should look or at least the right order: ?


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    DIRL = %00000000 'SET PINS 0-7 TO INPUT
    DIRH = %11110000 'SET PINS 8-12 TO INPUT, 12-15 TO OUTPUT
    
    Main:
    
    Buttons VAR Word
    
    DO
    
    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2)
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3)
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4)
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5)
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6)
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7)
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0)
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1)
    
    IF BUTTONS=%0000000000001001 THEN GOSUB SHIFT_TO_FIRST 'Button 0 and 3 pressed
    'IF BUTTONS=%0000000000000101 THEN GOSUB SHIFT_TO_SECOND 'Button 0 and 2 pressed
    
    LOOP
    
    SHIFT_TO_FIRST:
        HIGH 14
        HIGH 15
        PAUSE 375
        LOW 15
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    



    and when I check the syntax it shows me the attached image.

    Post Edited (sv806) : 4/25/2007 5:07:45 AM GMT
  • LSBLSB Posts: 175
    edited 2007-04-25 04:50
    A couple of things:

    1) Remove the Main: label, you don't need it.
    2) The spaces in the binary values (%000...) are fore readability, take them out.
    3) GOSUB works like goto, but returns to the line after the gosub when it hits the word RETURN. Each of your subroutines should end with RETURN lke:

    SHIFT_TO_FIRST:
    HIGH 14
    HIGH 15
    PAUSE 375
    LOW 15
    LOW 10
    HIGH 11
    PAUSE 468
    LOW 11
    RETURN '
    SEND EXECUTION BACK TO THE LINE UNDER THE GOSUB THAT SENT IT.

    4) 'is this in order from pin 16 to pin 0, left to right? Close-- pin 15 to 0

    5) No endif needed if the whole statement is on one line.
  • sv806sv806 Posts: 14
    edited 2007-04-25 04:52
    Okay, I understand the gosub, and the return...but one more thing for right now

    What does this mean and why only 10?

    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2)
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3)
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4)
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5)
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6)
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7)
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0)
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1)
  • LSBLSB Posts: 175
    edited 2007-04-25 04:56
    INS.Lowbyte(0) = 0 if the switch is not pressed, 1 if it is; same for (1), (2), etc. We're using 0-9 because those 10 bits hold the value of your 10 buttons (I assumed the switches were wired to P0 - P9). In your original post there were two unused pins (which we don't need to read) and the four output pins used to control the motors--we don't need those either.
  • sv806sv806 Posts: 14
    edited 2007-04-25 04:58
    so if we had an 11th switch...

    BUTTONS.HIGHBYTE(2)=INS.HIGHBYTE(2)

    ??
  • LSBLSB Posts: 175
    edited 2007-04-25 05:00
    Yes... and if you wanted to look for patterns of three buttons:

    IF BUTTONS= %00000100 00000101 THEN GOSUB SHIFT_TO_SECOND 'Button 10, 0 and 2 pressed.
  • sv806sv806 Posts: 14
    edited 2007-04-25 05:02
    okay...

    your last post...you wrote button 10, 0, and 2
    i think your code was set to 11, 0, and 2
    or am i wrong?

    and i assume it would work the same for a pattern for any single button to all 10?
    also, the periods within "BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1)" are they there so I can read it or do I need them there?
  • LSBLSB Posts: 175
    edited 2007-04-25 05:06
    Yes, again. 1, 2, 3, or 10-- just put 1's were you want to match a pushed button, 0 where you don't.

    Ah, a trick question on the periods. The short answer is yes, you need them. there are ways around it, but they're there and work.
  • sv806sv806 Posts: 14
    edited 2007-04-25 05:31
    I have attached the new and updated program with your help...please let me know if I did this correctly, or if you have any other advice.

    Post Edited (sv806) : 4/25/2007 5:40:53 AM GMT
  • LSBLSB Posts: 175
    edited 2007-04-25 11:48
    My mother used to say that the truth was in the tasting... that is, does it work?

    If not, what happens? Think twice, describe it carefully first to yourself, then to the forum.
    Did you check your switches, do they output 0 unless pressed?
    Did you check your BUTTONS variable with DEBUG BIN16 BUTTONS ? Did you try something like:

    DEBUG BIN16 BUTTONS,CR
    IF BUTTONS=%0000000100000001 THEN DEBUG "--MATCH. SHOULD GOSUB MAIN",CR 'pin 0 and 8

    To see if you're getting the Match that you expect?
    Did read and then reread the Help file sections I suggested to see if you could solve the problem without help?
    Do you have a teacher or advisor that can help without suffering the tedium of typing every word?

    I understand that desperation can certainly steal the motivation work thoroughly and carefully, but NOTHING is more important in progamming than a methodical and logical approach. My advice comes from Martin C. Clarke's book The Martian Way: "Hurry, but don't rush."
  • metron9metron9 Posts: 1,100
    edited 2007-04-25 15:05
    Not sure why you are trying to copy bit by bit the INS register to BUTTONS with all the highbyte(),lowbyte() commands when you can just use.

    BUTTONS=INS

    Since INS is a WORD variable the statement HIGHBYTE with a bit modifier makes no sense as HIGHBYTE(0) is the same as HIGHBYTE with no modifier. The statement HIGHBYTE(1) would point to actually I am not sure.

    After playing with the code I did find an anomaly that perhaps Parallax can address. The following code actually changes the variable on the right side of the equation. I assume this is because for example HIGHBYTE(1) modifier used with a word value is pointing outside the actual variable addressed. I put comments on the lines of code that modify the wrong variable but the code is invalid code, it compiles and runs because the syntax is correct but it sets up invalid pointers that overwrites your variable space. I don't have time to drill down to the specifics.

    You also have gosub MAIN in two statements and no MAIN: lable in the program.

    MAIN is typically not a name that is used for a subroutine as it is the MAIN entry point to a program. Not that it can't be used but it is typically not used as a subroutine name.


    I used a variable PINVAR instead of the INS variable so I did not have to actually hold any pins high or low. Your code is not doing what you want it to do.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM2}
    
    
    BUTTONS   VAR   Word
    
    PINVAR    VAR   Word      'SAMPLE DATA INSTEAD OF INS
    
    PINVAR=%1111111111111111  'SAMPLE DATA TO TEST WITH
    
    
    DEBUG "BUTTONS=%",BIN16 BUTTONS," PINVAR=%",BIN16 PINVAR,CR
    
    MAIN:
    
    
    
    'BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)      'PINVAR NOT AFFECTED
    'BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)      'PINVAR NOT AFFECTED
    'BUTTONS.LOWBYTE(2)=PINVAR.LOWBYTE(2)   'CHANGES pinvar TO %1111111100000000
    'BUTTONS.LOWBYTE(3)=PINVAR.LOWBYTE(3)   'CHANGES PINVAR TO %0000000011111111
    'BUTTONS.LOWBYTE(4)=PINVAR.LOWBYTE(4)   'PINVAR NOT AFFECTED
    'BUTTONS.LOWBYTE(5)=PINVAR.LOWBYTE(5)   'PINVAR NOT AFFECTED
    'BUTTONS.LOWBYTE(6)=PINVAR.LOWBYTE(6)   'PINVAR NOT AFFECTED
    'BUTTONS.LOWBYTE(7)=PINVAR.LOWBYTE(7)   'PINVAR NOT AFFECTED
    'BUTTONS.HIGHBYTE(0)=PINVAR.HIGHBYTE(0) 'PINVAR NOT AFFECTED
    'BUTTONS.HIGHBYTE(1)=PINVAR.HIGHBYTE(1) 'CHANGES pinvar TO %1111111100000000
    
    DEBUG "BUTTONS=%",BIN16 BUTTONS," PINVAR=%",BIN16 PINVAR,CR
    
    DEBUG "W0=",DEC W0,CR,"W1=",DEC W1,CR,"W2=",DEC W2,CR
    
    
    END
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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) : 4/25/2007 3:10:41 PM GMT
  • sv806sv806 Posts: 14
    edited 2007-04-26 00:18
    metron9, Im sorry, but i do not understand what you are trying to say about the pinvar. You state that my code will not do what i want it to do, however, i could not figure out what you are trying to explain to me, to help me improve my code. I appreciate you looking at it, and hope you can explain what you meant, so i could improve what i have. Thank you.

    LSB, i ran the code, it is attached, and while i was using the debug bin16 statement, i was able to see what the bits were doing, and every time they did something that corresponds to an IF THEN statement, they did not do the subroutine...i do not know if its comparing it correctly or maybe there is a problem with it checking to see if the new 16bit is the same as any of the IF statements?

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    DIRL = %00000000 'SET PINS 0-7 TO INPUT
    DIRH = %11001100 'SET PINS 8,9,12,13 TO INPUT, 10,11,14,15 TO OUTPUT
    
    Buttons VAR Word
    
    ' 0 'Up
    ' 1 'Down
    ' 2 'Reverse
    ' 3 'Neutral
    ' 4 'First
    ' 5 'Second
    ' 6 'Third
    ' 7 'Fourth
    ' 8 'Fifth
    ' 9 'Reverse
    'Pin 10 Direction H-Bridge Vertical
    'Pin 11 PWM H-Bridge Vertical
    'Pin 14 Direction H-Brige Horizontal
    'Pin 15 PWM H-Bridge Horizontal
    
    Main:
    
    DO
    
    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0)   'pin0
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1)   'pin1
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2)   'pin2
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3)   'pin3
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4)   'pin4
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5)   'pin5
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6)   'pin6
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7)   'pin7
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0) 'pin8
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1) 'pin9
    
    DEBUG BIN16 buttons, cr
    
    IF BUTTONS=%0000000000001010 THEN GOSUB Revers 'pin 2 and 4
    
    IF BUTTONS=%0000000000000001 THEN GOSUB OneF   'pin 0
    IF BUTTONS=%0000000000010001 THEN GOSUB TwoF   'pin 0 and 4
    IF BUTTONS=%0000000000100001 THEN GOSUB ThreeF 'pin 0 and 5
    IF BUTTONS=%0000000001000001 THEN GOSUB FourF  'pin 0 and 6
    IF BUTTONS=%0000000010000001 THEN GOSUB FiveF  'pin 0 and 7
    IF BUTTONS=%0000000100000001 THEN GOTO Main    'pin 0 and 8
    
    IF BUTTONS=%0000000100000010 THEN GOSUB FourB  'pin 1 and 8
    IF BUTTONS=%0000000010000010 THEN GOSUB ThreeB 'pin 1 and 7
    IF BUTTONS=%0000000001000010 THEN GOSUB TwoB   'pin 1 and 6
    IF BUTTONS=%0000000000100010 THEN GOSUB OneB   'pin 1 and 5
    IF BUTTONS=%0000000000010010 THEN GOTO Main    'pin 1 and 4
    
    IF BUTTONS=%0000000000010100 THEN GOSUB OneN   'pin 3 and 4
    IF BUTTONS=%0000000000100100 THEN GOSUB TwoN   'pin 3 and 5
    IF BUTTONS=%0000000001000100 THEN GOSUB ThreeN 'pin 3 and 6
    IF BUTTONS=%0000000010000100 THEN GOSUB FourN  'pin 3 and 7
    IF BUTTONS=%0000000100000100 THEN GOSUB FiveN  'pin 3 and 8
    IF BUTTONS=%0000001000000100 THEN GOSUB ReversN 'pin 3 and 9 (still needs to be done)
    
    LOOP
    
    Revers: 'going from one to reverse
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
        HIGH 14
        HIGH 15
        PAUSE 688
        LOW 15
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    OneF: 'going from neutral to one
        HIGH 14
        HIGH 15
        PAUSE 375
        LOW 15
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    TwoF: 'going from one to two
        HIGH 10
        HIGH 11
        PAUSE 937
        LOW 11
    RETURN
    
    ThreeF: 'going from two to three
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
        LOW 14
        HIGH 15
        PAUSE 375
        LOW 15
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    FourF: 'going from three to four
        HIGH 10
        HIGH 11
        PAUSE 937
        LOW 11
    RETURN
    
    FiveF: 'going from four to five
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
        LOW 14
        HIGH 15
        PAUSE 313
        LOW 15
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    OneB: 'going from two to one
        LOW 10
        HIGH 11
        PAUSE 937
        LOW 11
    RETURN
    
    TwoB: 'going from three to two
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
        HIGH 14
        HIGH 15
        PAUSE 375
        LOW 15
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    ThreeB: 'going from four to three
        LOW 10
        HIGH 11
        PAUSE 937
        LOW 11
    RETURN
    
    FourB: 'going from five to four
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
        HIGH 14
        HIGH 15
        PAUSE 313
        LOW 15
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    OneN: 'going to neutral from one
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
        LOW 14
        HIGH 15
        PAUSE 375
        LOW 15
    RETURN
    
    TwoN: 'going to neutral from two
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
        LOW 14
        HIGH 15
        PAUSE 375
        LOW 15
    RETURN
    
    ThreeN: 'going to neutral from three
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    FourN: 'going to neutral from four
        LOW 10
        HIGH 11
        PAUSE 468
        LOW 11
    RETURN
    
    FiveN: 'going to neutral from five
        HIGH 10
        HIGH 11
        PAUSE 468
        LOW 11
        HIGH 14
        HIGH 15
        PAUSE 313
        LOW 15
    RETURN
    
    
  • LSBLSB Posts: 175
    edited 2007-04-26 04:08
    Did you do this:
    IF BUTTONS=%0000000100000001 THEN DEBUG "--MATCH. SHOULD GOSUB",CR 'pin 0 and 8
    as I suggested last night?

    Also, check your IFs that start with Pin 3-- the '1' is in the wrong place for the second button.
  • metron9metron9 Posts: 1,100
    edited 2007-04-26 06:23
    Sorry the program I posted was just to illustrate that the code you are using is invalid code and does not function it creates a bug in the program.

    Just delete all this code

    BUTTONS.LOWBYTE(0)=INS.LOWBYTE(0) 'pin0
    BUTTONS.LOWBYTE(1)=INS.LOWBYTE(1) 'pin1
    BUTTONS.LOWBYTE(2)=INS.LOWBYTE(2) 'pin2
    BUTTONS.LOWBYTE(3)=INS.LOWBYTE(3) 'pin3
    BUTTONS.LOWBYTE(4)=INS.LOWBYTE(4) 'pin4
    BUTTONS.LOWBYTE(5)=INS.LOWBYTE(5) 'pin5
    BUTTONS.LOWBYTE(6)=INS.LOWBYTE(6) 'pin6
    BUTTONS.LOWBYTE(7)=INS.LOWBYTE(7) 'pin7
    BUTTONS.HIGHBYTE(0)=INS.HIGHBYTE(0) 'pin8
    BUTTONS.HIGHBYTE(1)=INS.HIGHBYTE(1) 'pin9

    and replace it with this code

    Buttons=INS

    What I was trying to point out is HIGHBYTE(1) is a pointer that doesn't point to the buttons variable.

    INS is a variable that has all of the pins bits set if the pin is high and zero if the pin is low so you just check these bits.

    You don't even need the BUTTONS variable

    You can say

    IF INS=%0000000000001001 then do something

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • LSBLSB Posts: 175
    edited 2007-04-27 13:17
    Metron9,
    Of course you are right; reading INS directly is more efficient and shorter. My intent was to make the relationship between INS bits and switches more transparent and aid understanding (same reason I didn't suggest a loop to copy the bits into BUTTONS). BUTTONS, too, my be written to without consequence-- allowing one button to set many bits (I'm still not sure why it takes more buttons than there are gears). It also allows for comparing old and new patterns of presses --suggestions I did not make, but considered in writing the code.

    Of more concern to me is that HIGHBYTE causes a problem; in testing the code I wired only two buttons and tried them in two positions each, it didn't occur to me that the high byte would respond any differently than the low byte. While I appreciate knowing this, it creates more questions than it answers. Perhaps this weekend I will have more time to test this, or perhaps Parallax staff will weigh in to clarify how I should use this correctly.

    In any case, I appreciate your stepping in to correct my error and I hope that SV806 has not suffered for it.
  • metron9metron9 Posts: 1,100
    edited 2007-04-27 14:56
    I think it's just as simple as the array function or the lookup function. The PBASIC interpreter was not designed to be foolproof in these areas. Its a micro controller not a PC and has limits of how much error checking can be implemented.

    As far as the HIGHBYTE and LOWBYTE usage with modifiers (0...x) I see it this way.

    A word value BUTTONS VAR WORD for example only has 2 bytes.

    HIGHBYTE and LOWBYTE so a modifier in either HIGHBYTE or LOWBYTE other than 0 as 0 is the same as no modifier has no meaning even though you can execute the code.

    I find these modifiers to be easy to use and understand without adding the numerical index modifier to it. The documentation says:

    Somebody said...

    The commonsense rule for combining modifiers is that they must get progressively smaller from left to right. It would make no sense to specify, for instance, the low byte of a nibble, because a nibble is smaller than a byte! And just because you can stack up modifiers doesn't mean that you should unless it is the clearest way to express the location of the part you want get at. The example above might be improved:


    as well as this
    Somebody said...

    If you looked closely at that example, you probably thought it was a misprint. Shouldn't myBytes.LowNib(1) give you the low nibble of byte 1 of the array rather than the high nibble of byte 0? Well, it doesn't. The modifier changes the meaning of the index value to match its own size.

    Now I have been programming in assembler for a long time and accessing parts of arrays in this manner is quite confusing.

    It aslo gos on to read..
    Somebody said...

    This property of modified arrays makes the names a little confusing. If you prefer, you can use the less-descriptive versions of the modifier names; Bit0 instead of LowBit, Nib0 instead of LowNib, and Byte0 instead of LowByte. These have exactly the same effect, but may be less likely to be misconstrued.


    So it is confusing or can be to use these modifiers with array values (1...xxx). I don't think many programmers ever need to use them as there are so many other ways to access the bits, nibbles, and bytes via their own memory register names.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-04-27 21:45
    Hi guys, I have never used the modifiers, I guess its an array that can be used without declaring the array.

    Using the syntax BUTTONS.HIGHBYTE.BITX , x being whichever bit you are addressing works·with the previous code examples, what is the advantage of using the modifiers in this situation , and if used in other situations would it work if you declared it as 16 bit array before using it.

    Jeff T.
Sign In or Register to comment.