Shop OBEX P1 Docs P2 Docs Learn Events
Using Arrays when Expanding I/Os — Parallax Forums

Using Arrays when Expanding I/Os

RGR_EngineerRGR_Engineer Posts: 18
edited 2009-12-15 20:43 in BASIC Stamp
I currently have 16 inputs and 16 outputs (using the expanding inputs and expanding outputs code from the parallax examples) coming off of a BS2p module. Here is my problem.

What I want my program to do:
I want to be able to go to a subroutine the displays "Male Limit Pressed" when the first button is pressed regardless of whether or not the other buttons are pressed.

What it currently does:
I can see when each button is press (via the DEBUG "Switches = ", bin8 Trigger, CR line) but the program never goes to the subroutine.

What I think I have (but I've never worked with arrays before) is each individual bit of the array (Triggers) put into Switches as the input variables. Is that right? I'm kind of stuck, I've tried everything I can think of and looked at several different websites. Am I on the right track? What am I doing wrong that is preventing the program to go to the subroutine? My code is below.

' {$STAMP BS2p}
' {$PBASIC 2.5}

' =========================================================================
'
'   File......Array Test.bs2
'   Purpose...To test the Array function with the expanding Inputs and Outputs
'   Started...12/11/09
'   Updated...12/15/09
'
'
' =========================================================================


' -----[noparse][[/noparse] Program Description ]---------------------------------------------
'When the first "switch" is pressed (MaleLimit), the program will say "Male
'Limit Pressed" and stop.

' -----[noparse][[/noparse] Revision History ]------------------------------------------------


' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
Clock     CON 15 ' shift clock (74x165.2)
DataOut   CON 14 ' serial data out (74HC595.14)
Latch     CON 13 ' output latch (74HC595.12)
DataIn    CON 12 ' shift data (74x165.7)
Load      CON 11 ' input load (74x165.1)

' -----[noparse][[/noparse] Constants ]-------------------------------------------------------


' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
n                    VAR Nib
Triggers             VAR Byte(8)
MaleLimit            VAR Bit
FemaleLimit          VAR Bit
ScooperLimit         VAR Bit
ScooperTrigger       VAR Bit
DispenserTrigger     VAR Bit
AirCylinderTrigger   VAR Bit
MaleTabs             VAR Bit
FemaleTabs           VAR Bit
switches             VAR Byte ' inputs switches
buttons              VAR Byte ' push button inputs
pattern              VAR Byte ' output pattern
counter              VAR Byte
MaleLimit            = Triggers(0)
FemaleLimit          = Triggers(1)
ScooperLimit         = Triggers(2)
ScooperTrigger       = Triggers(3)
DispenserTrigger     = Triggers(4)
AirCylinderTrigger   = Triggers(5)
MaleTabs             = Triggers(6)
FemaleTabs           = Triggers(7)

' -----[noparse][[/noparse] EEPROM Data ]-----------------------------------------------------


' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
Initialize:
LOW Latch ' make output and keep low
pattern = %11111111
counter = %00000000
HIGH Load ' make output; initialize to 1

' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
main:
GOSUB Read_165 ' read switches and buttons
IF (MaleLimit   = 1) THEN GOTO here

FOR n = 0 TO 7
  Triggers(n) = switches
  switches    = switches >> 1
  GOSUB Out_595
  PAUSE 20
NEXT

DEBUG "Swithces = ", BIN8 Triggers, CR
PAUSE 20
GOTO main

' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
Read_165:
PULSOUT Load, 5 ' latch inputs
SHIFTIN DataIn, Clock, MSBPRE, [noparse][[/noparse]buttons] ' get buttons
SHIFTIN DataIn, Clock, MSBPRE, [noparse][[/noparse]switches] ' get switches
RETURN

Out_595:
SHIFTOUT DataOut, Clock, MSBFIRST, [noparse][[/noparse]counter] ' send counter to 2nd 74HC595
SHIFTOUT DataOut, Clock, MSBFIRST, [noparse][[/noparse]pattern] ' send pattern to 1st 74HC595
PULSOUT Latch, 5 ' latch outputs
RETURN

here:
DEBUG "Male Limit Pressed", CR
pattern = %00000000
counter = %11111111
GOSUB Out_595
PAUSE 1000
END



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 17:53
    Tara,

    In your code when you read the shift-registers your data is in the variables buttons and switches. Immediately after the read you’re testing for MaleLimit to equal 1 but that variable has not been set. It seems you’re trying to alias an array but the data isn’t in the array at this point. From looking at the FOR…NEXT loop following I would recommend instead to test for the bit you’re looking for. For example:

    IF buttons.BIT3 = 1 THEN …

    Also, it is better to attach full programs…trying to follow a full program pasted into the message is difficult at best. Attaching it allows it to be easily loaded into the editor with all formatting intact. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 18:06
    Chris,
    I have also tried putting those two lines (GOSUB Read_165 ' read switches and buttons; IF (MaleLimit = 1) THEN GOTO here) below the FOR...NEXT loop and I get the same result. I have also tried it using

    If buttons.BIT0 =1 THEN GOTO here

    and again I get the same results. I have attached my code. Is there anything else I can try?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 18:25
    Okay, not sure what is happening...let's try focusing first on the aliasing of the variables...instead of trying to deal with an array you know you're going to get 16 bits in two 8-bit variables. Your target variables are going to be in one of them (not sure how you have them connected). Your aliasing should look more like this (arrays only confuse the issue here):

    MaleLimit            = variable.BIT0
    FemaleLimit          = variable.BIT1
    ScooperLimit         = variable.BIT2
    ScooperTrigger       = variable.BIT3
    DispenserTrigger     = variable.BIT4
    AirCylinderTrigger   = variable.BIT5
    MaleTabs             = variable.BIT6
    FemaleTabs           = variable.BIT7
    
    

    ...where variable is either buttons or switches depending on which shift-register you're connected to.· Remember, the first one in the chain goes into buttons and the last one goes into switches.· I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!


    Post Edited (Chris Savage (Parallax)) : 12/15/2009 6:31:54 PM GMT
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 18:39
    When I do that, I get an error message (see attached screen shot). I tried labeling them that way in the beginning and kept getting that error as well.

    If it helps, I have everything set up as described in www.parallax.com/dl/docs/books/sw/exp/sw23b.pdf and www.parallax.com/dl/docs/books/sw/exp/sw24b.pdf.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara

    Post Edited (RGR_Engineer) : 12/15/2009 6:52:59 PM GMT
    632 x 523 - 64K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 18:45
    Your screen shot didn't make it? Please retry and include the top of your code where the variables are located in the picture.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 18:52
    Sorry about that, I fixed the post and added the attachment.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 19:01
    Wow...I am having a moment here...haha, sorry, don't know where my mind was! You can't use = signs...must be VAR for alias. Sorry about that...I didn't catch that error in your original code so I copied it over with it.
    [size=2][code]
    MaleLimit            VAR variable.BIT0
    FemaleLimit          VAR variable.BIT1
    ScooperLimit         VAR variable.BIT2
    ScooperTrigger       VAR variable.BIT3
    DispenserTrigger     VAR variable.BIT4
    AirCylinderTrigger   VAR variable.BIT5
    MaleTabs             VAR variable.BIT6
    FemaleTabs           VAR variable.BIT7
    
    


    [/code][/size]
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 19:15
    I was able to get the button pressing to work using

    Triggers             VAR Bit(8)
    

    instead of
    Triggers             VAR Byte(8)
    



    But now the line:
    DEBUG "Switches = ", BIN8 Triggers, CR
    

    no longer displays the pressing of the buttons. Instead I get all 0s.

    Even when I do this:

    Triggers             VAR Bit(8)
    switches             VAR Byte ' inputs switches
    MaleLimit            VAR switches.BIT0
    FemaleLimit          VAR switches.BIT1
    ScooperLimit         VAR switches.BIT2
    ScooperTrigger       VAR switches.BIT3
    DispenserTrigger     VAR switches.BIT4
    AirCylinderTrigger   VAR switches.BIT5
    MaleTabs             VAR switches.BIT6
    FemaleTabs           VAR switches.BIT7
    
    



    I'm not sure why the display is wrong now that the button is working correctly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 19:21
    Because you're printing the variable 'Triggers' but that's not where your bits are...they're in 'switches'. Change Triggers to switches and it should work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 19:23
    I got it working now. Thank you so much. You've been a huge help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 19:28
    No problem...hopefully others can also benefit from this. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 20:20
    Oh, one more related question that I can't seem to find an answer to... Is it possible to toggle the value of a variable?

    Example:
    I have input variables (the ones listed in my above post) and output variable
    Motors               VAR Byte ' output Motors
    MaleMotor            VAR Motors.BIT0
    FemaleMotor          VAR Motors.BIT1
    ScooperMotor         VAR Motors.BIT2
    DriveMotor           VAR Motors.BIT3
    MaleRev              VAR Motors.BIT4
    FemaleRev            VAR Motors.BIT5
    ScooperRev           VAR Motors.BIT6
    AirCylinder          VAR Motors.BIT7
    
    



    I want to Toggle the value of the MaleRev (Motors.BIT4) when MaleLimit (Triggers.BIT0) is pressed.

    My code is attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-15 20:29
    MaleRev = ~MaleRev

    =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
  • RGR_EngineerRGR_Engineer Posts: 18
    edited 2009-12-15 20:43
    Thank you! That worked perfectly [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Tara
Sign In or Register to comment.