Problem using objects in functions


I’m having problems compiling functions that use objects that I created. I will try to explain the best that I can.

I’m working on a Hydroponics proyect. I have several pumps that I’m creating as objects. Then I want to write functions that use them.


#include <Arduino.h>
#include "globalVars.h"
#include "Pumps.h"

Pumps::Pumps(byte pin)
    _pin = pin;

    pinMode(_pin, OUTPUT);

void Pumps::ON()
        digitalWrite(_pin, HIGH);

void Pumps::OFF()
    digitalWrite(_pin, LOW);


#include <Arduino.h>    
#include "Pumps.h"

void waterMyPlants(int timeOn)
         wateringPump.ON(); //ERROR HERE: identifier "wateringPump" is undefined

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.

Hope someone can help.


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;


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);

Note the difference of declaration and definition in C++. See the resources linked in Divide code into several files.

Then any cpp file that does #include "globalVars.h" will have access to the globally shared wateringPump object.

Hey, thanks for the response.

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.

I’m including the library in my globaVar.h:

#include "BluetoothSerial.h"

extern BluetoothSerial SerialBT;

Also declaring it on globalVar.cpp

#include "BluetoothSerial.h"

extern BluetoothSerial SerialBT;

but when I try to use it anywhere on my code, for example my CLI.cpp :

#include "BluetoothSerial.h"
#include "globalVar.cpp"

SerialBT.begin(); // This declaration has no storage class or type specifier.

It works perfectly on my main.cpp, but thats not where I need it.

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

#include "BluetoothSerial.h"
//create object 
BluetoothSerial SerialBT;


Hi Max, thanks for the support!

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.


#include "BluetoothSerial.h"
void setup(){

SerialBT.begin("Name"); //NO PROBLEM HERE



#include "BluetoothSerial.h"

extern BluetoothSerial SerialBT; //NO PROBLEM HERE


extern BluetoothSerial SerialBT; //NO PROBLEM HERE


#include "globalVar.h"
#include "BluetoothSerial.h"

SerialBT.available(); // This declaration has no storage class or type specifier.

This should not be working since it does not include the header declarign SerialBT.

extern declarations always go in header files, not in cpp files. The cpp files include the header, and in one cpp file, create the object.

Let me quickly spin up a project to reproduce this.

Indeed, I am having 0 problems when doing it the way I described.

If you still have problems, please post the full code.

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.

Thank you again!

Statements like calls to a member function of an object can only be made inside functions.