Help improve this code

I am working on a small robot that would have 3 wheels 2 dc motors on the back for steering and a wheel in frint just for balance. I am using arduino uno with the adafruit motor shield and hc-05 and i have made a code.When i connect the robot to the computer with the cable and input the commands everything works fine but when i try to do the same with my mobile phone via bluetooth and an android application nothing happens i also tried using bluetooth adapter on the pc and connecting that way but i came across same results. The mobile application basicaly just sends characters to the arduino to control it. Can someone help me fix the problem here is the code

#include <AFMotor.h>

AF_DCMotor desniMotor ( 1 ); // right dc motor
AF_DCMotor lijeviMotor ( 2 ); //left dc motor


char dataIn = 'S';        //data comming from the phone
char determinant;         //used in the check function
char det;                 //used in the loop function
int velocity = 0;

void setup()  
{        
Serial.begin(9600);  

}

void loop()
{  
    det = check();
      while (det == 'F')   //if incoming data is a F, move foward
      {   
          desniMotor.run (FORWARD); //right motor moves forward 
          desniMotor.setSpeed (velocity); // rotational speed of the motor
          lijeviMotor.run (FORWARD); // -||-
          lijeviMotor.setSpeed (velocity); // -||-
          
          det = check();           
      }   
      while (det == 'B')   //if incoming data is a B, move back
      {     
          desniMotor.run (BACKWARD); //
          desniMotor.setSpeed (velocity); // 
          lijeviMotor.run (BACKWARD); // -||-
          lijeviMotor.setSpeed (velocity); //-||-         
          det = check();           
      }  

      while (det == 'L')   //if incoming data is a L, move wheels left
      {      
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);        
          det = check();           
      }   
      while (det == 'R')   //if incoming data is a R, move wheels right
      {     
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);         
          det = check();           
      }
      
      while (det == 'I')   //if incoming data is a I, turn right forward
      {      
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);         
          det = check();   
      }   
      while (det == 'J')   //if incoming data is a J, turn right back
      {       
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);         
          det = check();        
      }           
      while (det == 'G')   //if incoming data is a G, turn left forward
      {  
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);         
          det = check();         
      }     
      while (det == 'H')   //if incoming data is a H, turn left back
      {
           desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);         
          det = check();                                               
      }    
      while (det == 'S')   //if incoming data is a S, stop
      {
          desniMotor.run (RELEASE);
          lijeviMotor.run (RELEASE);        
          det = check();  
      }
}

int check()
{
  if (Serial.available() > 0)    //Check for data on the serial lines.
  {    
    dataIn = Serial.read();  //Get the character sent by the phone and store it in 'dataIn'.
        if (dataIn == 'F')
        {      
          determinant = 'F';
        }   
        else if (dataIn == 'B')
        {  
          determinant = 'B';  
        }
        else if (dataIn == 'L')   
        {  
          determinant = 'L';
        }
        else if (dataIn == 'R')   
        {  
          determinant = 'R';
        }  
        else if (dataIn == 'I')   
        {  
          determinant = 'I';  
        }   
        else if (dataIn == 'J')   
        {   
          determinant = 'J';
        }           
        else if (dataIn == 'G')  
        {
          determinant = 'G';  
        }     
        else if (dataIn == 'H')   
        {
          determinant = 'H';  
        }    
        else if (dataIn == 'S')  
        {
          determinant = 'S';
        }
        else if (dataIn == '0')  
        {
          velocity = 0;  
        }
        else if (dataIn == '1')  
        {
          velocity = 25;
        }
        else if (dataIn == '2')  
        {
          velocity = 50;
        }
        else if (dataIn == '3')  
        {
          velocity = 75;
        }
        else if (dataIn == '4')  
        {
          velocity = 100;
        }
        else if (dataIn == '5')  
        {
          velocity = 125;
        }
        else if (dataIn == '6')  
        {
          velocity = 150;
        }
        else if (dataIn == '7')  
        {
          velocity = 175;
        }
        else if (dataIn == '8')  
        {
          velocity = 200;
        }
        else if (dataIn == '9')  
        {
          velocity = 225;
        }
        else if (dataIn == 'q')  
        {
          velocity = 255;
        }        
       
  }
return determinant;
}

Check your Bluetooth modules default baud rate. is it 9600? Check the module using a direct serial interface and and read the received code for each character you sent by the software to verify is there any extra characters coming with each frame.

step 1 : remove all the while loops and do the checking in one place

