How do I include multiple files of my sketch

Suppose I've got a sketch with a mainfile named "test".
now I've created a new tab/file called "include"

how do I include "include" into "test"?

#include "include"
#include "include.c"
#include "include.cc"
#include "include.cpp"
#include "include.pde"

doesn't work

Save both files as a sketch. Then add the second sketch .pde file to your first sketch. Here is an example. It does nothing but verify the compiler finds the test function in the second sketch.

I saved this first sketch as filetest.

void setup()
{
  
}

void loop()
{
  test();  
}

I saved this sketch as test. Note no setup or loop functions.

void test()
{
  
}

Then select "Sketch - Add file". Find the test.pde file. Highlight and click OK.
Compile it.

Is that what you wanted to do?

yes that was what I wanted to do.

but there's another thing: can I only import the functions or can I also import "global" variables?

void setup() {  }

void loop() {
  test(); // this works
  testvar++; // error: 'testvar' was not declared in this scope
}
int testvar=0;

void test() {  }

Oops! I did not notice that. I declared my variables in the main sketch and that worked.

int testVar = 0;

void setup()
{
  
}

void loop()
{
  test();  
}
void test() { 
  testVar = 1;
}

ok, now everything works =)

to initialize the variables I'll just create some init* functions in the other files and call them out of setup.

thanks for the fast answers.

SurferTim:
Save both files as a sketch. Then add the second sketch .pde file to your first sketch. Here is an example. It does nothing but verify the compiler finds the test function in the second sketch.

IThen select "Sketch - Add file". Find the test.pde file. Highlight and click OK.
Compile it.

Along these same lines, it is possible to create header files that can be #included in arduino sketches ?

I typically put my structure definitions, global variables, in separate header files

Along these same lines, it is possible to create header files that can be #included in arduino sketches ?

Of course. You can add a tab in the IDE and assign it a name with a .h extension. Or, you can create the file using any editor, and use the same process outlined above to add the file to your sketch folder.

PaulS:

Along these same lines, it is possible to create header files that can be #included in arduino sketches ?

Of course. You can add a tab in the IDE and assign it a name with a .h extension. Or, you can create the file using any editor, and use the same process outlined above to add the file to your sketch folder.

When I tried saving a file with a .h on it, the Ide renamed it- and saved it to a subdirectory under the current project directory.
(this is not good- for the IDE to have more control than the user)

Having an example of a multi-file project with #included .h files would be great documentation to add to arduino.cc.
(If I only knew how to get in touch with the site maintainer(s), I would ask them directly )

Thanks

When I tried saving a file with a .h on it, the Ide renamed it

To what?

and saved it to a subdirectory under the current project directory.

Where else would you have wanted it to save it? If you are creating a reusable library, the process is somewhat different.

(this is not good- for the IDE to have more control than the user)

Sometimes, computers ARE smarter than the average bear.

(If I only knew how to get in touch with the site maintainer(s), I would ask them directly )

They regularly monitor the forum. Particularly the Website and Forum subforum and the Development forum.

PaulS:

When I tried saving a file with a .h on it, the Ide renamed it

PaulS:
To what?

I tried saving the header as structures.h. The IDE renamed it to Subdirectory\structures_d.pde
(I forgot what the name of the subdirectory was)

PaulS:
Where else would you have wanted it to save it? If you are creating a reusable library, the process is somewhat different.

In the directory where I saved it.
I'm not creating a reusable library, just organizing my code into multiple files.

Ideally, all files for a given project should be in the same directory, as in

main.c (pde)
common.c (pde)
structs.h
globals.h
file1.c (pde)
file2.c (pde)

There are occasions where some source files should be in separate directories, when compiling for different variations of a proejct..
\x86
\Arm

etc..

The IDE renamed it to Subdirectory\structures_d.pde

If you don't supply an extension, .pde is assumed, regardless of the actual contents of the file. I'm guess you didn't supply one, since the IDE does handle .h files correctly for me.

In the directory where I saved it.
I'm not creating a reusable library, just organizing my code into multiple files.

You can do this IF all the files are in the same directory. If you want to splatter them all over the place, the IDE is not for you.

Ideally, all files for a given project should be in the same directory, as in

That's not only ideal, it's required.

Using a .c extension is going to make life very difficult for you. Go with the flow and use .cpp. Trust me on this.

