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?

William Roy
  • 515
  • 4
  • 10
  • 22

5 Answers5

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.

Majenko
  • 105,851
  • 5
  • 82
  • 139
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.

gilhad
  • 1,466
  • 2
  • 11
  • 20
1

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

  1. Switch to a real ide.
  2. Use an extern atteibute.
dannyf
  • 2,813
  • 11
  • 13
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.

qwr
  • 165
  • 6
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.

JRobert
  • 15,407
  • 3
  • 24
  • 51