Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino delimiter, stores 12,24 data to (val = 12, val1 = 24)

Status
Not open for further replies.

eebhoi01

Advanced Member level 4
Full Member level 1
Joined
Feb 22, 2012
Messages
116
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Visit site
Activity points
2,347
Hello guys,

My Arduino will be receiving a string data that has two values separated by a comma. Can anyone please help me split those values and assign it to two separate variables?

Say for example: the string is equal to 12,24. I wanted to make it val=12, val=24.

Thanks in advance


Best regards
 

if you look here
https://arduino.cc/en/Reference/String

you will see that strings is arrays of chars. So just look for the symbol "," and then split the results. The algorithm should be
if "," found copy the value into another string and then use atoi function
Code:
test="Hello,word";
char temp[5];
int i,j,val1,val2;
for (i=0;i<=test.length;i++){ //check entire string 
if test[i]="," {//if comma found 
for (j=0;j=i-1;j++)//for the beginning to the current position - 1 for the comma symbol 
{
temp=test[j];//transfer the chars to temp string 
}
val1=atoi(temp);
for(j=i+1;j=string.length;j++)//the remaining after comma symbol
{
temp[j]=test[j];//transfer the remain chars
}
val2=atoi(temp);
}
}
hope that helps.
 
Last edited by a moderator:

if you look here
https://arduino.cc/en/Reference/String

you will see that strings is arrays of chars. So just look for the symbol "," and then split the results. The algorithm should be
if "," found copy the value into another string and then use atoi function
test="Hello,word";
char temp[5];
int i,j,val1,val2;
for (i=0;i<=test.length;i++){ //check entire string
if test="," {//if comma found
for (j=0;j=i-1;j++)//for the beginning to the current position - 1 for the comma symbol
{
temp=test[j];//transfer the chars to temp string
}
val1=atoi(temp);
for(j=i+1;j=string.length;j++)//the remaining after comma symbol
{
temp[j]=test[j];//transfer the remain chars
}
val2=atoi(temp);
}
}

hope that helps.


thank you for your reply sir,

but i cant get the code running,

i have questions, what type of variable is the test?

I will be getting data through Serial.read()

will it work if i set test = Serial.read(); ?


thank you
 

test is the string (char test[100]; or whatever length you need) which contains your data. Of course you should use test=serial.read();
To use the atoi function you need to use #include <stdlib.h>
I do not know what else your problem could be.
 

test is the string (char test[100]; or whatever length you need) which contains your data. Of course you should use test=serial.read();
To use the atoi function you need to use #include <stdlib.h>
I do not know what else your problem could be.

Hello Sir, this is my code based on your example:

#include <stdlib.h>

char test[5];
char temp[5];
int i,j,val1,val2;
void setup()
{
Serial.begin(9600);
}

void loop()
{
if (Serial.available())
{
test = Serial.read();
for (i=0;i<=test.length;i++){ //check entire string
if test="," {//if comma found
for (j=0;j=i-1;j++)//for the beginning to the current position - 1 for the comma symbol
{
temp=test[j];//transfer the chars to temp string
}
val1=atoi(temp);
for(j=i+1;j=string.length;j++)//the remaining after comma symbol
{
temp[j]=test[j];//transfer the remain chars
}
val2=atoi(temp);
}
}
}
}


error says: incompatible types in assignment of 'int' to 'char [5]'

im confusd, thanks in advance
 

test = Serial.read(); is not correct. It assigns only one byte to test. test should be something like test[100] and every time a data comes serially it should be assigned to test[] like

Code C - [expand]
1
2
3
4
5
6
7
8
i = 0;
while(i<100) {
if(serial.available()) {
   test[i] = serial.availabel();
   i++;
 
}
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top