PaulS:

PaulS:
If you don't supply an extension, .pde is assumed, regardless of the actual contents of the file. I'm guess you didn't supply one, since the IDE does handle .h files correctly for me.
{/quote]
I gave the file an explicit .h extension.

PaulS:
You can do this IF all the files are in the same directory. If you want to splatter them all over the place, the IDE is not for you.

You're missing the point. I tried saving the header.h file in the directory the the main .pde file was. The IDE renamed it to header_h, and saved it to a subdirectory
under the project directory.

PaulS:
That's not only ideal, it's required.

Tell that to the IDE, apparently it doesn't know that :wink:

PaulS:
Using a .c extension is going to make life very difficult for you. Go with the flow and use .cpp. Trust me on this.

The main.c filename was just an example, to show that I want all files in the same directory.

My actual files have .pde for an extension. I didn't know I could rename them to .cpp.
I prefer .cpp myself, over PDE.

I wish the people who make Arduino & Processing would use different default extensions for each project.
PDE gets associated with the IDE that was installed last. If I want to double-click on a .PDE file, it launches the Processing IDE, not the Arduino IDE.

I wish the people who make Arduino & Processing would use different default extensions for each project.

In Arduino 1.0, the extension changes to .ino.

I gave the file an explicit .h extension.

The IDE renamed it to header_h, and saved it to a subdirectory
under the project directory.

The _ is used to replace invalid characters in file names. I'm wondering if you tried to use comma h (inadvertently, of course) instead of dot h.

My actual files have .pde for an extension. I didn't know I could rename them to .cpp.
I prefer .cpp myself, over PDE.

The main sketch file must have a .pde extension. Other files can use .cpp, .c. or .pde, depending on what is in the file.

PaulS:
The _ is used to replace invalid characters in file names. I'm wondering if you tried to use comma h (inadvertently, of course) instead of dot h.

PaulS:
The main sketch file must have a .pde extension. Other files can use .cpp, .c. or .pde, depending on what is in the file.

Good to know. I'll try saving .h again to see what happens.

Thanks!

[quote author=PaulS
The main sketch file must have a .pde extension. Other files can use .cpp, .c. or .pde, depending on what is in the file.
[/quote]

I've just tried doing a File Save as, with .pde extension

The IDE renamed .PDE to _pde, and saved that file in a subdirectory under the current project directory.
It also displayed this in the bottom window of the IDE

The sketch name had to be modified. Sketch names can only consist of ascii characters and numbers (but cannot start with a number).
They should also be less than 64 characters long.

This is the exact same thing that happened when I tried saving a header file as header.h

I'm guessing it didn't like the . in the .PDE extension

I've just tried doing a File Save as, with .pde extension

Saving what? The extension must be defined when the file is created. It can not be changed later.

The IDE renamed .PDE to _pde, and saved that file in a subdirectory under the current project directory.
It also displayed this in the bottom window of the IDE

Saved that file with what extension?

PaulS:
Saving what? The extension must be defined when the file is created. It can not be changed later.

cappy2112:
I've just tried doing a File Save as, with .pde extension

PaulS:
Saved that file with what extension?

The IDE renamed .PDE to _pde, and added .pde
I've tried saving the file as junk.pde

The IDE saved it as Junk_pde\junk_pde.pde.

(where Junk_pde is a NEW subdirectory made by the IDE.)

The IDE renamed .PDE to _pde, and added .pde
I've tried saving the file as junk.pde

The IDE saved it as Junk_pde\junk_pde.pde.

(where Junk_pde is a NEW subdirectory made by the IDE.)

The IDE expects a sketch of a given name, like junk.pde, to be located in a directory of the same name without the .pde extension, like junk. There can be other files in that directory, with .cpp, .h, and .pde extensions.

Open the IDE, which will create a new sketch with today's date as part of the name. Add a new tab. Give the file associated with that tab a name, like testing.h. Add another tab, with the name testing.cpp. Add a third tab, with the name functions.pde. Now, save the files, using File + save. Put the folder/sketch in C:\Temp.

Then, navigate to C:\Temp\sketch_<todays_date>. What is in that folder?

I just did this, and I have:
C:\Temp\sketch_oct07a containing
sketch_oct07a.pde
testing.h
testing.cpp
functions.pde