Convert array into one variable

Hello

I successfully connected to a RN4871U module with my Nano 33 IoT.
Now, after I read the sensor-data of a specific characteristic, I can print
the correct data in the console with:

for(int i = 0; i < 8; i++)
{
Serial.print(value[i]);
}

This is nice and works, but I want the complete array in one variable.
How is that possible?

I tried it with atoi, but it didnt seem to work. It returns 0 in the serial monitor.

The characteristic reading part of my code looks like this at the moment:

// Value Anzeige

int gunstatus; // Here the the values of "value[0], value[1] ... value[8] one after another should go
char value[8];

while (peripheral.connected()) {

if (GunCharacteristic.valueUpdated()) {

GunCharacteristic.readValue(value,8);

gunstatus = atoi(value);

for(int i = 0; i < 8; i++)
{
Serial.print(value[i]);
}

Serial.println("");
Serial.println(value[7]); // Value [7] is the only value i need, but i want to know how to get every 9 values into the "gunstatus" variable
Serial.println("");

Serial.print("Gunstatus: ");
Serial.println(gunstatus); // prints 0

}
}

Would be great if you can help me with that!

Use writeln() instead of print() and show the result.

If you request only

If you are sure that exactly value[7] contains the expected value then assign it to gunstatus like
gunstatus = value[7] - '0'; //convert ASCII to digital value

unfortunately, it doesn't work for me so far.

i added a new function:

void writeString(String stringData) { // Used to serially push out a String with Serial.write()

for (int i = 0; i < stringData.length(); i++)
{
Serial.write(stringData[i]); // Push each char 1 by 1 on each loop pass
}

}

Now the code looks like

int gunstatus;
char value[8];

while (peripheral.connected()) {

if (GunCharacteristic.valueUpdated()) {

GunCharacteristic.readValue(value,8); // serial monitor reads "R1000001M1234001", thats correct

Serial.println("");

writeString(value);

}
}

"writeString(value);" prints out the Data "R1000001M1234001", thats fine.
But I want something like this:

gunstatus = "writeString(value);"
my goal is that "gunstatus" has the value of "R1000001M1234001".

Sadly, this doesnt work.

Sure, I can assign a specific value to "gunstatus", but i want everything writeString(value) puts out.

Then gunstatus must be a char array to hold characters.

Autsch! that was too easy.

I just wrote

char gunstatus;
gunstatus = value;

instead of

char* guntstatus;
gunstatus = value;

i dont even need serial.write

Now it works. :slight_smile: Thanks!

just for curiosity, what can i do to convert "gunstatus" (which now has the value of R1000001M1234001),
to a hex value?

Serial.write(gunstatus,HEX);

prints only "R1000001M1234001"

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