Converting int to a char array in order to send it to another Arduino

So my project is for school, and we’re basically trying to send an integer value, which is from a potentiometer/joystick, to another Arduino, using radio transmitter and receiver. Everything works fine using characters, like sending our names, but when using an integer everything gets messed up.

Here is the tx code:

#include <RH_ASK.h>
#include <SPI.h>

#define JOYY A0
#define JOYX A1

RH_ASK rf_driver;

int pos_x;
int pos_y;

void setup() {
  Serial.begin(9600);
  rf_driver.init();
  pinMode(13, OUTPUT);
  pinMode(JOYY, INPUT);
  pinMode(JOYX, INPUT);
}

void loop() {
  readJoy();
  //Serial.println(pos_y);
  char *msg = (char*)pos_y;
  
  Serial.println(*msg);
  rf_driver.send(msg, strlen(msg));
  rf_driver.waitPacketSent();
  
  
}

void readJoy() {
  pos_x = analogRead(JOYX);
  pos_y = analogRead(JOYY);
  pos_x = map(pos_x, 497, 1023, 1, 173);
  pos_y = map(pos_y, 497, 1023, 1, 173);
  //Serial.println(pos_y);
  //Serial.println(pos_x);
  
  }

and this is the rx code:

#include <RH_ASK.h>

#include <SPI.h>

#include <Servo.h>

int val = 0; 

int x = 0;

RH_ASK rf_driver;

Servo myservo;


void setup() {
  rf_driver.init();
  
  Serial.begin(9600);

  myservo.attach(6);

}

void loop() {
  
  uint8_t buf[10];
  uint8_t buflen = sizeof(buf);

  if (rf_driver.recv(buf, &buflen))
    {
      
      Serial.print("Message: ");
      Serial.println((char*)buf);
      
      
      //myservo.write(buf);
    }

}

The problem I’m having is with

char *msg = (char*)pos_y;
  
  Serial.println(*msg);

I’m trying to convert the integer into a char, and then print it. There are no errors, but looking in the serial monitor, the expected value 0 is actually just nothing, and pushing the joystick up or down just writes a bunch of these ⸮, backwards question marks. I’ve tried lots of different methods, most of them just doing the same thing.

Can anyone help me? Thanks.

Use itoa() if you want to convert your int into ascii before sending it (the way you try to do so is not correct, you have a point to the binary representation, not a cString)

You could also just send the binary version

Yeah I tried using itoa a few hours ago, and it worked to turn the integer into a string, but the problem is is that the library wont accept strings, only char.

itoa builds a cString which is a (null terminated) array of chars and the library does send byte buffers just fine..

Not sure what you mean

oumardiop1:
the problem is is that the library wont accept strings, only char.

Errrrm… ?

So your pos_y value will always be between 1 and 173 right? this will convert a 3 digit int to a character array

    char posy_str[3];
    uint8_t pos_y= 123;
    sprintf(posy_str, "%u%u%u",(pos_y/100)%10, (pos_y/10)%10, (pos_y/1)%10);
    printf("%s", posy_str);

this will print 123

typematrix: So your pos_y value will always be between 1 and 173 right? this will convert a 3 digit int to a character array

    char posy_str[3];
    uint8_t pos_y= 123;
    sprintf(posy_str, "%u%u%u",(pos_y/100)%10, (pos_y/10)%10, (pos_y/1)%10);
    printf("%s", posy_str);

this will print 123

posy_str needs an additional space for the null character at the end. If you are going to use sprintf, might as well do it the easy way:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char posy_str[4];
  uint8_t pos_y = 123;
  sprintf(posy_str, "%03u", pos_y); // convert to three digits with leading zero's
  Serial.println(posy_str);
  while(1);
}

Although you can save some program memory and do it manually:

void setup() {
  Serial.begin(9600);
}

void loop() {
  char posy_str[4];
  uint8_t pos_y = 123;
  posy_str[0] = (pos_y / 100) + '0';
  posy_str[1] = ((pos_y / 10) % 10) + '0';
  posy_str[2] = (pos_y % 10) + '0';
  posy_str[3] = '\0';
  Serial.println(posy_str);
  while (1);
}