Behind The Scence With Functions?

I am trying to understand in the most simplest form of what is inside or made up of predefine functions?

C++ Tutorial does not explain this nor did I find anythig on this site.

Example:

setup() function

void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);

Where is the code for setup() or pinMode(var1,va2)???

This is never described in detail anywhere?

Any help is much appreciated. Thanks.

Where is the code for setup()

You write it.

or pinMode()

All of the source code is available to you. The only trick is finding it. That depends on your board, for one thing. Code for ARM boards is different from that for AVR boards.

On my system, the IDE is installed in C:\Users\PaulS\Documents\arduino-, and pinMode() is declared in hardware\arduino\avr\cores\arduino\Arduino.h (relative to the install path) and implemented in wiring_digital.c in the same directory.

OUTPUT, HIGH, LOW, these a predefined words some where and have hidden values. I don't create them.

Just like if I remove or change the setup() function name in that simple program it fails.

I guess I might be explaining it wrong. How about this:

Header files, we never see being included in here.

But if we did include a header file example <stdio.h>

In that header file is pre-written code that describes every thing that is connected to one another.

Like excepting parameters for this pinMode() function. Who knows how or what those two variables are created.

Is there anywhere to see the code for them? Hope this explains it in a better way.

You have the code - see reply #1

I wondered about the same stuffwhen I started working with Arduino after writing C for other platforms in other environments for years. Turns out that the Arduino IDE (to the extent that it is an IDE) tries to be “helpful” by doing a lot of #includes (and other stuff) behind the scenes. Others on the board can explain it in much more detail than I.

BTW, I still don’t like it since I really prefer to know where things are coming from. But that’s the way it is.

BTW, I still don't like it since I really prefer to know where things are coming from.

You could use a professional development system like Atmel Studio instead. That way you have complete control.

@whalers1988,
Yep, a lot goes on in the background.
If you start with main.cpp, you will see the function main(), which calls a couple things that only occur once to set up timers and interrupts and such, then setup() once, and then loop() in a never-ending for-loop:

int main(void)

{

init();

 initVariant();

#if defined(USBCON)

 USBDevice.attach();

#endif

 setup();

 for (;;) {

 loop();

 if (serialEventRun) serialEventRun();

 }

 return 0;

}

So your sketch will contain a few section that will get compiled (pin assignments, global variables, library usages, and setup() and loop() and any other functions that you may use.

As noted, you have all the files called out, you just need to find them as .cpp or .h files, it can be confusing with files calling out other files, like a wild goose chase at times.
On my PC I have them at C:/Arduino_version/hardware/arduino/avr/cores/arduino
where everything after Arduino_version (such as 1.6.5-r5) is the stuff that gets set up when the Arduino files get unzipped.

Oh thank you so much. I hope I can get the hang of this. I am really interested and I like to know everything that is happening from top to bottom. Thanks everyone for your help. :slight_smile: :slight_smile:

whalers1988:
Where is the code for setup() or pinMode(var1,va2)???

setup() is just the name of the function and you provide all of its code.

The code for pinMode() and the other Arduino-specific functions is included in the files that come with the Arduino IDE. On my PC the code form pinMode() is in the file ..../arduino-1.6.3/hardware/arduino/avr/cores/arduino/wiring_digital.c

...R

I don't really have anything to add to what other have said, except so make a shameless plug...

This is where a proper IDE (like, ehem, Sloeber) comes into play. Your question would have been answered with one Ctrl-Click or F3 press. :smiley:

Cosme_Fulanito:
I don't really have anything to add to what other have said, except so make a shameless plug...

This is where a proper IDE (like, ehem, Sloeber) comes into play. Your question would have been answered with one Ctrl-Click or F3 press. :smiley:

I would LOVE to move to a more capable IDE. Started looking through the Sloeber install advice. Ran across this:

spaces Windows users keep in mind that spaces cause problems. So do not install the arduino IDE eclipse or Sloeber in "program files". Don't create the workspace in "my documents".”

Come on, REALLY? It’s 2017.

I might give it a try anyway, but REALLY?