The question is: Where do I define the name of the watering pump?
If I create it in main.cpp it doesn’t work either. I have all the .h files inside the include folder and the .cpp files inside src. In main.cpp I’m #includding everything.
You have to create this variable in some scope somehow. You probably want to create this is a global variable, as opposed to a local function variable. I see you already have
So in here should be some content like
#ifndef _GLOBAL_VARS_H
#define _GLOBAL_VARS_H
#include "Pumps.h"
//declare existence of wateringPump variable to the world
extern Pumps wateringPump;
#endif
And then in some .cpp file, say e.g. src\globalVars.cpp, or src\main.cpp, …
#include "Pumps.h"
#include "globalVars.h"
// example pin
#define WATERPUMP_PIN 10
// **define** global variable here
Pumps wateringPump(WATERPUMP_PIN);
I ended up declaring them on the file that is going to use them. Now I’m trying to use your suggestion to use the BluetoothSerial Library across my files as if it was the standard Serial class.
You don’t need to be double-declaring it as extern in that file too. By including BluetoothSerial.h you already have the extern declaration. That’s the only place where it needs to be.
Is that in the global scope of the file, just at the top, outside of every function? You cannot call into objects like that in the global scope. Only within functions. Usually people create an init() function that takes care of initialization like that.
Also, in which CPP file did you write the necessary
What I want to do is to use SerialBT instead of Serial.
If I initialize the object BluetoothSerial SerialBT anywhere on the code, it only works on main.cpp . I want to use it across all my other .cpp files just like the standard Serial. The system will not have a computer connected to it so everything has to be done via bluetooth.
Let me give you an example:
In main.cpp I call the interface so I can configure some parameters as well as calibrating some pumps. I have a file called CLI.cpp where I have the menu function that allows the user to select which parameter to edit, which sensor to calibrate, as well as the pumps. Pumps.cpp is where I have a method for the object called calibrate() that I want call from the CLI, this method walks the user step by step to calibrate the pump.
When I try to follow your instructions, the compiler doesn’t seem to find the objects. Even If I initialize them before setup(), like a global object, which is what I want. Neither the pumps objects, nor the SerialBT object.
My project looks like this right now.
main.cpp:
#include "BluetoothSerial.h"
void setup(){
SerialBT.begin("Name"); //NO PROBLEM HERE
}
globalVar.h:
#include "BluetoothSerial.h"
extern BluetoothSerial SerialBT; //NO PROBLEM HERE
globalVar.cpp
extern BluetoothSerial SerialBT; //NO PROBLEM HERE
CLI.cpp
#include "globalVar.h"
#include "BluetoothSerial.h"
SerialBT.available(); // This declaration has no storage class or type specifier.
Thank you so much. It does work, I was trying to write SerialBT.available() outside of a function jsut to test, I guess it doesn’t work that way . When I saw your code I realized that.