The_N00b:
What could i use instead of goto? cause i kinda need that command to realize what i have in mind
Functions are called (as mentioned above). Have a look at all the standard functions that you use like digitalWrite, Serial.print or Serial.println. You don't use goto digitalWrite(somePin, HIGH); but you use digitalWrite(somePin, HIGH).
On your question why you need the round brackets; that is just in the language.
And to your original question how to call functions that are defined in a different tab (and hence a different file), you simply call them.
If you create a new file / tab and give it the extension ino, the IDE will take care of anything that is needed.
tab 1 (yourSketch.ino)
void setup()
{
Serial.begin(115200);
calcAndPrint(5, 7);
}
void loop()
{
// put your main code here, to run repeatedly:
}
In setup you call a function 'calcAndPrint' that takes two parameters. That function is defined in another file as shown below.
tab 2 (yourFunctions.ino)
void calcAndPrint(int i, int j)
{
Serial.println(i * j);
}
If you compile this, there is no issue. The IDE just combines all ino files in one massive ino file. It also does some other clever things like analyzing and automatically adding some of the includes before the resulting file is eventually 'passed' to the compiler.
Now e.g. libraries usually have cpp and h files. You can also do this. Rename yourFunctions.ino to yourFunctions.cpp (can be done in the IDE).
When you compile, the compiler will complain that it does not know about the function 'calcAndPrint' when it compiles; part of the error message
'calcAndPrint' was not declared in this scope
You have to tell it that it exists and you can do that using a function prototype in the file where you want to use that function (in this case yourSkect.ino).
// function prototype
void calcAndPrint(int i, int j);
void setup()
{
Serial.begin(115200);
calcAndPrint(5, 7);
}
void loop()
{
// put your main code here, to run repeatedly:
}
When you compile this, there is an improvement (the above error is gone) but you have another one. In yourFunctions.cpp the compiler does not know about Serial.
Part of the error message
'Serial' was not declared in this scope
To solve that, you need to include Arduino.h in that file (the same as when you want to use e.g. the LiquidCrystal library in the main sketch).
So 'yourFunctions.cpp' now becomes
#include <arduino.h>
void calcAndPrint(int i, int j)
{
Serial.println(i * j);
}
Now imagine what needs to happen in your main sketch if you have e.g. 20 functions in yourFunctions.cpp. You need to add 20 prototypes in yourSketch.ino. OK, doable. But now you also have another cpp file where the compiler also needs to know about (some of) those functions in yourFunctions.cpp and you need to add prototypes there as well.
That is why include files (.h) exist. Add a new tab called yourFunctions.h.
tab 3 (yourFunctions.h)
void calcAndPrint(int i, int j);
// other prototypes here
...
...
...
And just like you include a library, you can now include yourFunctions.h
#include "yourFunctions.h"
void setup()
{
Serial.begin(115200);
calcAndPrint(5, 7);
}
void loop()
{
// put your main code here, to run repeatedly:
}
Note that there is one difference compared to the including of libraries; this uses double quotes instead of <>. These double quotes tell the compiler to first look for the specified include file in the sketch directory. If it can't find it there, it will look in the standard directories.
Although there is a little more to it, this should be sufficient for now.
Now the question that you can raise is "why go through the hassle of .cpp and .h files if I can use multiple .ino files".
And the answer is that .ino files get special processing by the IDE to basically convert them to valid C/C++ files; the compiler only 'understands' (see the earlier errors that you got while following this post) files that contain correct C/C++.
The IDE is designed to make life easy for beginners; as a result it hides a lot of mistakes that people make. As an example
void setup()
{
doSomething();
}
void loop()
{
}
void doSomething()
{
}
The compiler will bitterly complain because it does not know about the function doSomething() by the time it encounters the call to it in setup(); the IDE modifies the above so the compilere will be happy.
You should either add a prototype before the setup() function or move the complete doSomething() function to before setup() if you were using the compiler directly.
Hope this helps.