void loop()
{  
    det = check();
      if (det == 'F')   //if incoming data is a F, move foward
      {   
          desniMotor.run (FORWARD); //right motor moves forward 
          desniMotor.setSpeed (velocity); // rotational speed of the motor
          lijeviMotor.run (FORWARD); // -||-
          lijeviMotor.setSpeed (velocity); // -||-
      }   
      else if (det == 'B')   //if incoming data is a B, move back
      {     
          desniMotor.run (BACKWARD); //
          desniMotor.setSpeed (velocity); // 
          lijeviMotor.run (BACKWARD); // -||-
          lijeviMotor.setSpeed (velocity); //-||-         
      }  
      else if (det == 'L')   //if incoming data is a L, move wheels left
      {      
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);                
      }   
      else if (det == 'R')   //if incoming data is a R, move wheels right
      {     
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);         
      }
      else if (det == 'I')   //if incoming data is a I, turn right forward
      {      
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);         
      }   
      else if (det == 'J')   //if incoming data is a J, turn right back
      {       
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);                
      }           
      else if (det == 'G')   //if incoming data is a G, turn left forward
      {  
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);             
      }     
      else if (det == 'H')   //if incoming data is a H, turn left back
      {
           desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);                                                  
      }    
      else if (det == 'S')   //if incoming data is a S, stop
      {
          desniMotor.run (RELEASE);
          lijeviMotor.run (RELEASE);         
      }
}

step 2: replace the motorcalls with one parameterized function

void loop()
{  
    det = check();
      if (det == 'F')   //if incoming data is a F, move foward
      {   
          action(FORWARD, velocity, FORWARD, velocity);
      }   
      else if (det == 'B')   //if incoming data is a B, move back
      {     
           action(BACKWARD, velocity, BACKWARD, velocity);
      }  
      else if (det == 'L')   //if incoming data is a L, move wheels left
      {      
          action(FORWARD, velocity, BACKWARD, velocity);            
      }   
      else if (det == 'R')   //if incoming data is a R, move wheels right
      {     
          action(BACKWARD, velocity, FORWARD, velocity);     
      }
      else if (det == 'I')   //if incoming data is a I, turn right forward
      {      
          action(FORWARD, velocity, FORWARD, velocity);      
      }   
      else if (det == 'J')   //if incoming data is a J, turn right back
      {       
           action(BACKWARD, velocity, BACKWARD, velocity);               
      }           
      else if (det == 'G')   //if incoming data is a G, turn left forward
      {  
           action(BACKWARD, velocity, BACKWARD, velocity);   
      }     
      else if (det == 'H')   //if incoming data is a H, turn left back
      {
           action(BACKWARD, velocity, BACKWARD, velocity);                                                
      }    
      else if (det == 'S')   //if incoming data is a S, stop
      {
           action(RELEASE, 0, RELEASE, 0);
      }
}

void action( int directionRight, int speedRight, int directionLeft, int speedLeft )
{
          desniMotor.run (directionRight); 
          desniMotor.setSpeed (speedRight);
          lijeviMotor.run (directionLeft);
          lijeviMotor.setSpeed (speedLeft );
}

Note not all calls correctly

Step 3: remove unneeded comments and braces

void loop()
{  
      det = check();
      if (det == 'F')   action(FORWARD, velocity, FORWARD, velocity);
      else if (det == 'B') action(BACKWARD, velocity, BACKWARD, velocity);
      else if (det == 'L') action(FORWARD, velocity, BACKWARD, velocity);            
      else if (det == 'R') action(BACKWARD, velocity, FORWARD, velocity);     
      else if (det == 'I') action(FORWARD, velocity, FORWARD, velocity);      
      else if (det == 'J') action(BACKWARD, velocity, BACKWARD, velocity);               
      else if (det == 'G') action(FORWARD, velocity, BACKWARD, velocity);   
      else if (det == 'H') action(BACKWARD, velocity, FORWARD, velocity);                                                
      else if (det == 'S') action(RELEASE, 0, RELEASE, 0);
}
  • you could align the code visually to get nice columns of params (easier to check)
  • you could use a "switch case" here instead of the "if else ladder"
  • As the parameter velocity is always equal you can refactor one away (but if you want diff speeds -> don't)
  • ...
    my 2 cents

s_premkumar999:
Check your Bluetooth modules default baud rate. is it 9600? Check the module using a direct serial interface and and read the received code for each character you sent by the software to verify is there any extra characters coming with each frame.

i have checked and it is 9600 i have successfully connected to the modul and it works fine

robtillaart:
step 1 : remove all the while loops and do the checking in one place

void loop()


    det = check();
      if (det == 'F')  //if incoming data is a F, move foward
      { 
          desniMotor.run (FORWARD); //right motor moves forward
          desniMotor.setSpeed (velocity); // rotational speed of the motor
          lijeviMotor.run (FORWARD); // -||-
          lijeviMotor.setSpeed (velocity); // -||-
      } 
      else if (det == 'B')  //if incoming data is a B, move back
      {   
          desniMotor.run (BACKWARD); //
          desniMotor.setSpeed (velocity); //
          lijeviMotor.run (BACKWARD); // -||-
          lijeviMotor.setSpeed (velocity); //-||-       
      } 
      else if (det == 'L')  //if incoming data is a L, move wheels left
      {     
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);               
      } 
      else if (det == 'R')  //if incoming data is a R, move wheels right
      {   
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);       
      }
      else if (det == 'I')  //if incoming data is a I, turn right forward
      {     
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);       
      } 
      else if (det == 'J')  //if incoming data is a J, turn right back
      {     
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);               
      }         
      else if (det == 'G')  //if incoming data is a G, turn left forward
      { 
          desniMotor.run (FORWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (FORWARD);
          lijeviMotor.setSpeed (velocity);           
      }   
      else if (det == 'H')  //if incoming data is a H, turn left back
      {
          desniMotor.run (BACKWARD);
          desniMotor.setSpeed (velocity);
          lijeviMotor.run (BACKWARD);
          lijeviMotor.setSpeed (velocity);                                                 
      }   
      else if (det == 'S')  //if incoming data is a S, stop
      {
          desniMotor.run (RELEASE);
          lijeviMotor.run (RELEASE);       
      }
}




