Why is 'i' used in this code?

Hi All;

Thanks for looking at this post. The design intent of this project is to utilize a Adafruit Neopixel LED strip to increment and illuminate 4 LED’s at once after a determined delay. I got the code from the Neopixel example library file “simple” and modified it a bit to turn 4 LED’s and then increment. More specifically, why do is ‘i’ in the for loop and the increment used in this code (provided below)? I am a newb to arduino programming and from what I can tell, this is programmed in C.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 5000; // delay for half a second

void setup() {
  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  int j=1;

  while(j<2)
  {

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

  for(int i=0;i<60;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(255,255,0)); // Moderately bright green color.
    pixels.setPixelColor(i+1, pixels.Color(255,255,0)); // Moderately bright green color.
    pixels.setPixelColor(i+2, pixels.Color(255,255,0)); // Moderately bright green color.
    pixels.setPixelColor(i+3, pixels.Color(255,255,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

    pixels.setPixelColor(i, pixels.Color(0,0,0)); // Turn LED off.
    pixels.setPixelColor(i+1, pixels.Color(0,0,0)); // Turn LED off.
    pixels.setPixelColor(i+2, pixels.Color(0,0,0)); // Turn LED off.
    pixels.setPixelColor(i+3, pixels.Color(0,0,0)); // Turn LED off.

    i = i+3;
  }
  j=j+1;
  break;
+  }
}

Any info would be appreciated. Thanks!

Are you referring to this line?

for(int i=0;i<60;i++){

In that line ‘i’ is just the name of a variable (any name could be used) that increments through the range 0 - 59 as the FOR iterates. Personally I usually use ‘n’ simply because it is easier to read than ‘i’

…R

That's what thought. Correct me if I am wrong, why is that the 'i' is not declared above as a variable? Is it a universally recognizable variable that does not have to be declared?

It could be a global variable, but if it doesn't need to be global, we make it local so it disappears when we are finished with it.
This can help reduce the amount of ram used in a sketch.

i is declared:
int i=0;

.

BigAl10:
why is that the ‘i’ is not declared above as a variable? Is it a universally recognizable variable that does not have to be declared?

No, it is declared in the for statement and is only visible inside it.

for(int i=0; <60; i++){

Robin2:
Are you referring to this line?

for(int i=0;i<60;i++){

In that line ‘i’ is just the name of a variable (any name could be used) that increments through the range 0 - 59 as the FOR iterates. Personally I usually use ‘n’ simply because it is easier to read than ‘i’

…R

And personslly I prefer to use names that reflect what they do; e.g. cnt.

@OP, you need to read up on scope. Variables can be global (known everywhere in the code), local to a function (only known in that function so they can not accidently br modified in other functions) and local to e.g. a for-loop. In this case, i is declared in the for statement and hence, as explained, local to the for-loop.

You could say:

int i;
for(i = 0; i < 60;i++)
  // but
for(int i = 0; i < 60;i++)

Does it in 1 line, the common use of “i” for an index is a holdover from Fortran where a group of single letter variables was reserved for index variables, i, j, k, l, m and n, AWAIR.

Thanks for your help everyone. I agree, I have some elementary work to do on learning the language. I am doing a school project and I had purchased the arduino in hopes of learning how to use it. Now, I finally am, lol.

The use of i, j, k as iteration or index variables is straight from maths.

Definitely from mathematicians a very long time ago, who liked to use cluster of variables for similar things (descartes, Laplace etc)

a,b,c for coefficients (or greek equivalents)
x,y,z for unknown
f,g,h for functions,
i,j,k for summation indexes
p,q,r in probability or truth logic

Etc

holdover from Fortran where a group of single letter variables was reserved for index variables, i, j, k, l, m and n

No, they were not reserved. In Fortran, unless explicitly declared, variable types were implied by the first letter of the variable name. A variable beginning with any one of the letters I, J, K, L, M, or N was implicitly an integer. The use of those single letters for DO loop indices was, I suspect, partially a hang-over from their use in math (as has been mentioned) and partially the inherent laziness of Fortran programmers who never used 2 or more letters where 1 letter would do - and for the humour impaired I should mention that I used to be one of those programmers and I'll add a smiley just as a bonus. ;D

Pete

it was not just laziness (my first programing language was fortran and i was young and fearless, could have typed for hours :slight_smile: )

Remember - Recording was often done on punch cards then and each hole (IBM format) was a letter or number. Less letters = better use of paper = faster to save or ingest programs with less risk of jam (Later on binary representation and evolution optimized somewhat paper use)

from wikipedia
Card from a Fortran program: Z(1) = Y + W(1)

But fortran programmers where influenced by math background and principles