Using Arrays when Expanding I/Os
RGR_Engineer
Posts: 18
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara
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
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!
·
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
...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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Check out the new Savage Circuits TV!
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara
[/code][/size]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Check out the new Savage Circuits TV!
·
instead of
But now the line:
no longer displays the pressing of the buttons. Instead I get all 0s.
Even when I do this:
I'm not sure why the display is wrong now that the button is working correctly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Check out the new Savage Circuits TV!
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Check out the new Savage Circuits TV!
·
Example:
I have input variables (the ones listed in my above post) and output variable
I want to Toggle the value of the MaleRev (Motors.BIT4) when MaleLimit (Triggers.BIT0) is pressed.
My code is attached.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Check out the new Savage Circuits TV!
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Tara