step 2: replace the motorcalls with one parameterized function



void loop()

    det = check();
      if (det == 'F')  //if incoming data is a F, move foward
      { 
          action(FORWARD, velocity, FORWARD, velocity);
      } 
      else if (det == 'B')  //if incoming data is a B, move back
      {   
          action(BACKWARD, velocity, BACKWARD, velocity);
      } 
      else if (det == 'L')  //if incoming data is a L, move wheels left
      {     
          action(FORWARD, velocity, BACKWARD, velocity);           
      } 
      else if (det == 'R')  //if incoming data is a R, move wheels right
      {   
          action(BACKWARD, velocity, FORWARD, velocity);   
      }
      else if (det == 'I')  //if incoming data is a I, turn right forward
      {     
          action(FORWARD, velocity, FORWARD, velocity);     
      } 
      else if (det == 'J')  //if incoming data is a J, turn right back
      {     
          action(BACKWARD, velocity, BACKWARD, velocity);             
      }         
      else if (det == 'G')  //if incoming data is a G, turn left forward
      { 
          action(BACKWARD, velocity, BACKWARD, velocity); 
      }   
      else if (det == 'H')  //if incoming data is a H, turn left back
      {
          action(BACKWARD, velocity, BACKWARD, velocity);                                               
      }   
      else if (det == 'S')  //if incoming data is a S, stop
      {
          action(RELEASE, 0, RELEASE, 0);
      }
}

void action( int directionRight, int speedRight, int directionLeft, int speedLeft )
{
          desniMotor.run (directionRight);
          desniMotor.setSpeed (speedRight);
          lijeviMotor.run (directionLeft);
          lijeviMotor.setSpeed (speedLeft );
}



Note not all calls correctly

Step 3: remove unneeded comments and braces


void loop()

      det = check();
      if (det == 'F')  action(FORWARD, velocity, FORWARD, velocity);
      else if (det == 'B') action(BACKWARD, velocity, BACKWARD, velocity);
      else if (det == 'L') action(FORWARD, velocity, BACKWARD, velocity);           
      else if (det == 'R') action(BACKWARD, velocity, FORWARD, velocity);   
      else if (det == 'I') action(FORWARD, velocity, FORWARD, velocity);     
      else if (det == 'J') action(BACKWARD, velocity, BACKWARD, velocity);             
      else if (det == 'G') action(FORWARD, velocity, BACKWARD, velocity); 
      else if (det == 'H') action(BACKWARD, velocity, FORWARD, velocity);                                               
      else if (det == 'S') action(RELEASE, 0, RELEASE, 0);
}



- you could align the code visually to get nice columns of params (easier to check)
- you could use a "switch case" here instead of the "if else ladder"
- As the parameter velocity is always equal you can refactor one away (but if you want diff speeds -> don't)
- ...
my 2 cents

can you tell me how would i declare action in the scope?

you need to define action() before it is used. (so move it up in the listing or use a separate .cpp file for it)

robtillaart:
you need to define action() before it is used. (so move it up in the listing or use a separate .cpp file for it)

thank you i managed to do it but again i can't get it to work it only works on pc when i connect with cable i wrote another program just to test the modul and it is transmiting fine dont know how to write a code to check if it's receiving data normaly

time to make a minimal application that shows the correct working [or not] of the bluetooth connection

i tryed using simple serial read code but still the same resaults if i hook upbluetooth modul to tx and rx (rx to tx and tx to rx) does it transfer its data through the serial line? can i use Serial.available() to check for data if not can you tell me what can i use to read data from the modul?

Does anyone have any idea what might be the problem?