Problem with code.

Whats wrong with this code. While compiling it says " a function change is not allowed before'{' token and points to the void loop {

byte ledPin[] = {11, 12, 13};
int ledDelay(65);
int direction = (1);
int currentLED = 0;
unsigned long changeTime;

void setup()
{
  for(int x=0; x<3; x++)
  {
    pinMode(ledPin[x], OUTPUT);
    changeTime = millis();
}
  
  void loop()
 {
    if (millis()-changeTime > ledDelay)
    {
      changeLED();
      changeTime = millisec();
    }
 }
  void changeLED()
  {
    for(int x=o; x<3; x++)
    {
      digitalWrite(ledPin[x], LOW);
    }
    digitalWrite(ledPin[currentLED], LOW);
    currentLED += direction;
    if(currentLED = 3){direction = -1;}
    if(currentLED = 0}{direction = 1;}
}

Thanks in advance. :slight_smile:

your for loop needs a closing bracket ( in setup function )

also

if(currentLED = 3){direction = -1;}

no need for '{ }' on single statements ( no need to remove either, thought you might like to know )

and maybe you mean currentLED == 3, a single = will assign 3 to currentLED, which resolves to true... always

your for loop needs a closing bracket ( in setup function )

Alternatively, you could safely lose the opening brace { of the for loop :wink:

int ledDelay(65);
int direction = (1);
int currentLED = 0;

Better make up your mind how you are going to initialize variables. Using 3 methods looks rather indecisive.

for(int x=o; x<3; x++)

o != 0

and if(currentLED = 0}
is always false

Using Tools + Auto Format would have allowed you to see the missing curly brace AND made your code easier to read. I recommend that you become familiar with this tool, and use it often.

Thank you all, guys.My led chase effect is ready. And also thank you for the extra tips. And PaulS what do you mean by "Tools + Auto Format". I don't get you. :slight_smile:

Ufoguy:
Thank you all, guys.My led chase effect is ready. And also thank you for the extra tips. And PaulS what do you mean by "Tools + Auto Format". I don't get you. :slight_smile:

At the top of your IDE, there is a menu "Tools" that you can click and then select the Auto Format to fix your formatting.

Or Ctrl-T (Cmd-T for Mac).

What does the Tools + Autoformat button do? Does it automattically format the code and solve errors(at least some)? Excuse me for my ignorance. :blush:

Ufoguy:
What does the Tools + Autoformat button do? Does it automattically format the code and solve errors(at least some)? Excuse me for my ignorance. :blush:

That would be nice 8), but all it does is lay out your code in a consistent way that makes missing "{" and "}" more obvious.

PeterO

It doesn't solve any problems. It tries to indent the code correctly. It gets some things wrong, in my opinion, like array initialisations spread over two lines, but mostly does a good job. If open and close braces can't be matched, it just fails with a 'too many/not enough' error message. It can also fail, with the same error, even when there isn't a mismatch, which sometimes means there is some other issue, but I can't work out under what conditions it happens.