Why cannot just use a variable?

I suppose to write a program that four leds can shine and shift from left to right.

The book I bought to learn Arduino use two variables i,j to complete the function.

Could you help me find out why I can't just use a variable in the statement?

Thanks a lot.

int j;
const int led[]={10,11,12,13};

void setup() 
{
for(j=0;j<4;j++)
     pinMode(led[j],OUTPUT);
}

void loop() 
{
for(j=0;j<4;j++)  
    {digitalWrite(led[j],LOW);}
digitalWrite(led[j],HIGH);
delay(1000);
j++;
if(j==4)
    j=0;
}

What statment? if you mean
for(j=0;j<4;j++)

you can use

for(int j=0;j<4;j++)

Hope this helps

Thanks a lot, it helps.

or byte j, since 8 bits are more than enough to hold 0 to 4.

I have declared j as an integer, why do I have to declare it again in the following statement?

You don’t need to. I make All my variables global, and never declare any inside of loop code.
Your code is little mixed up it seems to me. Reorganized a little and added some << notes

void loop() 
{
for(j=0;j<4;j++)  { // this loop sets all 4 outputs low
digitalWrite(led[j],LOW);
}
digitalWrite(led[j],HIGH); // then one output is written high - maybe j = 3 here? maybe 4?
delay(1000);                     // delay for a second
j++;                  // these 3 lines then do some pointless manipulation of j
if(j==4)            // because j is set to 0 at the start of the for loop anyway.
    j=0;
}

CrossRoads:
You don’t need to. I make All my variables global, and never declare any inside of loop code.
Your code is little mixed up it seems to me. Reorganized a little and added some << notes

digitalWrite(led[j],HIGH); // then one output is written high - maybe j = 3 here? maybe 4?

delay(1000);                    // delay for a second
j++;                  // these 3 lines then do some pointless manipulation of j
if(j==4)            // because j is set to 0 at the start of the for loop anyway.
    j=0;
}

What would happen if j was 4 there? I think it’d have to be 4, in order for the test to be false. So after the for loop, it will would try to read beyond the end of the array…

My impression is that the code the OP wrote is entirely different from what he intended.

My impression is that the code the OP wrote is entirely different from what he intended.

That my reading of the code - the fourth led will be on all the time (to the human eye).

digitalWrite(led[j],HIGH);
delay(1000);
j++;
if(j==4)
    j=0;

The j's above should be i's and i should be global.

Mark

The j's above should be i's

I use x myself.

I guess a Serial.print(j); there would tell us.

void loop()
{
for(j=0;j<4;j++) 
    {digitalWrite(led[j],LOW);}
digitalWrite(led[j],HIGH);
delay(1000);
j++;
if(j==4)
    j=0;
}

Your code is very confusing. How about:

void loop()
{
for(j=0;j<4;j++) 
    {
    digitalWrite(led[j],LOW);
    delay(1000);
    digitalWrite(led[j],HIGH);
    delay(1000);
    }
}

You only need one variable: j

You need a delay after both LOW and HIGH or you won’t see any blinking.

holmes4:
The j's above should be i's and i should be global.

I don't like using i or j for loop variables because they can be difficult to read in amongst all the . n, m, b. a etc are much easier to read IMHO.

...R

holmes4:
The j's above should be i's and i should be global.

I don't really agree that loop control variables, like i and j should be global. Far too easily corrupted.

I use i or j - or if there's an obvious word that describes what the loop is counting, I use the first letter of that.

I can't see anything good coming from making them global. When do you need to know the value of that variable outside the loop when you don't already have another way of knowing what the value is? Maybe if you're expecting an interrupt while the for loop is running, and that ISR needs to know which step of the loop it's in for some reason? Are there any other situations where making it global would be of any use?

I wager that it's global in this case because he meant for that second digitalWrite() to be in the loop, but put the brace in the wrong place, and got compile error "variable was not declared in this scope", and declared it global to get it to compile.