Home AMX User Forum AMX General Discussion
Options

How does this loop get screwed up?

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

  • Options
    a_riot42a_riot42 Posts: 1,624
    vegastech wrote: »
    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
  • Options
    Jorde_VJorde_V Posts: 393
    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?
  • Options
    Spire_JeffSpire_Jeff Posts: 1,917
    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
  • Options
    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.
  • Options
    Spire_JeffSpire_Jeff Posts: 1,917
    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
  • Options
    ericmedleyericmedley Posts: 4,177
    vegastech wrote: »
    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.
  • Options
    yuriyuri Posts: 861
    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 ;)
  • Options
    What about the Break command? Wouldn't that typically stop the loop after the first run?
  • Options
    vegastech wrote: »
    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
  • Options
    jjamesjjames Posts: 2,908
    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.
Sign In or Register to comment.