Why are there Parenthesis after loop in Sketch? I am just starting and this bugs the shit out of me. If anyone can give me an answer it would help me alot since I sometimes get hung up on little things like that when learning something new.
A short-but-not-very-satisfying answer:
Treat it as an idiom. You don't have to like it, but you have to live with it as long as you are doing things the "Arduino Way."
A longer-but-actually-makes-you-learn-a-little-or-maybe-makes-you-sorry-you-asked answer:
Every Arduino sketch gets embedded in a C++ program file. The syntax shown means that the identifier loop is a function name. The function has a void return data type. The function has no parameters.
Same for setup.
Regards,
Dave
Footnote:
Consider the following sketch:
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hi");
delay(1000);
}
Your stuff gets put into a C++ program file that looks like this:
#include "WProgram.h"
void setup();
void loop();
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hi");
delay(1000);
}
That file is compiled and linked with the necessary Arduino core files to allow the complete program to be put together in a way that the Arduino hardware can execute it.
In particular, here is the main C++ program file that will be linked to your stuff. The main() function calls some Arduino-provided initialization function, then it calls your setup function once and then repeatedly calls your loop function time after time after time...
#include <WProgram.h>
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
The whole point of the Arduino developers is to let people start doing interesting things without having to learn a bunch of stuff like this at the beginning. It can be kind of frustrating for people who know (or want to know) a little C++ but can't see how the heck anything works with the "Arduino Way."
Thank you. This is why I love the internets.
Arduino-provided initialization function
Is that init function something in the Arduino core library that one could look at for educational purposes?
Lefty
Is that init function something in the Arduino core library
Yes. It's in "wiring.c". It starts the timers running and ensures the A/D converter is ready for business.
that one could look at for educational purposes?
Absolutely. But bring your datasheet.