How does this loop get screwed up?
vegastech
Junior Member
I have this for loop programmed into a button press:
LOCAL_VAR integer nChannel FOR (nChannel=0;nChannel<4;nChannel++)but it seems that when I press the button, the variable starts at 0, and then jumps to 4. Shouldn't it only increment by 1 after each press?
Comments
-
I have this for loop programmed into a button press:
LOCAL_VAR integer nChannel FOR (nChannel=0;nChannel<4;nChannel++)
but it seems that when I press the button, the variable starts at 0, and then jumps to 4. Shouldn't it only increment by 1 after each press?
Your for loop isn't going to stop after one time through and wait for you to hit the button again. Its going to execute the loop until nChannel is not less than 4 every time you hit the button. What were you expecting to happen?
Paul -
It continues for as long as nChannel is less than 4, hence it will not stop before it reaches 4. What is it that you want to do?
-
here is some code to accomplish this:
button_event[dvTp,nBtn]{ push:{ local_var integer nChannel; nChannel = (nChannel+1)%4; //nChannel will cycle 0,1,2,3,0,1,2,3,0... } }
I think this should work.
Jeff -
I want it to increment by 1 after each pass, and then stop until I press another button, until I have pressed 3 buttons. I have a variable array that I am trying to fill 3 digits with. My goal here is to press 3 numerics, and have a numeric load the 1st, then 2nd, and finally the 3rd spot, to then send out a 3 digit channel.
-
Try this:
define_variable volatile integer nChannel = 1; button_event[dvTp,nBtn]{ push:{ nChannel++; //Store button number data accordingly. } release:{ if(nChannel >= 3){ nChannel = 1; //send data to device. } } }
Jeff -
I want it to increment by 1 after each pass, and then stop until I press another button, until I have pressed 3 buttons. I have a variable array that I am trying to fill 3 digits with. My goal here is to press 3 numerics, and have a numeric load the 1st, then 2nd, and finally the 3rd spot, to then send out a 3 digit channel.
Yeah, a FOR loop doesn't work that way in Netlinx. Once started it will run on its own without stopping. There's not really a 'Next' command as you might see in other programming languages. -
first time i used a FOR loop in Netlinx i expected to "see" it count in the debug window.
But instead i just saw 0 -> 255
-
What about the Break command? Wouldn't that typically stop the loop after the first run?
-
What about the Break command? Wouldn't that typically stop the loop after the first run?
It'll break the FOR loop when it's encountered but it won't restart it.
--John -
Spire_Jeff wrote: »
define_variable volatile integer nChannel = 1; button_event[dvTp,nBtn]{ push:{ nChannel++; //Store button number data accordingly. } release:{ if(nChannel >= 3){ nChannel = 1; //send data to device. } } }
This is where a button_var (a variable only specific to a button event; broader scope than a local or stack, but smaller than a global) would come very handy. Unfortunately, in a circumstance like this we "waste" / unnecessarily use a global variable.
Anyway - I'd vouch for code like this.
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums