Convert Many ints to char Array

Hi,

I'm using the nRF24L01 transceiver to write and receive data from a controller to a car.

The controller reads many potentiometers, and I'm trying to put all the inputs into one variable which can then be sent through the transceiver and decoded once it gets over.

I'm able to get sample code working with the transceivers and I have successfully sent "Hello World" from one to the other, however, I cannot get a solution working that sends the variable that contains all the potentiometer outputs.

Transmitter Code:

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

// This is the code for the transmitter, which goes on a nano and into my controller. 

RF24 radio(2, 3); // CE, CSN

const byte address[6] = "00001";
String s = ", ";

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
  Serial.begin(9600);
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(A6, INPUT);
  pinMode(A7, INPUT);
}


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


void loop() {
  int joysticka1 = analogRead(A0);
  int joysticka2 = map(analogRead(A1), 0, 1023, 99, 0);
  int joysticka3 = map(analogRead(A2), 0, 1023, 99, 0);
  int joystickb1 = analogRead(A3);
  int joystickb2 = map(analogRead(A4), 0, 1023, 99, 0);
  int joystickb3 = map(analogRead(A5), 0, 1023, 99, 0);
  int switcha = digitalRead(4);
  int switchb = digitalRead(5);
  int pota = map(analogRead(A6), 0, 1023, 99, 0);
  int potb = map(analogRead(A7), 0, 1023, 99, 0);


  if (joysticka1 > 50){
    joysticka1 = 0;
  }
  else
  {
    joysticka1 = 1;
  }
  if (joystickb1 > 50){
    joystickb1 = 0;
  }
  else
  {
    joystickb1 = 1;
  }

  String test = "";


  
  test += joysticka1;

  if (joysticka2 <= 9){
    test += "0";
  }
  test += joysticka2;

  if (joysticka3 <= 9){
    test += "0";
  }
  test += joysticka3;

  test += joystickb1;

  if (joystickb2 <= 9){
    test += "0";
  }
  test += joystickb2;

  if (joystickb3 <= 9){
    test += "0";
  }
  test += joystickb3;

  test += switcha;
  test += switchb;
  
  if (pota <= 9){
    test += "0";
  }
  test += pota;
  
  if (potb <= 9){
    test += "0";
  }
  test += potb;

  const char charbuf[32];
  char text[] = test.toCharArray(charbuf, 32);
  radio.write(&text, sizeof(text));
  

//  Serial.println(test);
//  radio.write(&test, sizeof(test));
  delay(50);
}

Receiver Code:

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

RF24 radio(8, 7); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

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

I am fairly sure the issue lies within how the transmitter is 'packaging up' and sending the data. The issue almost certainly lies within the last 4 lines of the loop().

Hoping someone can take a look and tell me what I'm doing wrong :slight_smile:

Thanks in advance

The documentation for toCharArray says:

Returns

Nothing

You seem to be initializing 'text' with nothing. Since you copied the String to 'charbuf', why not send charbuf?

  char charbuf[32];
  test.toCharArray(charbuf, 32);
  radio.write(charbuf);

or skip the copying and use the String directly:

  radio.write( test.c_str());

Great! That worked, thank you.

Only one question -- I'm only getting half of the message that is being sent.

Sent:
0515104950119999

Received:
051510

I only get the first 6 out of 16 digits. I tried changing the size of the char array to 64 to no avail. If you can't tell, I kind of have no idea what I'm doing :slight_smile: so I appreciate any input on this, even though it's technically a different topic.

That sends just one byte. To send the entire array use
radio.write(charbuf, sizeof(charbuf));

Working great! Thank you everyone

The nRF24L01 sends packets of bytes. It doesn't care whether those bytes represent a character array or numerical values. The best technique is to build a struct whose elements are the data types that you want to send. Then, send the entire struct at once as a group of bytes. Beware that the maximum payload size of an nRF24L01 packet is 32 bytes. So, if you need to send more data than that, you have to send multiple packets.

On the other side, you populate an identical struct with the bytes from the received packet. This puts the original values back into the struct's elements exactly how they were on the transmit side.

Sorry. I thought they were inheriting from Stream.
radio.write(charbuf, sizeof(charbuf));
That will send all 32 bytes. You probably only want to send the contents:
radio.write(charbuf, strlen(charbuf));
alternatively:
radio.write( test.c_str(), test.length());

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