hai,
i am using arduino for past 2 months i am still not well versatile in it , currently i am trying to develop a code in which i am sending a set of integer data splitted by a character for example 100,20,3000,105* here in this above example i am sending for integer value splitted by cuma (,) and last end of the data is been determined by () which are been sent serially to arduino and are been splitted
i.e 100,20,3000,4000
angle = 100 , fuel = 20, speed = 3000 , altitude = 105
this is the code
String readString; //main captured String
String angle; //data String
String fuel;
String speed1;
String altidude;
String trr[10];
int ind1; // , locations
int ind2;
int ind3;
int ind4;
int c = 0;int count=0;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}
void loop() {
if (Serial.available()> 0) {
//gets one byte from serial buffer
char c = Serial.read();
if (c == '*') {
//do stuff
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString); //prints string to serial port out
ind1 = readString.indexOf(','); //finds location of first ,
angle = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1 + 1 ); //finds location of second ,
fuel = readString.substring(ind1 + 1, ind2 + 1); //captures second data String
ind3 = readString.indexOf(',', ind2 + 1 );
speed1 = readString.substring(ind2 + 1, ind3 + 1);
ind4 = readString.indexOf(',', ind3 + 1 );
altidude = readString.substring(ind3 + 1); //captures remain part of data after last ,
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
readString = ""; //clears variable for new input
angle = "";
fuel = "";
speed1 = "";
altidude = "";
}
else {
readString += c; //makes the string readString
}
}
}
and when i send the data its executing correctly and the data are been splitted but the correct value are coming only when the variables are been printed within the if(serial.available()>0) loop , if i try to print the variable out side the loop that is as mentioned below its not printing the value...plz help me how to solve this issue
String readString; //main captured String
String angle; //data String
String fuel;
String speed1;
String altidude;
String trr[10];
int ind1; // , locations
int ind2;
int ind3;
int ind4;
int c = 0;int count=0;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}
void loop() {
if (Serial.available()> 0) {
//gets one byte from serial buffer
char c = Serial.read();
if (c == '*') {
//do stuff
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString); //prints string to serial port out
ind1 = readString.indexOf(','); //finds location of first ,
angle = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1 + 1 ); //finds location of second ,
fuel = readString.substring(ind1 + 1, ind2 + 1); //captures second data String
ind3 = readString.indexOf(',', ind2 + 1 );
speed1 = readString.substring(ind2 + 1, ind3 + 1);
ind4 = readString.indexOf(',', ind3 + 1 );
altidude = readString.substring(ind3 + 1); //captures remain part of data after last ,
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
readString = ""; //clears variable for new input
angle = "";
fuel = "";
speed1 = "";
altidude = "";
}
else {
readString += c; //makes the string readString
}
}
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
}