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++)

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.


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;

    state = !state;
    //First 8 LEDs

    //Next 8 LEDs

I am using the Arduino IDE.

What is going wrong ? Why ?

  • \$\begingroup\$ Are you able to post the entire code including the setup() and void() methods as well as any variable declarations at the top? \$\endgroup\$ – Marko Feb 14 '13 at 4:25
  • \$\begingroup\$ @Marko: Did you mean setup() and loop()? \$\endgroup\$ – Dave Tweed Feb 14 '13 at 4:32
  • 1
    \$\begingroup\$ @AnindoGhosh Now I definitely look mentally challenged laughing at my laptop. Thanks for that. \$\endgroup\$ – Marko Feb 14 '13 at 4:39
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Marko Feb 14 '13 at 6:48
  • 1
    \$\begingroup\$ But i means a lot to me ;) No pun intended. \$\endgroup\$ – Marko Feb 14 '13 at 6:51

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.

| improve this answer | |
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Dave Tweed Feb 14 '13 at 4:33
  • \$\begingroup\$ Agreed, I have seen Arduino examples where i is declared with other variables at the top of the file. Generally bad practice. \$\endgroup\$ – Marko Feb 14 '13 at 4:36
  • \$\begingroup\$ @DaveTweed I saw your update and have updated it to letter k. \$\endgroup\$ – Marko Feb 14 '13 at 4:37
  • \$\begingroup\$ Yes, that was the issue. Making it a local solved the problem. \$\endgroup\$ – asheeshr Feb 14 '13 at 5:42
  • \$\begingroup\$ @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. \$\endgroup\$ – Marko Feb 14 '13 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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