Home AMX User Forum NetLinx Studio

Resetting a count

There's gotta be a better way than this...
i_theNumber ++;
if(i_theNumber == 4)
  i_theNumber = 1;

I've never really thought about it - but I am now.

What's more processing power, a mathematical equation to creating a "ceiling" or an evaluation?

Comments

  • Jorde_VJorde_V Posts: 393
    jjames wrote: »
    There's gotta be a better way than this...
    i_theNumber ++;
    if(i_theNumber == 4)
      i_theNumber = 1;
    

    I've never really thought about it - but I am now.

    What's more processing power, a mathematical equation to creating a "ceiling" or an evaluation?

    Are you trying to make a for loop?
  • Spire_JeffSpire_Jeff Posts: 1,917
    Are you looking to compare something like:
    local_var x,y;
    
    x++;
    if(x>4)
       x = 0;
    y = (y+1)%5;
    
    or, if you want to go from 1-4:
    local_var x = 1;
    local_var y = 1;
    
    x++;
    if(x>4)
       x = 1;
    y = (y%4)+1;
    

    If this is what you are asking, I can try to run it through a test later today.

    Jeff
  • jjamesjjames Posts: 2,908
    Essentially I'm creating a manual for loop; I basically have the alphabet on 9 buttons (abc,def,ghi, etc. etc.) - user hits the button that has ABC on it once, it shoots out 'A', hits it again, it shoots out 'B', again then 'C', again - 'A'. Useful for small remotes so you don't have 26 buttons for each letter.

    I just didn't know if there was a better way of doing it without the comparison of saying, "if you've reached the max, reset yourself back to the beginning." It just doesn't seem very streamlined is all.
  • truetrue Posts: 307
    Unless you want to make sure your count can be evenly divided into the max of a data type, and if you want to understand what you did later on, that's the way to do it.

    Usually greater than would be used rather than equality...unlikely there should be a bug to affect the variable or a cosmic ray will hit the right place and cause the variable to be some random value but who knows? things happen
  • ericmedleyericmedley Posts: 4,177
    true wrote: »
    Unless you want to make sure your count can be evenly divided into the max of a data type, and if you want to understand what you did later on, that's the way to do it.

    Usually greater than would be used rather than equality...unlikely there should be a bug to affect the variable or a cosmic ray will hit the right place and cause the variable to be some random value but who knows? things happen



    I suppose you could just use the overrun of an integer.

    65535/4 parts and just keep adding them up. the overrun will do the reset for you.
  • kbeattyAMXkbeattyAMX Posts: 358
    You could do what Eric mentioned with an overrunning integer and use band.
    counter++
    counter band 1 for the first letter
    counter band 2 for the second letter
    counter band 3 for the third letter

    That should work... binary is
    00000000
    00000001
    00000010
    00000011
    00000100
    00000101
    00000110
    00000111 the repeat seems to happen naturally
  • a_riot42a_riot42 Posts: 1,624
    I do something similar with the alphabet but don't use hard coded numbers like that. Instead I have a 2 dimensional array that stores each group of characters, and at every button press I increment the index and check to see if its larger than the length of the array, and if so set the index back to 1. That way, you can have a different number of characters per array and it will get set back to 1 whenever the index goes past the lenght of the array. If you change the arrays at some point you don't need to change any magic numbers in your code.
    Paul
  • jjamesjjames Posts: 2,908
    a_riot42 wrote: »
    I do something similar with the alphabet but don't use hard coded numbers like that. Instead I have a 2 dimensional array that stores each group of characters, and at every button press I increment the index and check to see if its larger than the length of the array, and if so set the index back to 1. That way, you can have a different number of characters per array and it will get set back to 1 whenever the index goes past the lenght of the array. If you change the arrays at some point you don't need to change any magic numbers in your code.
    Paul

    I like that . . . I thought about that briefly, but am unsure at this moment why i disregarded it. With a clearer head, I'll stick with that approach.
  • mushmush Posts: 287
    I have just done the same thing in a job.
    I know this doesn't answer your question but here's how I did it..
    BUTTON_EVENT[vdvTPCommon,btnSEARCH]
    {
    	PUSH:
    	{
    		switch(GET_LAST(btnSEARCH))
    		{
    			case 1:									// abc
    			{
    				if(cSearchCounter < 'C')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'A'
    				}
    			}
    			case 2:									// def
    			{
    				if(cSearchCounter > 'C' and cSearchCounter < 'F')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'D'
    				}
    			}
    			case 3:									// ghi
    			{
    				if(cSearchCounter > 'F' and cSearchCounter < 'I')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'G'
    				}
    			}
    			case 4:									// jkl
    			{
    				if(cSearchCounter > 'I' and cSearchCounter < 'L')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'J'
    				}
    			}
    			case 5:									// mno
    			{
    				if(cSearchCounter > 'L' and cSearchCounter < 'O')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'M'
    				}
    			}
    			case 6:									// pqrs
    			{
    				if(cSearchCounter > 'O' and cSearchCounter < 'S')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'P'
    				}
    			}
    			case 7:									// tuv
    			{
    				if(cSearchCounter > 'S' and cSearchCounter < 'V')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'T'
    				}
    			}
    			case 8:									// wxyz
    			{
    				if(cSearchCounter > 'V' and cSearchCounter < 'Z')
    				{
    					cSearchCounter ++
    				}
    				else
    				{
    					cSearchCounter = 'W'
    				}
    			}
    		}
    		fnSortPriority()
    		fnSearch(cSearchCounter)
    	}
    }
    
    

    Work very well for me.
Sign In or Register to comment.