[SOLVED] Simple LED Blinking Sketch | Question

I don't have a background in C++ so I am still getting my bearings with the language syntax so please forgive the simplicity of my question.

Here is a simple sketch (it does not use Pin11 or Pin10 at this point) that is lightly modified from an LED tutorial (public domain):

--

int Pin12 = 12;
int Pin11 = 11;
int Pin10 = 10;

void setup()
{
pinMode(Pin12, OUTPUT);
pinMode(Pin11, OUTPUT);
pinMode(Pin10, OUTPUT);
}

// Note: Pin11 and Pin10 aren't used in this early version of the sketch

void flash(int duration)
{
digitalWrite(Pin12, HIGH);
delay(duration);
digitalWrite(Pin12, LOW);
delay(duration);
}

void loop() // run once, when the sketch starts

{
flash(200); flash(200); flash(200); // S
delay(300); // delay to seperate flashes
flash(500); flash(500); flash(500); // O
delay(300); // delay to seperate flashes
flash(200); flash(200); flash(200); // S
delay(2000); // delay to reset SOS flash sequence
}

// END CODE

--

The code works just fine - but my question has to do with the term "duration" in the void flash(int duration) section - which I have highlighted in red.

The term "duration" in the delay(duration) does not seem to ever be defined. But if I try to change the term "duration" to something else (like "dur") I get an error in the compiler. Is "duration" a pre-determined command? If so, what does it do? I'm just not following the syntax of this sketch very well and could use some help parsing it out.

Thanks.

phear_sc:

void flash(int duration) 

{
 digitalWrite(Pin12, HIGH);
 delay(duration);
 digitalWrite(Pin12, LOW);
 delay(duration);
  }

The function call includes a declaration of an integer called "duration." Anything that calls flash() much include an integer.

later in your code you use (note, I've put statement on its own line, as you should too):

phear_sc:

  flash(200); 

flash(200);
  flash(200);

So you declared the function flash() with a variable called duration. Then in your loop() you call flash with a value of 200. This means inside of the function flash, the variable duration has a the value of 200.

BTW, your comments are wrong here:

phear_sc:

void loop()   // run once, when the sketch starts

First of all - thank you for responding.

Second, and please do forgive me, I'm still not following. I guess I still don't see where duration got defined in the flash statement (I realize this must look moronic to an experienced user).

We have the following code:

void flash(int duration)
{
digitalWrite(Pin12, HIGH);
delay(duration);
digitalWrite(Pin12, LOW);
delay(duration);

How this "reads" to me is something like this:

digitalWrite(Pin12, HIGH); -- Power on Pin 12
delay(duration); delay for whatever number "duration" is defined as
digitalWrite(Pin12, LOW); -- Power off Pin 12
delay(duration); -- delay for whatever number "duration" is defined as

The way my mind is processing this code I would have to define "duration" at some point. I might say, for instance, int Duration = 50

Then, the delay in the above code would be 50 as it references back to my definition of Duration (i.e. 50) in the previous sentence.

Flash seems to be defined as the write/delay/write/delay code and I'm still just not seeing how adding a value to "flash" is populating "duration". Clearly I am not understanding Arduino's syntax correctly.

I guess I still don't see where duration got defined in the flash statement

void flash(int duration)
The highlighted bit is where you defined a variable. It gets its value when the function is called:
flash(200);
The highlighted bit becomes the value for duration.

I get it now.

So lets say I wanted the delay to be fixed at 50 then I would have just written:

void flash
{
digitalWrite(Pin12, HIGH);
delay(50);
digitalWrite(Pin12, LOW);
delay(50);
}

And then I could have just called "Flash()"

Is that right?

Is that right?

You still need the parentheses in the function declaration, just like setup() and loop(). Otherwise, yes, that would use a constant, instead of a variable, for the on/off time.

I really appreciate both of you taking the time to answer my question so quickly. It can be so frustrating when you know the answer is something simple but you can't figure it out for want of someone more experienced just pointing at the obvious thing you're missing.

REALLY appreciate the help.