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.
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?
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.