I am having a problem getting the right serial print from my program.
I serial write then save it to a char
transform chat to int
and use index of() to search for specific char
then i want to print out the string from 0 to indexOf()
but instead i receive the whole serial input.
Any suggestion would be much appreciated serial_write_example.ino (925 Bytes)
char d;
char d1;
String str_d;
int int_d;
int servo1degree, servo2degree;
int indexOfA, indexOfB;
String str_send;
String str_servo1, str_servo2;
void Receive_serial_data(){
while (Serial.available()>0){
d=Serial.read();
if(d=='\n') {break;}
else {str_d+=d;}
}
}
void Parse_the_data()
{
indexOfA = str_d.indexOf("A");
indexOfB = str_d.indexOf("B");
if(indexOfA >-1){ str_servo1=str_d.substring(0,indexOfA ); servo1degree=str_servo1.toInt(); }
if(indexOfB >-1){ str_servo2=str_d.substring(indexOfA+1,
indexOfB); servo2degree=str_servo2.toInt(); }
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
Serial.write("\n8A13\nB56");
Receive_serial_data();
if(d=='\n')
{
Parse_the_data();
d=0;
str_d="";
}
Serial.print(str_servo1);
}
and the print i am getting is whatever i put on serial write command
Hello, I’ve seen your code, my question is about the difference between what you say and what you do, could you please be so kind and comment this in your code explicitly because I cannot find it
char d; // char reads serial input
String str_d; // convert serial input to str
int servo1degree, servo2degree; // wanted output
int indexOfA, indexOfB;
String str_servo1, str_servo2;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write("8 A"); // serial input
Receive_serial_data(); // function of receiving data from serial (below)
if(d=='\n') //checks input
{
Parse_the_data(); // parse data function (below)
d=0;
str_d="";
}
Serial.println(indexOfA); // serial print output
}
void Receive_serial_data(){ //function for receiving serial data
while (Serial.available()>0){
d=Serial.read(); //save serial read at char d
if(d=='\n') {break;}
else {str_d+=d;} // save serial read at str str_d
}
}
void Parse_the_data(){
indexOfA = str_d.indexOf("A"); //check if "Α" is in the str_d
indexOfB = str_d.indexOf("B"); //check if "Α" is in the str_d
if(indexOfA >-1){ str_servo1=str_d.substring(0,indexOfA ); servo1degree=str_servo1.toInt(); } // if "Α" exist in str_d create a sub string from (0, indexOfA )
if(indexOfB >-1){ str_servo2=str_d.substring(indexOfA+1, indexOfB); servo2degree=str_servo2.toInt(); }// if "B" exist in str_d create a sub string from (indexOfA+1, indexOfB )
}
for Serial.println(indexOfA); i am geting output : "serialinput""0"
no mater what the input is.
What i am trying to do is to split serial input to strings based on letters.
Is there any example for that?