How to convert an int to array char and vice versa?

Hi!

I need to send data from one arduino to an other. To do so I need to convert the int to an array of chars (specifically to ASCII caracters), send them and "decode" them back to the int. I will use a value of a PWM, map it between 0 and 255 so theoretically I need an array of 3 chars. Thats all that I know so far.

#define PWM_PIN A0
int PWM_Value = 0;
char Brightness = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PWM_PIN, INPUT);
}

void loop() {
  PWM_Value = analogRead(PWM_PIN);
  Brightness = map(PWM_Value, 0, 1023, 0, 255);
  Serial.println(Brightness);

  int BrightnessArray[3]; // Because I'll use only 3 digits

  

  delay(1000);
  
}

I have 0 experience with arrays and don't know how to proceed with them :confused:

Serial.print does a pretty good job of converting numbers (variables) to their ASCII representation

TheMemberFormerlyKnownAsAWOL:
Serial.print does a pretty good job of converting numbers (variables) to their ASCII representation

If you don't need leading 0s then this is the easiest method. Otherwise with leading 0s you can use:

char str[3];
snprintf (str, 3, "%03d", n);

And I can receive them on the other arduino by the same method? I mean by Serial.read().

What if I'll have more potentiometers?

ToddL1962:
If you don't need leading 0s then this is the easiest method. Otherwise with leading 0s you can use:

char str[3];
snprintf (str, 3, "%03d", n);

No that would not work as the trailing NULL char will come bite you :wink:

try this to see what happens

void setup()
{
  Serial.begin(112500);
  char str[3];
  snprintf (str, 3, "%03d", 15);
  Serial.println(str[0]); // 0
  Serial.println(str[1]); // 1
  Serial.println(str[2]); // <<== the trailing NULL char !! argh...
}

void loop() {}

if you want it to work with leading 0 and all the way to 255, then you could use

void setup()
{
  Serial.begin(112500);
  char str[4];
  snprintf (str, 4, "%03d", 15);
  Serial.println(str[0]); // 0
  Serial.println(str[1]); // 1
  Serial.println(str[2]); // 5
  Serial.println(str[3]); // <<== the trailing NULL char
}

void loop() {}

but snprintf() uses tons of flash memory... better do a couple test to get leading '0' if you are short in flash.

Skiermaxhtc:
And I can receive them on the other arduino by the same method? I mean by Serial.read().

What if I'll have more potentiometers?

as it fits on 1 byte, if this is the only thing you send, don't bother going ASCII. just send the byte with Serial.write() and read it on the other side with a Serial.read() and you'll have your value

You are correct J-M-L. That should have been a 4 character array for the null. I haven’t used any of those functions on the Arduino so I haven’t surveyed the memory footprint.

The best solution would be to frame the data so the receiver knows it is in sync with the transmitter. But that is another subject.

ToddL1962:
You are correct J-M-L. That should have been a 4 character array for the null. I haven’t used any of those functions on the Arduino so I haven’t surveyed the memory footprint.

The best solution would be to frame the data so the receiver knows it is in sync with the transmitter. But that is another subject.

I think on any platform snprintf() will add the trailing null char

Indeed, framing the data is usually the way to go - here if it is only 1 byte that has to be sent (and if there is no need for verifying it’s sent without error) then just pushing that byte would make development easy