Go Down

Topic: Problem with code. (Read 3015 times) previous topic - next topic

Ufoguy

Whats wrong with this code. While compiling it says " a function change is not allowed before'{' token and points to the void loop {
Code: [Select]
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. :)
If you want to meet a beautiful nurse you must be patient.

pYro_65

#1
May 31, 2012, 02:17 pm Last Edit: May 31, 2012, 02:19 pm by pYro_65 Reason: 1
your for loop needs a closing bracket ( in setup function )

also
Code: [Select]
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
Forum Mod anyone?
https://arduino.land/Moduino/

AWOL

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

Alternatively, you could safely lose the opening brace { of the for loop   ;)

nickgammon

Code: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

AWOL

Code: [Select]
for(int x=o; x<3; x++)
o != 0

and
Code: [Select]
if(currentLED = 0}
is always false

PaulS

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.
The art of getting good answers lies in asking good questions.

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. :)
If you want to meet a beautiful nurse you must be patient.

Arrch


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. :)

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.

dxw00d

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

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. :smiley-red:
If you want to meet a beautiful nurse you must be patient.

PeterO


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


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

dxw00d

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.

Go Up