Variable declarations in FOR loop

Other than the fact that a declaration must match the type of object being declared i.e can’t use char where int is required etc., does it matter what you use in a FOR loop for large sketches? For example the code below accepts two variables when called which determine the case (numCase) and number of LED blinks (numBlink) required. Only single digits are passed in and char would do fine, I just happened to use int. Does this matter to the ultimate size of the compiled sketch?

If you’re wondering about the use of delay here it’s because this function is only called during setup and will not effect looping code.

void feedBack(int numCase , int numBlink)//for audio/visual feedback
  {
  switch(numCase)//feed back type
        {
      case 1:// operation complete
          tone(6, beepFreq, beepTime); //output tone
          delay(250);
          noTone(6);
          tone(6, beepFreq, beepTime);
          delay(250);
          noTone(6);
		for (int z = 0 ;  z < numBlink ; z++)
		  {			
		  digitalWrite(greenLED, HIGH);
		  delay(1000);
		  digitalWrite(greenLED, LOW);
		  }
	    break;
    
     case 2://operation failed - try again
          tone(6, beepFreq, beepTime); //high tone
          delay(200);
          noTone(6);
          tone(6, 250, beepTime);//low tone
          delay(200);
	  noTone(6);
 		for (int z = 0 ;  z < numBlink ; z++)
		  {			
		  digitalWrite(yellowLED, HIGH);
		  delay(2000);
		  digitalWrite(yellowLED, LOW);
		  }  
	    break;
          
      case 3://operation failed - start over
		digitalWrite(redLED, HIGH);
          tone(6, beepFreq, 3000);
		digitalWrite(redLED, LOW);
		noTone(6);
		  for (int z = 0 ;  z < numBlink; z++)
		    {			
		     digitalWrite(redLED, HIGH);
		     delay(1000);
		     digitalWrite(redLED, LOW);
		    }  
	    break;

Try declare your index counter "z" as global, then remove the keyword "int" in all yor for loops. Remember to record your memory usage before - and after this change :-)

Would be interesting to read your observations !

Anders53: Try declare your index counter "z" as global, then remove the keyword "int" in all yor for loops. Remember to record your memory usage before - and after this change :-)

Would be interesting to read your observations !

It is prudent to declare variable ( scope) close to its usage. Variable z is "local" to for loop and declaring its scope global is not a good programming practice. It is also a common convention, not a strict rule, to name index variables starting with i.

I deliberately kept the scope local to prevent mix ups. Good reminder on the naming convention, I'd forgotten that.

I agree Vaclav -

But in this instance we are looking to optmize things, which means we sometimes needs to be un-conventionel in thibking :-)

Besides - the index variable will always be reset to 0, whenever you get into a for loop.

Declare variable uninitialized and only once early at the beginning of function or each time use different name (different variable). In second case compiler optimize it and will use the same register with high probability. In case of global variable it takes a place in the memory, it means additional instructions also. It is wasting of sources for counter which lifetime is just 'for' loop.

Budvar 10

So you suggest int z = 0; at the beginning of the function and just call z in the FOR loops?

void feedBack(int numCase , int numBlink)//for audio/visual feedback
  { int z = 0;
  switch(numCase)//feed back type
        {
      case 1:// operation complete
          tone(6, beepFreq, beepTime); //output tone
          delay(250);
          noTone(6);
          tone(6, beepFreq, beepTime);
          delay(250);
          noTone(6);
		for (z = 0 ;  z < numBlink ; z++)
		  {			
		  digitalWrite(greenLED, HIGH);
		  delay(1000);
		  digitalWrite(greenLED, LOW);
		  }
	    break;
     //     msBeepLast = msBeep; 
 
     case 2://operation failed - try again
          tone(6, beepFreq, beepTime); //high tone
          delay(200);
          noTone(6);
          tone(6, 250, beepTime);//low tone
          delay(200);
	  noTone(6);
 		for (z = 0 ;  z < numBlink ; z++)
		  {			
		  digitalWrite(yellowLED, HIGH);
		  delay(1000);
		  digitalWrite(yellowLED, LOW);
		  }  
	    break;
          
      case 3://operation failed - start over
		digitalWrite(redLED, HIGH);
          tone(6, beepFreq, 3000);
		digitalWrite(redLED, LOW);
		noTone(6);
		  for (z = 0 ;  z < numBlink; z++)
		    {			
		     digitalWrite(redLED, HIGH);
		     delay(2000);
		     digitalWrite(redLED, LOW);
		    }  
	    break;

Or do you mean just declare int z; and then use int z = 0; in the FOR loops?

No, let it uninitialized. You have to initialize it to zero in the ‘for’ statement.

EDIT: Just declare.

Like this you mean?

void feedBack(int numCase , int numBlink)//for audio/visual feedback
  { int z;
  switch(numCase)//feed back type
        {
      case 1:// operation complete
          tone(6, beepFreq, beepTime); //output tone
          delay(250);
          noTone(6);
          tone(6, beepFreq, beepTime);
          delay(250);
          noTone(6);
		for (z = 0 ;  z < numBlink ; z++)
		  {			
		  digitalWrite(greenLED, HIGH);
		  delay(1000);
		  digitalWrite(greenLED, LOW);
		  }
	    break;

Yes, like this.

So CopperDog -

Did you get less memory usage … ? :slight_smile:

Vaclav: It is also a common convention, not a strict rule, to name index variables starting with i.

That's just one of those stupid conventions going back to FORTRAN, when variables I through N were automatically integers, and so more suitable as array indices.

No the number of bytes is the same when compiled. I really do think the only thing that will change the number of bytes used is when the variables are actually created. A problem wouldn't show up until the room isn't available to contain the information.

Vaclav:
Irregardless of this discussion - this won’t do much “flashing” or blinking.

for (int z = 0 ; z < numBlink ; z++)
{
digitalWrite(greenLED, HIGH);
delay(1000);
digitalWrite(greenLED, LOW);
}

As Vaclav forrgot to mention : because there is no delay after the greenLed LOW setting.

double post.

Arrg, your right! I was cutting and pasting to get the code sample for the forum. Thanks for the catch, I actually left that out in every block.

Vaclav: No, he did not. Little challenge does not hurt once a while. Use "blink without delay" and connect the grounds ( and format your code ).

Ohhh - yes he did. Not a question of challenges here, but a question of polite and friendly help.

So, by definition, you forgot ...

Vaclav: No, he did not. Little challenge does not hurt once a while. Use "blink without delay" and connect the grounds ( and format your code ).

Well I considered using millis(). I've used blink without delay in other sketches but wasn't sure how to implement it here inside a FOR loop that resides in setup(). Sorry if the code formatting is odd. I could use some instruction there I'll admit. Can you point me to a good tutorial on that by any chance?

but wasn't sure how to implement it here inside a FOR loop that resides in setup().

That's because the blink without delay concept (cooperative multitasking) and the for loop are not natural bedfellows.