Dynamic allocation of array for NRF24L01 payload

Hello

My problem is that I have NRF24L01 sending and receive modules. At the moment the receiver is configured to receive two bytes. I want to rewrite the receiver code so that it can either receive two bytes as now or receive 13 bytes, depending on whether a condition is met. I can't use the NRF dynamic payload option (I think) because I don't have the option of reprogramming the existing transmitters which are stuck with the two byte payload.

Also, I can't use any of the NRF libraries because the SPI functionality within them seems to conflict with other libraries and peripherals. The existing transmitters are using ATTINY84s which makes it complicated.

So I thought that I would declare a dynamic array globally in the receiver code and then specify its size in setup(). I have never used dynamic variables so I'm not sure what I am doing wrong. At the moment I just want to set the size unconditionally in setup

So I start with this at the top

byte TX_PLOAD_WIDTH = 0;  // 
unsigned char* tx_buf = 0;

Then in setup() I have this

 delete[] tx_buf;
tx_buf = new unsigned char [TX_PLOAD_WIDTH];

It doesn't work. It only works if I declare the array size at the start but then I can't change it. Help, please.

Any write takes a pointer and a length.

It give a rats about where that address is pointing and what length ist is (if between 1 and 32).

The structure of the memory pointed to does not matter.

In the receiver you can determine the actual length of the payload before reading it.

So your question does not make sense.

delete[] tx_buf;

Remove this.

Why not just statically allocate 13 bytes and use less if you don't need all of them?

robertjenkins: So I thought that I would declare a dynamic array globally in the receiver code and then specify its size in setup().

I assume you are talking about the place where the Arduino will store the message that comes from the nRF24

If so just create a 32 byte array and make life simple.

Even if you do have some dynamic way to set the array size there is no practical way to use the extra memory elsewhere in your program. Dynamic memory allocation in the small memory of an Arduino brings with it a high risk that of a crash when you least expect it. Unlike on a PC there is no operating system to oversee things.

...R

Thanks. I thought, from (possibly mis-) reading your excellent tutorial, that the payload array sizes on the sending and receiving ends had to match in order for anything to be received. If it's ok for the receiver side to be larger than the transmitter then there is no problem.

Edit - having tested this, it doesn't seem to work. If I declare the sending side as 2 bytes and the receiving as 13, nothing comes through.

The clunky way to solve this is to split up the 13 sending bytes into 7 chunks and send them sequentially

robertjenkins: Also, I can't use any of the NRF libraries because the SPI functionality within them seems to conflict with other libraries and peripherals.

Could you please elaborate on that?

From my experience the NRF library uses SPI as intended and so does the NRF chip.

robertjenkins: Thanks. I thought, from (possibly mis-) reading your excellent tutorial, that the payload array sizes on the sending and receiving ends had to match in order for anything to be received.

I expressed it that way for simplicity - it will cover most people's need.

What must match on both sides is the length (in bytes) that is used in the write() and read() commands.

...R

Got you (I think).

Whandall: Could you please elaborate on that?

From my experience the NRF library uses SPI as intended and so does the NRF chip.

I posted about it here but got no response

https://forum.arduino.cc/index.php?topic=669188.msg4504637#msg4504637

The SPI included in RF24 works fine for me but then won't work with an accelerometer.

Robin2: I expressed it that way for simplicity - it will cover most people's need.

What must match on both sides is the length (in bytes) that is used in the write() and read() commands.

...R

Thanks for the clarification. Problem now solved.

Not being able to use two libraries on the ATTINY84 does not mean you can not use it on other Arduinos. As I understood you post, you have to live with the senders, but are rebuilding the receiver.

I only build very small series if any, so I can use less cost optimized chips.

Yes I can't reprogram the transmitters.