Problem with indexing the transmissions

Arduino mega connected to nRF24L01 module transmitter and receiver.I want to index the transmission in nRF24L01 , for example while transmitting the character, i need to concatenate a variable in the end so that i get to know which transmission the receiver is receiving for example: if i am transmitting Hello world i need to concatenate a variable i in the end of the char hello world which will take values from 1 to 9 and send Hello world1, Hello world2,...Hello world 9 and in the receiver side see how many data i do receive... any ideas?The transmitted code.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);

const byte rxAddr[6] = "00001";

void setup()
{
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(rxAddr);
  
  radio.stopListening();
}

void loop()
{
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  
  delay(1000);
}

The receiver Code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);

const byte rxAddr[6] = "00001";

void setup()
{
  while (!Serial);
  Serial.begin(9600);
  
  radio.begin();
  radio.openReadingPipe(0, rxAddr);
  
  radio.startListening();
}

void loop()
{
  if (radio.available())
  {
    char text[32] = {0};
    radio.read(&text, sizeof(text));
    
    Serial.println(text);
  }
}

Thanks in advance.

Any chance in sending the identifier from the transmitter? That would likely simplify things.

thats what i want to know , how do i change the code to send the identifier from the transmitter

Change "Hello World" text in the transmitter. ?

how do you concatenate a integer to a character hello world and transmit it thats what i want to know

how do you concatenate a integer to a character hello world and transmit it thats what i want to know

The snprintf function is one way.

Where is the int coming from? Digital inputs? If so:

Since you have defined text[] as const char[], that declaration will also need to be changed. It will no longer be a precompiled constant. The const will need to be deleted, a length defined for text[] (text[x ]). Then use sprintf or snprintf to get Hello World and the integer assigned to text[].

This Simple nRF24L01+ Tutorial may give you some ideas.

If you just want an ID along with a message just use a character A to Z or a to z

...R

how do i increment the variable to go from a to z or A to Z.

Characters are represented as numbers (ASCII character codes) internally. If you add 1 to 'A' you get 'B'. 'B' +1 = 'C'. Note the single quotes around the letters, Without the quotes they are not characters.

char variable = 'A';

void setup() 
{
  Serial.begin(9600);
  for(int n = 0; n < 26; n++)
  {
  Serial.print ("Variable (char) = ");
  Serial.print(variable);
  Serial.print("     ascii code (DEC) = ");
  Serial.println(variable, DEC);
  variable ++;
  } 
}

void loop()
{

}

but how do i concatenate the variable in this case "text" and transmit it such that it looks like HelloworldA, Hello WorldB....

Read about snprintf (see the link in reply #5) to see one way.

Example:

char variable = 'A';

void setup() 
{
  Serial.begin(9600);
  for(int n = 0; n < 26; n++)
  {
  //Serial.print ("Variable (char) = ");
  //Serial.print(variable);
  //Serial.print("     ascii code (DEC) = ");
  //Serial.println(variable, DEC);
  char payload[16];  
  snprintf(payload, 15, "Hello World%c", variable);
  Serial.println(payload);
  variable ++;
  } 
}

void loop()
{
}

Thnk you let me try this out.

If you have a char array for the data to be sent and if the char array has been designed with some empty space - for example

char myCharArray[32] = {"    Hello World"}; // hope I have the syntax correct
char myIndexChar = 'A';

then you can add an index character in the first position like this

myCharArray[0] = myIndexChar;

which will result in "A Hello World"

...R

Thank You guys both of these codes worked!! Thank You!!