Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I am programming effects for an LED cube. The cube and the independent functions are working correctly. The problem occurs when I call turnon_layer function in a loop. The loop counter is not incremented.

for(i=0; i<4; i++)
{
  Serial.println(i);
  turnon_layer(1000,i);
}

The Serial monitor shows that i has a constant value of 0 throughout.

When I call the functions with constants, then the output is as expected.

turnon_layer(1000,0);
turnon_layer(1000,1);
turnon_layer(1000,2);
turnon_layer(1000,3);

My turnon_layer function is :

void turnon_layer(long time, int layer)
{
  boolean state = HIGH;
  long wait, start = millis();
  for(i=0; i<4; i++)
  {
      if(i!=layer) digitalWrite(levelPin[i], !LOW);
      else digitalWrite(levelPin[i], !HIGH);
  }

  if(time/2<10)  wait = time/2;
  else  wait = 10;

  while(millis()-start<time)
  {
    state = !state;
    //Serial.println(state);
    //First 8 LEDs
    for(i=0;i<2;i++)
    for(j=0;j<4;j++)
    digitalWrite(ledPin[i][j],state);

    //Next 8 LEDs
    for(i=2;i<4;i++)
    for(j=0;j<4;j++)
    digitalWrite(ledPin[i][j],!state);
    delay(wait);
  }
}

I am using the Arduino IDE.

What is going wrong ? Why ?

share|improve this question
Are you able to post the entire code including the setup() and void() methods as well as any variable declarations at the top? – Marko Feb 14 at 4:25
@Marko: Did you mean setup() and loop()? – Dave Tweed Feb 14 at 4:32
1  
@AnindoGhosh Now I definitely look mentally challenged laughing at my laptop. Thanks for that. – Marko Feb 14 at 4:39
1  
@Swanand do you have anything concrete to back that statement? A quick scan at the Wikipedia For Loop Page reveals that just about every example uses i. – Marko Feb 14 at 6:48
1  
But i means a lot to me ;) No pun intended. – Marko Feb 14 at 6:51
show 6 more comments

1 Answer

up vote 4 down vote accepted

Is the variable i declared as a global variable? i.e. at the top of the file outside all methods?

It may be getting overwritten by the loop inside your turnon_layer() method.

To test, just change the variable to letter k inside just one of your loops.

share|improve this answer
1  
The experiment you're proposing will fail, because j is also used in the called function. In general, loop index variables should pretty much always be declared local to each function in which they're used. – Dave Tweed Feb 14 at 4:33
Agreed, I have seen Arduino examples where i is declared with other variables at the top of the file. Generally bad practice. – Marko Feb 14 at 4:36
@DaveTweed I saw your update and have updated it to letter k. – Marko Feb 14 at 4:37
Yes, that was the issue. Making it a local solved the problem. – AshRj Feb 14 at 5:42
@DaveTweed I just had another look and I believe that unless j was declared at the top it wouldn't interfere because when specified in the loop declaration it becomes a local variable to that loop body. – Marko Feb 14 at 20:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.