Please help me understand this code!!

Hi…i am trying to create a program to send mobile accelerometer data to arduino using the HC-05 bluetooth module. i found this tutorial to help me. the website has provided the code but i dont fully understand the code. can anyone help me out in understanding this code.
The link to the website is herehttp://quzalmehmood.com/send-mobile-accelerometer-data-to-android-using-bluetooth/

the code is given below:

int x=0;
char xx='0';
int mydata=0;
int Led_positive_xx,Led_negative_x,Led_positive_xy,Led_negative_y,Led_positive_xz,Led_negative_z;
                          
String datax,datay,dataz;
void setup() {            // connect Leds at pin numbers 13,12,11,10,8,7
Led_positive_xx=13;      // initalizing leds 
Led_negative_x=12;
Led_positive_xy=11;
Led_negative_y=10;
Led_positive_xz=8;
Led_negative_z=7;
// put your setup code here, to run once:
Serial.begin(9600);     // establish connection

pinMode(Led_positive_xx,OUTPUT);   
pinMode(Led_negative_x,OUTPUT);
pinMode(Led_positive_xy,OUTPUT);
pinMode(Led_negative_y,OUTPUT);
pinMode(Led_positive_xz,OUTPUT);
pinMode(Led_negative_z,OUTPUT);
datax="";
datay="";
dataz="";
xx='0';

}

void loop() {

// put your main code here, to run repeatedly:
if(Serial.available()){
x=Serial.read();
if(x!='\n' && xx=='0'){
datax+=(char)x;
}
else if (x=='\n' && xx=='0'){
mydata=datax.toInt();
if(mydata > 3){          
digitalWrite(Led_positive_xx,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_x,HIGH);
}else {
digitalWrite(Led_positive_xx,LOW);
digitalWrite(Led_negative_x,LOW);
}

datax="";
xx='1';
}
else if (xx=='1' && x!='a'){
datay+=(char)x;

}
else if (x=='a' && xx=='1'){
mydata=datay.toInt();
if(mydata > 3){
digitalWrite(Led_positive_xy,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_y,HIGH);
}else {
digitalWrite(Led_positive_xy,LOW);
digitalWrite(Led_negative_y,LOW);
}

datay="";
xx='2';
}
else if (x!='b' && xx=='2'){
dataz+=(char)x;
}
else if (x=='b' && xx=='2'){
mydata=dataz.toInt();
if(mydata > 3){
digitalWrite(Led_positive_xz,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_z,HIGH);
}else {
digitalWrite(Led_positive_xz,LOW);
digitalWrite(Led_negative_z,LOW);
}
dataz="";
xx='0';
}
}

}

in this code i do not understand the following part which is void loop() . i do understand the statements like this given below because i have the basic knowledge of using bluetooth with arduino and and if,else if statements :slight_smile:

if(Serial.available()){
x=Serial.read();

the part where i have problem:

void loop() {

// put your main code here, to run repeatedly:
if(Serial.available()){
x=Serial.read();
if(x!='\n' && xx=='0'){
datax+=(char)x;
}
else if (x=='\n' && xx=='0'){
mydata=datax.toInt();
if(mydata > 3){          
digitalWrite(Led_positive_xx,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_x,HIGH);
}else {
digitalWrite(Led_positive_xx,LOW);
digitalWrite(Led_negative_x,LOW);
}

datax="";
xx='1';
}
else if (xx=='1' && x!='a'){
datay+=(char)x;

}
else if (x=='a' && xx=='1'){
mydata=datay.toInt();
if(mydata > 3){
digitalWrite(Led_positive_xy,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_y,HIGH);
}else {
digitalWrite(Led_positive_xy,LOW);
digitalWrite(Led_negative_y,LOW);
}

datay="";
xx='2';
}
else if (x!='b' && xx=='2'){
dataz+=(char)x;
}
else if (x=='b' && xx=='2'){
mydata=dataz.toInt();
if(mydata > 3){
digitalWrite(Led_positive_xz,HIGH);
}else if(mydata < -1 ){
digitalWrite(Led_negative_z,HIGH);
}else {
digitalWrite(Led_positive_xz,LOW);
digitalWrite(Led_negative_z,LOW);
}
dataz="";
xx='0';
}
}

If there is a character to read, then read it.

i dont understand u

shantanudestroyer: i dont understand u

I assume you meant " I don't understand you".

So, turn my answer on it's head - why would you read something if there's nothing there to read?

shantanudestroyer:
the part where i have problem:

yes, I find it hard to understand too; in part because it has such poor formatting…

look at the auto-format option in the IDE in order to make your code prettier, and easier to read/understand.

the part where i have problem:

void loop() {

  // put your main code here, to run repeatedly:
  if (Serial.available()) {
    x = Serial.read();
    if (x != '\n' && xx == '0') {
      datax += (char)x;
    }
    else if (x == '\n' && xx == '0') {
      mydata = datax.toInt();
      if (mydata > 3) {
        digitalWrite(Led_positive_xx, HIGH);
      } else if (mydata < -1 ) {
        digitalWrite(Led_negative_x, HIGH);
      } else {
        digitalWrite(Led_positive_xx, LOW);
        digitalWrite(Led_negative_x, LOW);
      }

      datax = "";
      xx = '1';
    }
    else if (xx == '1' && x != 'a') {
      datay += (char)x;

    }
    else if (x == 'a' && xx == '1') {
      mydata = datay.toInt();
      if (mydata > 3) {
        digitalWrite(Led_positive_xy, HIGH);
      } else if (mydata < -1 ) {
        digitalWrite(Led_negative_y, HIGH);
      } else {
        digitalWrite(Led_positive_xy, LOW);
        digitalWrite(Led_negative_y, LOW);
      }

      datay = "";
      xx = '2';
    }
    else if (x != 'b' && xx == '2') {
      dataz += (char)x;
    }
    else if (x == 'b' && xx == '2') {
      mydata = dataz.toInt();
      if (mydata > 3) {
        digitalWrite(Led_positive_xz, HIGH);
      } else if (mydata < -1 ) {
        digitalWrite(Led_negative_z, HIGH);
      } else {
        digitalWrite(Led_positive_xz, LOW);
        digitalWrite(Led_negative_z, LOW);
      }
      dataz = "";
      xx = '0';
    }
  }

}

shantanudestroyer: the part where i have problem:

that's the whole program!!

start with going through the tutorials in the IDE and look at some examples on how to parse Serial input.

A tutorial is supposed to help people learn. If it baffles you, move on.

You said you understand quite a few statements. Then you post a load of code containing mainly those things that you claim to understand and say you don't understand it.

That code is a poor tutorial because the variables mainly have unhelpful names like x and xx and it is lacking any useful comments. So why not go through and at least add comments explaining what the bits you understand are doing then put in some questions against the bits you really don't understand. Then we might have something to work with.

Steve