I am confused.
To my way of thinking...
The function flash() should come before loop(), but it doesn't matter where I put flash() as long as it is after the definitions ledPin and delayPeriod since it calls those definitions.
Is this sequenced by the compiler? Why doesn't it matter? I would think that loop() would complete and jump back to it's beginning to start over without going any further? apparently this is wrong.
Yes I know this is just the way it is, but WHY? I kinda like that it doesn't matter, but I tend to think linearly.
const int ledPin=13;
const int delayPeriod=250;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
for (int i=0;i<20;i++)
{
flash();
}
delay(3000);
}
void flash()
{
digitalWrite(ledPin,HIGH);
delay(delayPeriod);
digitalWrite(ledPin,LOW);
delay(delayPeriod);
}
Functions must be defined before they are referenced. That can involve implementing the function before it is referenced, or it can involve defining a function prototype before the function is referenced, and implementing the function after it is referenced.
The IDE creates function prototype for you, at the top of the sketch.
Of course the paths in the #line directives will be different for you. Those #line directives are necessary so that the line numbers and file names in the output shown in the black console window at the bottom of the Arduino IDE window will still match your sketch, rather than the preprocessed file which is what is actually being compiled.
This file is stored in a temporary build folder after compilation. You can find that folder by turning on verbose output during compilation (File > Properties > Show verbose output during: compilation (check)) and then examining the contents of the black console window after compilation (you need to scroll up to see it all). On my computer the temporary build folder path is something like C:\Users\per\AppData\Local\Temp\arduino_build_658609 (the number at the end is random). The preprocessed sketch is in the sketch subfolder of the temporary build folder. You'll note the extension of the sketch file has been changed to .cpp. That is because after any missing function prototypes have been automatically added your sketch is now valid C++ and will be compiled as such.
It can sometimes be useful to be able to look at the preprocessed sketch since in rare cases the Arduino IDE's preprocessing gets things wrong, which can lead to mysterious errors.
As @PaulS Said the Arduino IDE creates function prototypes for you. So you can put your functions any where you want(except in another function). If they didn't you would have to write your own function prototypes or put all your functions before they are called.
EDIT: Also the Arduino does not run a program from top to bottom. The IDE calls setup() first and then loop() which runs over and over forever. All other functions (the ones you create) have to be "called" from within setup or loop.
but I tend to think linearly
A common problem and a habit you need to break. The "BLINK" program that is usually a beginners first sketch is written linearly:
Turn on LED;
Wait 1 second;
Turn off LED;
Wait 1 second;
Loop around and do it agian.
Notice that it takes 2 whole seconds for loop to go around once. and if you try to do anything else things will get messed up.
This is not a good programming technique because during the time it is waiting(delay) the arduino could be doing literally millions of other things.
It's better to take a reactionary approach like in the " Blink without delay" sketch:
Loop;
Is it time yet (no);
Do other stuff;
Loop;
Is it time yet (no);
Do other stuff;
Loop;
Is it time yet (yes);
switch the LED;
Do other stuff;
With this sketch loop will go around THOUSANDS of times per second. Lots of time to do other stuff