Hi! I'm pretty much a novice at coding in general, and I need some help. I'm trying to transmit a set of three numbers all at once via serial.write. the first two are analog input readings, so a number between 0000 and 1023, and the third is a decimal representation of a binary number. The program receiving this number has a pretty simple function for splitting variables into pieces, so I could send a number like 0034095323 and it could easily split that into 0034, 0953, and 23, I just can't figure out how to combine them on the arduino. The three numbers are currently stored individually in three variables.
I think it has something to do with strings, but I can't figure it out. I would appreciate some help with this. Thanks!
keboose:
...it could easily split that...
Just don't split the number. Why do you do it?
it's the program on my PC that can split the number. I want to transmit all three numbers at once from the arduino, so I am looking for a way to combine them all into one variable to send over the serial connection.
With Serial.write(), as you can read in this page, can't send int variables directly, so you need to convert it into a string.
So, 3 numbers of 4 digits each = 12 chars + 1 NULL TERMINATOR, you have a string of fixed length 13.
Using the itoa() function, you can convert an int into string.
Start with this code:
char DataToSend[13];
char SingleInt[5];
int Number0 = 123;
int Number1 = 567;
int Number2 = 8901;
void setup() {
Serial.begin(115200);
memset(DataToSend,0,sizeof(DataToSend)); //Ensure it's empty.
itoa(Number0,SingleInt,10);
strcat(DataToSend,SingleInt);
itoa(Number1,SingleInt,10);
strcat(DataToSend,SingleInt);
itoa(Number2,SingleInt,10);
strcat(DataToSend,SingleInt);
Serial.println(DataToSend);
//Serial.write(DataToSend); This will send the info
}
void loop() {
// put your main code here, to run repeatedly:
}
Thank you for the help. I'll give it a try and see if it works out.