I am confused on arrays in the programs for NRF24L01 modules

I found an NRF24L01 transmitter program online, and I am quite confused on how the msg[1] array works, can anyone explain to me what that does. Can someone also explain to me how radio.write(msg, 1) works?

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;

void setup(void) {
  pinMode(SW1,INPUT);
  digitalWrite(SW1, LOW);
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop(void) {
  if (digitalRead(SW1) == HIGH)
  {
    msg[0] = 111;
    radio.write(msg, 1);
  }
}

I understand what the pipe, radio, and button is but I do no understand what the msg[1] does, and I do not understand why it says msg[0] = 111, it would be helpful if someone could explain that part to me too. I also do not quite understand digital.write(msg, 1). I do not get what the parameters msg and 1 do.

array msg, location [0] has been assigned a value of 111

digital.write(msg, 1) doesn't appear in what you posted, perhaps you meant radio.write(msg, 1)?
If so, it's passing the message array to radio.write, and indicating only one value is to be sent. That means the first value in the array is to be sent, the value that was written to msg[0].
Remember, in C/C++, arrays are zero-based; that is, the first value in an array is at location 0.
At least, that's how I read it.

The array msg is an array of int data type, so
msg[0] = 111;
assigns the value 111 to the first element of the array.

That's wrong. If it appears to "work" it's only because the processor is little -Endian and you put a value less than 256 into the variable. It should be:
radio.write(msg, sizeof(msg));

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.