Hi everyone, I was hoping someone could help me out. So I have a project where I send two values at the same time via bluetooth app on android, for example 3,330. I am trying to use strcpy and strncpy to copy the whole string that is sent and the store the individual elements in variables, so I can make the different variables do different actions.
below is my test code, and please be nice as I have never used strcpy or really worked with strings and char arrays much.
This is the code that I was basing on that I found on an Arduino forum
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char text[21] //contains a 0x00 terminated string between 5..20 characters
char textA[11] //is expected to contain the first 10 chars
char textB[11] //is expected to contain the second 10 chars
char tmp[11];
strcpy(text, "0123456789ABCDEF");
// fill tmp with 0x00
memset(tmp, 0, sizeof(tmp);
// copy first 10 characters (or less) to tmp
strncpy(tmp, text, 10);
// pad
snprintf(textA, sizeof(textA), "%-10s", tmp);
// fill tmp with 0x00
memset(tmp, 0, sizeof(tmp);
// copy second 10 characters (or less) to tmp
strncpy(tmp, &text[10], 10);
// pad
snprintf(textB, sizeof(textB), "%-10s", tmp);
}
Is this a good way of doing it because everything else I have found online such as strtok seem really complicated or are for inputting data in the serial monitor to send over bluetooth, when in my case I am sending data from an android phone to the arduino using hc-05 bluetooth module.
strcpy works with C strings. Serial.read gets you a char - they're not compatible.
It would be a bit easier if you sent a packet start and/or end marker e.g. <3,333>. As it is, after you see 33, how do you know if there's more to come?
Also, you will need to use serial.available to find out if there is anything to read.
The way you have done this here you have done this in setup, will it be possible to do this as a loop. As the purpose of the code is for a drinks machine that is connected to a android app on my phone. So it will need to be able to send different values, anything from 1-9 (number of shots) and upto 999 (glass size in ml). @UKHeliBob
I have seen this mentioned "Serial input basics" but in the examples they all seem to revolve around typing into the serial monitor to send data via bluetooth where as I am sending it via my phone
After looking at the code I kinda get whats happening but is definitely above my level of expertise, it would be amazing if you could tell me which lines to change so that it just uses the integers cause I only need to send integers not floating point and strings.
The code that I posted creates string variables which could be used in comparisons but not directly for calculations. However, the strings can be converted to integers if required
void setup()
{
char all[10] = {"3,330"};
Serial.begin(115200);
char * shot = strtok(all, ",");
char * glass_size = strtok(NULL, ",");
int shotInt = atoi(shot); //convert strings to integers
int glass_sizeInt = atoi(glass_size);
Serial.println(50 * shotInt); //just to prove that they are integers
Serial.println(4 * glass_sizeInt);
}
void loop()
{
}
In the code, Robin explains that what it currently expects to be entered is <HelloWorld, 12, 24.7>, ie a string, an integer and a float in that order
Once the string has been received the values are parsed out to appropriate data types cunningly named messageFromPC (the string), integerFromPC (the integer) and floatFromPC (the float)
That, and my example in post #10 should give you plenty to go on I think
I understand that in the code there is an order that it should be inputted in. I managed to change the code so that the float works just as an integer, but I just cant figure out to change the string to work as an integer or just remove the string component entirely. Like I say this level of coding on this subject is well over my head, I would really appreciate some clarification.
Thanks in advance.
Dean.
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
//strtokIndx = strtok(tempChars,","); // get the first part - the string
//strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(tempChars, ","); //get first integer
integer1FromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); //get second integer
integer2FromPC = atoi(strtokIndx); // convert this part to an integer
}
I will leave it to you to declare the integer variables and delete other declarations not needed now
Thank you so much your a saint, I did thing it had something to do with them lines, I just didnt think to put the tempChars into the integer one. Again thank you so much.