Syntax question, int x(y)?

Hello there Arduino community! :smiley:

I recently got my hands on an Arduino starter kit from Earthshine Electronics and am currently using their free e-book to familiarize myself with everything. Being my first real experience in electronics (I have done some Java and a bit of C), it's been a great deal easier and more fun than I thought it'd be.

Currently I'm on project 5 of the e-book, the LED Chase Effect. Part of the code given for the project for the variable initializations is: "int ledDelay(65);"
How exactly does it differ from doing "int ledDelay = some number"?

Also why does it look like the int is acting as a function?

Thanks for your help!

It's doing exactly the same thing. Personally I don't use that syntax. Even basic types such as int can had a constructor.

A constructor is a function with the same name as the type. In this case it is initialising the integer with the value in brackets. However the equals notation is much clearer.

Constructors are useful for classes / structs but do not offer much for basic types other than confusion to those unfamiliar with the syntax.

Iain

Sorry, another question. If the notation is practically the same as the equals notation, why is it that changing the value in the parentheses of ledDelay(x), change the speed at which the LEDs move? There doesn't seem to be any sort of explicit delay so far as I can tell.

This is the source code for the program by the way:

// Project 5 - LED Chase Effect
// Create array for LED pins
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay(65); // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
  // set all pins to output
  for (int x=0; x<10; x++) {
    pinMode(ledPin[x], OUTPUT); }
    changeTime = millis();
}
void loop() {
  // if it has been ledDelay ms since last change
  if ((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
  }
}
void changeLED() {
  // turn off all LED's
  for (int x=0; x<10; x++) {
    digitalWrite(ledPin[x], LOW);
  }
  // turn on the current LED
  digitalWrite(ledPin[currentLED], HIGH);
  // increment by the direction value
  currentLED += direction;
  // change direction if we reach the end
  if (currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}

Thank you again for the help.

You are correct that there is no explicit use of the delay() function. It is never good practice to introduce delays. So instead the program is looking to see if a certain amount of time has elapsed, ledDelay. Then the program enters that condition and changes the state of the LED's.

Study and understand what this code snippet is doing, this will answer your question.

if ((millis() - changeTime) > ledDelay) {
    changeLED();
    changeTime = millis();
  }

Ah I see, I kept misinterpreting the for loop in changeLED() as turning on the LED, not turning all of them off. :0