Home AMX User Forum NetLinx Studio

Loops

Can we use For loops in a netlinx program, eg

INTEGER i
for(i = 0 ; i < nLENS_STEPS ; i ++)

Thanks

Comments

  • jjamesjjames Posts: 2,908
    Sure can!

    You can even use WHILE loops as well! :D

    Keep in mind that NetLinx is not 0 based, all arrays start at 1.
  • Loops

    integer i
    FOR( i = 1; i < 19; i ++ )

    The code above gives me the following error. Any ideas



    ERROR: 764): C10567: Unrecognized node type [423] in compileStatement()
  • Loops

    I had the integer i, within a function call. Got it fixed
  • viningvining Posts: 4,368
    pnaidoo1502 wrote:

    Now I know I shouldn't ask this but wasn't that questioned covered in any of the courses that afforded you the title of "AMX Certified Expert Programmer", or were you playing hooky that day?
    Can we use For loops in a netlinx program, eg

    INTEGER i
    for(i = 0 ; i < nLENS_STEPS ; i ++)

    Thanks
    __________________
    Pooven Naidoo
    AMX Certified Expert Programmer
  • GSLogicGSLogic Posts: 562
    I had the integer i, within a function call. Got it fixed
    You can have variables, arrays and structures in a function!
    You can also have multiple loops going at the same time.
    for(i=1, j=10, k=90;   i<10;    i++, j++, k--)
    {
       send_string 0, "'i:', itoa(i), ' ~ j:', itoa(j), ' ~ k:', itoa(k), ";
    }
    

    If designed right, loops are the key for writing limited amounts of code and for great rides. :)
  • DHawthorneDHawthorne Posts: 4,584
    And done wrong, they can be the key to some truly spectacular train wrecks :D
  • feddxfeddx Posts: 183
    DHawthorne wrote: »
    And done wrong, they can be the key to some truly spectacular train wrecks :D

    mmmmm Train wrecks.... :P~

    I've gotten into the habit of using nested for loops for feedback. For example, if I have multiple panels, thereby having several instances of input feedback, I may try something like this:
    Dev     TP[]={...}  //19 Touchpanels
    Integer in_butt[] = {...}  //64 input buttons
    
    Input [19] //Monitors panel's individual input selection
    
    
    local var integer TTPCount
    local var integer TTButtcount
    
    For (TTPCount=1;TTPCount<=19;TTPCount++)
    {
    	FOR (TTButtcount=1;TTButtcount<=64;TTButtcount++)
    	{
    		[tp[TTPCount],in_Butt[TTButtcount]]	=	(input[TTPCount]=TTButtcount)
    	}	
    }
    
    

    The train wreck cones in when I do some quick coding, use local variables for counters named x, x1, x2,etc., and then accidentally transpose x1 for x2 or x.... I actually did that in my programmer 2 class and brought my processor to a screeching halt. Even my instructor was impressed at how elegantly I crushed the controller.

    Just be careful when nesting for loops.
  • mpullinmpullin Posts: 949
    I never use global variables such as i, j, k anymore for counting loops. It's real easy at first, you don't have to define STACK_VARs every time you want to count out a loop, but eventually you'll get zapped by calling a function inside a for loop, and inside the function is another for loop that uses the same counter as the outer for loop.

    Get a new plate each time you go to the buffet, even if it is a few steps away and your current plate only has a couple crumbs on it.
  • avi_daveavi_dave Posts: 62
    Arrays+Loops = Very happy/Short program
  • GSLogicGSLogic Posts: 562
    mpullin wrote: »
    I never use global variables such as i, j, k anymore for counting loops. It's real easy at first, you don't have to define STACK_VARs every time you want to count out a loop, but eventually you'll get zapped by calling a function inside a for loop, and inside the function is another for loop that uses the same counter as the outer for loop.

    Get a new plate each time you go to the buffet, even if it is a few steps away and your current plate only has a couple crumbs on it.
    I also never use global variables for i,j,k, I setup stack_vars every time... keeps me out of trouble.
    I liked the clean plate correlation. :)
  • mpullin wrote: »
    Get a new plate each time you go to the buffet, even if it is a few steps away and your current plate only has a couple crumbs on it.
    We heartily support programmer consumerism. :D

    And though it's been said, I too agree; keeping variables which can be handled locally out of the global realm definitely helps to keep one clean of an over-excess of global variables which leaves one open for tying up code over a single variable.

    For heaven's sake, don't be snooty; give the dishwasher kid a job. :)
  • ericmedleyericmedley Posts: 4,177
    GSLogic wrote: »
    I also never use global variables for i,j,k, I setup stack_vars every time... keeps me out of trouble.
    I liked the clean plate correlation. :)

    I do, but I give the loop counters a descriptive name to keep them straight. I like to make them full-time variables so I can track them in the loop for debugging.

    I have to admit that I almost never use stack_var or local_var myself. I just don't think it burns up that much RAM.
  • DHawthorneDHawthorne Posts: 4,584
    ericmedley wrote: »
    I have to admit that I almost never use stack_var or local_var myself. I just don't think it burns up that much RAM.

    I had a system run out of memory and just stop because of too many non-volatile declarations in its component modules. Since then, I always declare variable either non-volatile or stack as a matter of course (when the situation allows it, of course), because I don't want whatever I'm working on to become that last straw in the next project. I by no means consider it necessary in every case, but neither does it hurt anything, and it saves that panicked re-write at the 11th hour.
  • Spire_JeffSpire_Jeff Posts: 1,917
    DHawthorne wrote: »
    Since then, I always declare variable either non-volatile or stack as a matter of course (when the situation allows it, of course), because I don't want whatever I'm working on to become that last straw in the next project.

    Just be aware of the fact that a program with NO non-volatile variables will cause the debugger to freak out! You have to have at least 1 non-volatile variable, and it doesn't have to do anything :)

    Jeff
  • DHawthorneDHawthorne Posts: 4,584
    Spire_Jeff wrote: »
    Just be aware of the fact that a program with NO non-volatile variables will cause the debugger to freak out! You have to have at least 1 non-volatile variable, and it doesn't have to do anything :)

    Jeff

    Once a program is running properly, this is not usually an issue. I will often declare a variable local until it's debugged, then change it to stack afterward. But, more often, I'll bypass the buggy debugger altogether and use carefully placed send_string 0's.
  • ericmedleyericmedley Posts: 4,177
    DHawthorne wrote: »
    I had a system run out of memory and just stop because of too many non-volatile declarations in its component modules. Since then, I always declare variable either non-volatile or stack as a matter of course (when the situation allows it, of course), because I don't want whatever I'm working on to become that last straw in the next project. I by no means consider it necessary in every case, but neither does it hurt anything, and it saves that panicked re-write at the 11th hour.

    Oh, I didn't say that I don't use Volatile. All my variables have one of those keywords before it. Volatile, Persistent...

    I just don't bother with local_vars or stack_vars. It's no biggie. For me it's nice to not have to worry about keeping the value for a little time after the event and/or loop's done.
  • feddxfeddx Posts: 183
    Ok Stack vs Local

    I see that this has gone a bit far afield, so I'll pile on.

    I prefer to use Local_Var as I can check them in the "buggy" Debugger if I have problems (which is all the time). I was told that Stack_Var's were blown out as soon as the event was over and therefore uncheckable.

    Is there REALLY that much of a memory savings from Local to Stack? Or is there another advantage I don't know of?

    Thanks, and excuse my ignorance on this. Even though I've gotten my "certificationizationism" (sorry W), I've learned most of my AMX programming from trial and error, and cannot recall everything I learned in a classroom 3 years ago.....
  • It's not that a Stack_Var is impossible to check from within the debugger (and honestly, I really have no problems with Debugging, so I'm not sure where the implication that it doesn't work well comes from); if you wish to check a Stack_Var, you simply need to place a breakpoint in the code in the method containing the variable. Once the processor is stopped at that point, you can check it just like any other variable.

    Local_Var is simply, in a sense, more easily read, as it retains its value even after that particular method has been left.

    I tend to use Stack_Var more often, as I don't particularly like relying on a method to perfectly retain the value of the variable since it was last entered. If I need the value elsewhere, I simply create a global variable, otherwise I don't mind reintroducing the value to the variable each time its used. The only exception I've encountered for this is when requiring the variable to be accessible from within a Wait - in which case a Local_Var is necessary.

    I like this method, as it does keep memory rather cleared up, but mine's just one style among many. :)

    Hope this helps.
  • DHawthorneDHawthorne Posts: 4,584
    ... (and honestly, I really have no problems with Debugging, so I'm not sure where the implication that it doesn't work well comes from); if you wish to check a Stack_Var, you simply need to place a breakpoint in the code in the method containing the variable. ...

    There is no implication, it just doesn't work well. Try debugging a variable inside a module that is running multiple instances, or came from an include file. My special favorite is when you add a watch, and it asks you which of four includes it's named in to use ... and in fact, it's not in any of them ... even when you dragged it directly from the code. I'm not saying it's useless ... I use it all the time, in fact. It just has some serious flaws.
  • DHawthorne wrote: »
    It just has some serious flaws.
    C'est la vie.

    I agree that dragging in variables of multiple instances tends to work poorly. For this reason I tend to just type in the variable myself, such as: "FUNCTIONNAME:VARIABLE" and call it a day. Admittedly, however, I've little experience with debugging multiple instances of a module, so I'll submit that to you by default.

    Out of curiosity, are you aware of any completely debilitating pitfalls of the debugger? I mean, something that does not work, causes problems, and/or has no workaround?
  • ericmedleyericmedley Posts: 4,177
    C'est la vie.

    I agree that dragging in variables of multiple instances tends to work poorly. For this reason I tend to just type in the variable myself, such as: "FUNCTIONNAME:VARIABLE" and call it a day. Admittedly, however, I've little experience with debugging multiple instances of a module, so I'll submit that to you by default.

    Out of curiosity, are you aware of any completely debilitating pitfalls of the debugger? I mean, something that does not work, causes problems, and/or has no workaround?

    I have it crash to program many times. It seems to be when I'm monitoring several variables and they get a little verbose. It's particularly dicey if you're monitoring a buffer or two.

    I've gotten in the habit of 'saving' a lot when I'm running the debugger.
  • feddxfeddx Posts: 183
    but mine's just one style among many. :)

    And like a piggy bank this is just a penny. (I know, I'm old)

    Thank you. So no real advantage, just preference. Any real memory difference?

    As for debug, I've not had many problems with it (I recall at one point having the same problem that "ericmedley" did, but I think it was because I was doing serial communication to the master. 99.99% of the time I'm using IP communication even if the system is not on a network, and I no longer seem to have that problem). But I usually am monitoring only global variables in my main code. And I try to use proven modules for multiple devices (I'm currently working on a project with about 50 of the same LCD panels, monitoring variables inside 1 of the nearly 50 instances of these modules would be a daunting task).

    But I HAVE noticed that if you setup Debug with a variable that has multiple instances, and isolate that instance once, EVERY time I restart Debug, I have to RETELL the Debugger which instance it is (after a reboot for example). This could be though because I do not save any debug info with the program.

    But I'm pro Debug. It's got it's problems, but it's a handy little tool.
  • Admittedly, I'm not aware of precisely how much memory it would use up, perhaps someone with more detailed knowledge of the processor could help you there. I do know, however, that wherein with a Stack_Var there's no retention, the processor maintains the value of the Local_Var no matter what it does - same as a global variable, really, with the difference that Local_Var is, in fact, only locally accessible. With that in mind, it's a matter of buildup - one Local_Var, single array variable is no problem, but those things add up, and in my experience, it's better to work with a more conservative mindset.

    Hope this helps.
Sign In or Register to comment.