passing value to a function

Hello friends,
I am trying to use an 'integer value' that is to be added each time to a 'char array' and the assembled text is to be transmitted by the nrf-radio.
something like this;

void loop(){
....
....
int slaveID=44;
transmitt(slaveID);
....
....
}

void transmitt(int slv){
char text[13]={slaveID+"-No Signal"};
radio.write(&text,sizeof(text));
}
I tried various ways but no success,
please help.

I tried various ways but no success,

Post something that actually compiles, in code tags.

Or post something that doesn't and post the errors you get.

Meanwhile, google sprintf().

Hello PaulS..many thanks for ur quick reply...I shall soon add my exact code that has problems , meantime I just wanted to know in short 'how to add an integer to a char array '.... if u cud help...

meantime I just wanted to know in short 'how to add an integer to a char array

That depends on what you mean by adding an int to an array.

Do you want to convert the int to a string, and then append that string to an existing string? If so, there are several ways. You could use itoa() to convert the int to a string, and then use strcat() to append it to another string.

Or, you could use the sprintf() function to that in one step - you know the function I mentioned in the first reply.

Hi PaulS,
Here is my .ino file.
In line numbers 69/70 I am trying to transmit the 'slave number' along with a message as '-No Signal'.
The slave number is an int slaveID as a received parameter in the function 'transmitt'
If I keep only "-No Signal" for transmission then NRF radio transmits the message but when I want to add the integer slaveID with the message...there seems to be no transmission.
If I try to change the way how I add the integer, then some or other error pops up while compiling.
Am I clear in stating my problem now please ?

ReapeterStn2.1.ino (1.69 KB)

    char text[13];
    sprintf(text, "%d-No Signal", slaveID);

You COULD have googled sprintf().

I am really sorry PaulS, probably I am not capable of understanding, although you are definitely trying to help me....How can I replace the 'radio.write' function with 'sprintf' , finally I need to transmit the combined string on NRF radio....

How can I replace the 'radio.write' function with 'sprintf'

You can't. What sprintf() does is populate the array that you want the radio.write() method to send.

wow...probably I cud try last lines as----

text[] = sprintf (text, "%d-No Signal", slaveID);
radio.write(&text,sizeof(text));

--hope that will work

sprintf returns the length of the string it generated.

Thank you everybody for the help...finally I cud use the sprintf and resolve the problem...

As always, you have some options on how to implement your program. In the code below:

void loop(){
  int slaveID;

  for (slaveID = 0; slaveID < 25; slaveID++) {
    transmitt(slaveID);
  }
  delay(5000);    // Pause for 5 seconds
}

void transmitt(int slv){
  char text[13];

//  sprintf(text, "%d-No Signal", slv);
  itoa(slv, text, DEC);
  strcat(text, "-No Signal");
  
  Serial.println(text);
  //radio.write(&text,sizeof(text));
}

the version that used the sprintf() function uses 3306 bytes of flash and 200 bytes of SRAM. However, the sprintf() function is very flexible and powerful, but your program only uses a small fraction of that power. The alternative version may look longer, but it only uses 2136 bytes of flash memory and 198 bytes of SRAM. (Compiled on a Mega2560.) While memory resources don't come into play in such a short program, it could at sometime in the future. The sprintf() function is a great tool to hang on your belt, but often is overkill when a simple itoa()/strcat() combo will get 'er done and deserves to hang on that same belt.