Using the NRF24 class in multiple files

I’m trying to access the functions within the nRF24 library whilst having my own sketch broken into .ino, .h and .cpp.

However if I don’t include

 RF24 radio(3, 4);

in both the .cpp and .ino file, the class functions aren’t recognised.

however if i do include

RF24 radio(3, 4);

in both files, a load of errors come up about things being already initialised.

How can i fix this? i.e. have a subroutines.h/.cpp file that can work with the radio class as well as a node.ino file that can access the commands?

The rest of the code is below if necessary:
node.ino

#include "radioInterface.h"

RF24Data volatile iFile;

RF24 radio(3, 4);
void setup() {

  setupRadio();
  radio.printDetails();
}

void loop() {
  // Do nothing for now

}

radioInterface.h

//Prototype function definition
void setupRadio(void);

radioInterface.cpp

#include "Arduino.h"
#include "radioInterface.h"

void setupRadio(void) {

setupRadio();
  radio.begin();                           // Setup and configure rf radio
  radio.setChannel(74);                    // Set the channel
  radio.setPALevel(RF24_PA_HIGH);          // Set PA LOW for this demonstration. We want the radio to be as lossy as possible for this example.
  radio.setDataRate(RF24_1MBPS);           // Raise the data rate to reduce transmission distance and increase lossiness
  radio.enableDynamicPayloads();           // Enable Dynamic Payloads
  radio.setAutoAck(1);                     // Ensure autoACK is enabled
  radio.setRetries(2, 15);                 // Optionally, increase the delay between retries. Want the number of auto-retries as high as possible (15)
  radio.setCRCLength(RF24_CRC_16);         // Set CRC length to 16-bit to ensure quality of data
  radio.maskIRQ(1, 1, 0);                  // Only recieve notifications for data recieved;
  
  radio.openWritingPipe(pipes[0]);         // Open the default reading and writing pipe
  radio.openReadingPipe(1, pipes[1]);
  
  radio.powerUp();                         //Power up the radio
  delay(5);
  
  radio.startListening();                  // Start listening
}

Damnit forum, just wrote a long answer but the forum gave me an error.

Short answer, only declare it where you need it. If you only need it in your files, only declare it there. Don't forget ti include the library there and not in the ino.

Or, if you want to use it in both, just declare it in the .ino and pass it as parameter you functions. If you only want to do that once (for example in a begin() call, make a pointer.

Last option would be to declare it as a extern but I think that's the ugly way to do it.

Create all the functions that use the nRf24 in one file and then you can call those functions from other files.

I have a project with a .h file for all my wireless code and another .h file for all my motor control code. When it feels like it the motor control code calls the appropriate wireless function. The .ino file is really just there to include the other files.

...R

septillion:
Damnit forum, just wrote a long answer but the forum gave me an error.

Short answer, only declare it where you need it. If you only need it in your files, only declare it there. Don't forget ti include the library there and not in the ino.

Or, if you want to use it in both, just declare it in the .ino and pass it as parameter you functions. If you only want to do that once (for example in a begin() call, make a pointer.

Last option would be to declare it as a extern but I think that's the ugly way to do it.

Can you give a code example for how you’re using it in both files?
Cheers - H

Robin2:
Create all the functions that use the nRf24 in one file and then you can call those functions from other files.

I have a project with a .h file for all my wireless code and another .h file for all my motor control code. When it feels like it the motor control code calls the appropriate wireless function. The .ino file is really just there to include the other files.

...R

That’s actually a pretty good idea, thanks! However if i was going to using the functions in both files, how would i do this?

Thanks, -H

hazza_ob:
That’s actually a pretty good idea, thanks! However if i was going to using the functions in both files, how would i do this?

I have never experienced your problem so I am not sure how to answer that.

My .ino file has this

#include "./LocoSlaveBaseBFiles/LocSlvRadBTin1634.h"
#include "./LocoSlaveBaseBFiles/LocSlvSoftStart.h"

and in setup() it has

setupRadio();
setupMotorControl();

which are calls to functions in each of those files.

In the ...SoftStart.h file there is a call

doRadioStuff();

which is a function in the other file.

Ahhhh - I wonder is your problem due to the need to declare function prototypes at the top oh each .h file

This is at the top of the ...SoftStart.h file

    // function prototypes
void showDebugData(int interval) ;
void doRadioStuff();
void revDetectorISR();
void applyAcceleration();
void checkForStopCommand();
void checkForStall();
void checkForSteps();
void updatePWM();
long calcPWM(long errVal, byte kFactor);
void outputMotorPwr();

These functions are all within that file. And there is a similar piece at the top of the other file for its function prototypes.

With .ino files the Arduino system automatically creates the function prototypes.

...R

PS. Just to avoid confusion, the Arduino IDE does not recognize relative file references of the type '" ./LocoSlav...". I have another Python program that converts them to absolute references.

Uncompiled, just from the top of my head :smiley:

If you want to define it in the ino

.ino

#include "RF24.h"

RF radio(3,4);

void setup(){
  myThingsBegin(radio);
}

.h

class RF;

static RF* myRadio;

.cpp

#include "myThingsBegin.h"
#include "RF24.h"

void myThingsBegin(RF24& radio){
  myRadio = &radio;
  myRadio->someFunction();
}

Or the extern route, but that will fail if you rename it and doesn't lend itself to OOP that much
.ino

#include "RF24.h"

RF radio(3,4);

void setup(){
  myThingsBegin(radio);
}

.h

class RF;

extern RF radio;

.cpp

#include "myThingsBegin.h"
#include "RF24.h"

void myThingsBegin(){
  radio.someFunction();
}

septillion:
Uncompiled, just from the top of my head :smiley:

C/C++ really pisses me off when it is necessary to do all that stuff (not your fault, of course),

...R