Combining several integers into one with spaces

Hey people,

The thing I'm about to ask seems so simple but I didn't figure out with my limited knowledge.

So I have 3 integers in my hand and I would like to combine them into one with empty spaces in between.

They are for instance temp, humidity, pressure. And their values are like "25,80 and 1038" each. I want to turn them into an integer that looks like this "25 80 1038".

Can you also show me how to turn it into string also like this: "25 80 1038"

cagancelik:
Can you also show me how to turn it into string also like this: "25 80 1038"

one solution you could try is use sprintf.

for example:

void combine_ints (int temp, int humidity, int pressure)
{
  char buffer [20];
  sprintf (buffer, "%d %d %d", temp, humidity, pressure);
  Serial.println(buffer);
}

hope that helps....

sherzaad:
one solution you could try is use sprintf.

for example:

void combine_ints (int temp, int humidity, int pressure)

{
  char buffer [20];
  sprintf (buffer, "%d %d %d", temp, humidity, pressure);
  Serial.println(buffer);
}





hope that helps....

I tried to implement your method into my code but my serial output has weird symbols like this:

⸮⸮⸮⸮
⸮ ⸮
⸮⸮⸮⸮
⸮ @
⸮⸮⸮⸮
⸮ ⸮
⸮⸮⸮⸮
⸮
⸮@
⸮⸮⸮⸮
⸮ ⸮
⸮⸮⸮⸮

Here's my code:

void loop() {
int humidity = dht.readHumidity();
int temp = dht.readTemperature();
int pressure = readPressure();

 char buffer [20];
  printf (buffer, "%d %d %d", humidity, temp, pressure);
  Serial.println(buffer);
  delay(1000);
}

There's also a Serial.begin(9600); part in the void setup.

cagancelik:
They are for instance temp, humidity, pressure. And their values are like "25,80 and 1038" each. I want to turn them into an integer that looks like this "25 80 1038".

This "25 80 1038" is not an integer, it is a string of characters.

Why do you want to do this? I suspect it may not be necessary.

...R

Arduino will send it to Raspberry Pi via nrf24l01 and Raspberry Pi will parse it and show the individual values on the screen.

cagancelik:
I tried to implement your method into my code but my serial output has weird symbols like this:

⸮⸮⸮⸮

⸮ ⸮
⸮⸮⸮⸮
⸮ @
⸮⸮⸮⸮
⸮ ⸮
⸮⸮⸮⸮

⸮@
⸮⸮⸮⸮
⸮ ⸮
⸮⸮⸮⸮





Here's my code:



void loop() {
int humidity = dht.readHumidity();
int temp = dht.readTemperature();
int pressure = readPressure();

char buffer [20];
  printf (buffer, "%d %d %d", humidity, temp, pressure);
  Serial.println(buffer);
  delay(1000);
}




There's also a Serial.begin(9600); part in the void setup.

you used

printf (buffer, "%d %d %d", humidity, temp, pressure);

instead of

**s**printf (buffer, "%d %d %d", humidity, temp, pressure);

in your code... :o

cagancelik:
Arduino will send it to Raspberry Pi via nrf24l01 and Raspberry Pi will parse it and show the individual values on the screen.

There is no need to join them together like that when using an nRF24. Just put the three values as the elements of an array and send the array. Like this

int myArray[3];

myArray[0] = 25;
myArray[1] = 80;
myArray[2] = 1038;

radio.write(&myArray, sizeof(myArray);

and have an identical array on the RPI into which the received data is placed.

…R
Simple nRF24L01+ Tutorial