2

I have 2 .ino files (2 tabs) for one Arduino project/sketch: MySketch_File1.ino and MySketch_File2.ino.

I declared in MySketch_File2 variables and functions which I want to use in MySketch_File1, but I get this error massage:

error: 'a' was not declared in this scope

//MySketch_File1.ino
void setup(){
Serial.begin(9600);
Serial.println(String(a));
}

void loop(){

}

Second file:

//MySketch_File2.ino
int a;

How can I declare variables and functions one time in one .ino file and use it across multiple .ino files?

3
  • Do you mean multiple .INO files within the same sketch, or each in a completely separate sketch?
    – Majenko
    Commented May 23, 2017 at 9:30
  • Within the same sketch, see edit. Commented May 23, 2017 at 9:53
  • It still raise the question - WHY? (why you want two sketch files and why share variables between them) as maybe you are trying to emulate something, which is already present and in much better and more usable form.
    – gilhad
    Commented May 23, 2017 at 10:19

5 Answers 5

2

When you compile multiple files in the Arduno IDE the files first get concatenated together.

It happens in alphabetical order, except the main INO file (the one named the same as the folder it's in) is placed first.

In C the order in which things appear in the file matters. Basically you have to make sure that things (like variables, functions, etc) are defined before they are used.

This gets more complex when you have a hidden thing like file concatenation messing things up.

The Arduino IDE tries to hide some of it from you too by making function prototypes, so it doesn't matter what order you define functions in - however that "helpfulness" means you don't learn about the proper structure of a C program.

So what can you do? Well, simply make sure that all your global variables are defined in the main file, unless you know they are only going to be used within the file they are defined in.

5
  • What happens if you try to do what a C programmer should do? I mean, in one of the source (e.g. .ino) files you define the variable int a;, then in the other you declare it again with extern int a;. Will this work if 1) the variable is defined in the main ino and 2) if it is defined somewhere else but is declared in the main? I do not have the IDE installed here to test it myself ;)
    – frarugi87
    Commented May 23, 2017 at 10:10
  • Not sure. It may complain about multiple definitions of the same symbol, but may not. I'll test it.
    – Majenko
    Commented May 23, 2017 at 10:11
  • It compiles, so yes you can do that, but why exactly would you, when you are then effectively defining it in both files anyway? Just define it in the first. If you want to make something portable between sketches then make it a library.
    – Majenko
    Commented May 23, 2017 at 10:14
  • I think that the extern should be "ignored" (in fact you usually use the extern in the header file which is then included also in the c file defining it)
    – frarugi87
    Commented May 23, 2017 at 10:14
  • Yes, I agree that the global variables should be put in the first file so that everyone can access it. Or make a class... This was just the most classical solution (C/C++ compilers do not append source files)
    – frarugi87
    Commented May 23, 2017 at 10:16
1

In Arduino the sketch.ino is the main (and only) program, which runs on the processor.

If you upload another sketch to HW, then the previous one is overwritten and does not longer exist on the HW. So there is no way, they can share variables, as they never are run at the same time. (You can store some values to EEPROM or to external devices, but it is other thing).

What you probabely want to do is use the second file as a library, which is collection of variables and functions, that more then one program can use. Anyway what is used is the text of the library, not the values assigned to variables in one sketch.ino - so other sketch.ino using the same library would have the functions and variables of the same name accessible, but as it is compiled to othe program, the values assigned in one sketch would not be visible in other sketch.

Can you specify, why you want to use variables and functions accross multiple .ino files?

If it is just to not have type it again and again and fix found bugs in each aned every copy separately, then you are looking for libraries.

If you want to share also values (like read and compute value in one .ino then upload another .ino and use the result) you are looking for persistent storages (EEPROM on the chip, SD card via some reader or something like that.

If you want have more Arduinos running at the same time and sharing some values, you are looking for networks, like I2C, Serial communication, Ethernet and such.

If you want run mor programs on one arduino at the same time, you are looking for multitasking where each program is interrupted repeatedly and other one is continued, fast enought to user not notice, or for creatinig more functions and call them from loop() one after one to eg. read sensors, then light LEDs then run motors. State automats can make your way here easier.

5
  • Now that the OP clarified the question, this is not the correct answer. He has two "tabs" open, meaning he has two source files for the same program
    – frarugi87
    Commented May 23, 2017 at 10:07
  • Ok, this shifted it somewhere else, but still I think it may be relevant to his intent - why he wants to separate the two files - using secondary files to be merged together by some IDE hack while he wants emulate how library do function is not the best approach, if he can use libraries as well
    – gilhad
    Commented May 23, 2017 at 10:15
  • Of course.. Even if I usually tend to split (bigger) programs in smaller chunks (for instance, one file for the main loop, one for one of the peripherals, one for the interface, ...)
    – frarugi87
    Commented May 23, 2017 at 10:17
  • Yes, I think, that if I cannot see all relevant code on one (max two) screens, then something is probably wrong. The question is, what is wrong - and so the answers differs. If there are repeated parts, the answer is probably function. If there are lot of functions for one periferial/protocol, the answer is library for this. If the problem is just complicated, so if have a lot of code and functions, there is folding each function and then folding each group of folded functions. So my usual program includes lot of libraries and all functions are folded and main loop/ifs too.
    – gilhad
    Commented May 23, 2017 at 10:46
  • 1
    And anything longer then few lines means better editor than Arduino IDE. (I use VIM for example.)
    – gilhad
    Commented May 23, 2017 at 10:49
1

It is simplest to put only one .ino file in a directory with the filename. Why does an `.ino` file have to be in a folder of the same name?

Option 1 (cleanest option): Create a user library.

Option 2 (workaround): Relative paths for #include aren't supported but you can use a symlink to make it appear that there is the same header file in every directory:

sketch1/
  sketch1.ino
  mylib.h
sketch2/
  sketch2.ino
  mylib.h (this is a symlink to sketch1/mylib.h) 

Option 3: Avoid Arduino IDE entirely and build using avr-gcc. Example: http://thinkingonthinking.com/an-arduino-sketch-from-scratch/ and Will a .ino Arduino Sketch compile directly on GCC-AVR? This is much more complicated, but supports bigger projects using Makefiles.

0

Arduino ide has terrible project mgmt capabilities. So two solutions.

  1. Switch to a real ide.
  2. Use an extern atteibute.
0

You can make this easier by naming only the main sketch file '.ino', and naming all other code files as '.cpp'. The compiler has to know about 'a' before you use it, so declare 'a' in the .ino file as `extern int a;' before its first use, to tell it what 'a' is. Not having that is the reason for the error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.