Muhammad Anas
Published

Two Mode Robot Controlling through Android and Windowsphone8

Basically A Car which is controlled by 2 Modes: Accelerometer Sensor of the phone, and Autonomous (Moves in path and avoids object)

IntermediateFull instructions provided21,684
Two Mode Robot Controlling through Android and Windowsphone8

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
Can also use other versions such as Mega
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
Bluetooth Module or any other similar model
×1
HC-SR04 Ultrasonic Sensor
×1
Servo Motor
to hold the ultrasonic sensor, In my case (Tower-Pro sg90)
×1
9V battery (generic)
9V battery (generic)
Good enough to Supply arduino+bluetooth+ultrasonic , I recommend to get 2-3 of these
×1
Battery (Motor Supply depending on your motors)
Depends on your motor rating , A greater mAh means better speed at some load. I used LIPO 7.2V 1500mAh which is rechargable
×1
Male/Male Jumper Wires
Need plenty of these for connections
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
GoPiGo Robot Base Kit
Dexter Industries GoPiGo Robot Base Kit
Any robot chasis will work, In my case I made my own from Aluminium sheet and attached 2 DC motors.
×1

Software apps and online services

RoboControl (WP8)
RoboControl (Android)

Story

Read more

Schematics

Schematic Diagram

The diagram is simple, Starting from the Arduino I have connected 9V battery to give supply to arduino and through it's 5V pinout I'm giving supply to my ultrasonic and bluetooth module. For the HC-05 Bluetooth Module the RXD is connected to Digital pin 11 and TXD of module to Digital pin 10 as per logic the RXD of Module goes into the TXD of Arduino(assigning of pin 11 of arduino as TXD is done in code) and TXD of Module to RXD of Arduino. For the Motor Driver L298N the +ve terminal of Motor's battery is connected to +V of the L298N driver and -ve terminal to Gnd, The driver automatically generates 5V when you connect the battery, The IN1- IN4 pins are used to control the motor directions which are connected to arduino, EN pins are shorted to 5V to enable them and the motors are connected to the terminals labeled A & B. Please Note all the grounds are connected to each other that means they all will be common

Schematic Diagram

The Circuit is simple
Starting from the Arduino I'm using a 9V battery to power the arduino+bluetooth. The +ve terminal of the battery is attached to the Vin pin of arduino and -ve terminal to ground(any of them doesn't matter) of the arduino.

- HC-05 Bluetooth module for which the Vcc of the module is connected to 5V regulator pin of arduino, ground of the module to gnd of arduino, the RXD pin of bluetooth module to Digital pin 11 of arduino and TXD of module to Digital pin 10 of arduino.

-Motor driver L298N which we are powering through another battery , The +ve terminal goes into the +V of the driver and -ve of the battery goes into the gnd of the driver . The driver regulates the voltage and give output of 5V in the 5Vout pin , The IN1-IN4 pins are used for controlling and are connected to arduino digital pins as described in the diagram and finally the motors are connected to Motor A and Motor B pins.

-UltraSonic Sensor , The Vcc is connected to the 5V regulator pin of Motor driver L298N, Ground pin of the sensor to any ground (All the grounds are common so it doesnt matter) , Trig of the sensor to arduino Digital pin 2 and Echo of the sensor to arduino Digital pin 3.

Schematic Diagram

The Diagram is simple

Starting from the 9 V battery
-The positive terminal goes into the Vin of arduino
-The negative of battery goes into the ground

For the Bluetooth Module
- RXD goes into the digital pin 11
- TXD goes into the digital pin 10
- Vcc goes into the arduino 5V pin(to get the 5V for power)
- gnd goes to any of the grounds

For Motor Driver L298N

- The IN pins are for controlling and goes into digital pins of arduino(there order matter otherwise motor will work in opposite direction)
- +V of driver goes to the positive terminal of the other battery
- Ground is connected to negavtive of battery
- Motors are connected in Motor terminals A & B

For the Ultrasonic Sensor

- Vcc goes to the 5V pin of motor driver (to get 5V supply to power sensor)
- ground can be any of them
- Trig and echo pins as accordingly shown in the diagram

Other Notes
More details can be found in the story section and make sure to check all the grounds are connected to each other .

Code

Arduino Code

C/C++
Most of things are explained through comments , you must know C language in order to play with it
#include <SoftwareSerial.h> 
 #include <NewPing.h> //ultrasonic library
#include <Servo.h> //servo library
#define RightmotorF 7  //digitalpin 7 for right motor forward
#define RightmotorB 8  //digital pin 8 for right motor Backward
#define LeftmotorF 5 //digitalpin 5 for left motor forward
#define LeftmotorB 6 //digital pin 6 for right motor Backward
Servo myservo;  
int LeftDistance=0;
int RightDistance=0;
int distance=0;
int val;
String message="";

int FrontDistance=0;
NewPing sonar(2, 3, 400); //(trig,echo,maxdistance)
SoftwareSerial BT(10,11);  //Assigning arduino's (RXD,TXD)
void setup()
{
  // Setup LED

  pinMode(RightmotorF, OUTPUT);//declaring these pins as output to control them
    pinMode(RightmotorB, OUTPUT);
    pinMode(LeftmotorF,OUTPUT);
    pinMode(LeftmotorB,OUTPUT);

  Serial.begin(9600);
 myservo.attach(12);//telling the code that servo is at digital pin 12
  
  BT.begin(115200);
  
  BT.print("$$$");//Bluetooth stuff dont change
 
  delay(100);
  
  BT.println("U,9600,N");
 
  BT.begin(9600);
   serv(512);//setting the servo at initial position (Change this accordingly)
}

void loop()
{
 
 
  
  if(BT.available())
  {
 
  //Through comparing characters we will communicate, you can change this in app too
    char GetBT = (char)BT.read();
 
  
       if(GetBT=='d'){digitalWrite(RightmotorF, HIGH);//for forward movement both R  & L forwards are high and backward low
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW); } 
   else if(GetBT=='s'){ digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);} 
  else if(GetBT=='a'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH); }
  else if(GetBT=='g'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);}
   else if(GetBT=='f'){ digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
   } 
  
     
  if(GetBT=='o')
   {
     while(1)
    {
      
     
      scan();//checking the distance
   
      FrontDistance=distance;
      
     // if(FrontDistance>20|| FrontDistance==0)
        if(FrontDistance>20)
      {
      
       Forward(); 
      }
      else
       {
        movestop();
        navigate();
       
         
         
      }
        
    
    if(BT.available())
    {
          char newBT = (char)BT.read();
        
        if(newBT=='t'){movestop();break;}
    }
     }
   
   
  
   }
   else if(GetBT=='k')
   {
     scan();
   //  Serial.println(distance);
     BT.print(distance);
   }
   
   
    
   }
    
   
 
    

  

 

   
    
    
  
  
  if(Serial.available())
  {
 //this part can be used to test through serial monitor for the motors
    char al=(char)Serial.read();
      
       //  int uS = sonar.ping();
    //   Serial.print("Ping: ");
    //   Serial.print(uS / US_ROUNDTRIP_CM);
     //  Serial.println("cm");
     if(al=='d'){ digitalWrite(RightmotorF, HIGH);
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);} 
   else if(al=='s'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);} 
  else if(al=='a'){digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);  }
  else if(al=='g'){ digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);}
   else if(al=='f'){ digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
      
   
   }
   
    
//    Serial.print(al);
   
   
   
  
    
  }
 
 
}
void serv(int a)
{
    val=map(a,0,1023,0,179);
  myservo.write(val);
  delay(1000);
}
void Forward()
{
  digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
}
void Backward()
{
 
        digitalWrite(RightmotorF, HIGH);
        digitalWrite(RightmotorB,  LOW); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
}
void Right()
{
  digitalWrite(RightmotorF, HIGH);
     digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,HIGH);
}
void Left()
{
  
     digitalWrite(RightmotorF, LOW);
       digitalWrite(RightmotorB, HIGH); digitalWrite(LeftmotorF,HIGH); digitalWrite(LeftmotorB,LOW);
}  
void movestop()
{
  digitalWrite(RightmotorF, LOW);
        digitalWrite(RightmotorB, LOW); digitalWrite(LeftmotorF,LOW); digitalWrite(LeftmotorB,LOW);
   
}
void scan()
{
 int uS = sonar.ping();
      
  distance=(uS / US_ROUNDTRIP_CM); 
 delay(500);
}

  void navigate()
{
   
    serv(1023);                                //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
                                          //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
     LeftDistance = distance;                          //Set the variable LeftDistance to the distance on the left
    
 
   serv(10);                                  //Move the servo to the right
                                        //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
    RightDistance = distance;                         //Set the variable RightDistance to the distance on the right
    
    
    if(abs(RightDistance - LeftDistance) < 5)
    {
      Backward();                                  //Go to the moveBackward function
      delay(200);                                      //Pause the program for 200 milliseconds to let the robot reverse
     Right();                                     //Go to the moveRight function
      delay(100);       //Pause the program for 200 milliseconds to let the robot turn right
   serv(512);
  }
    else if(RightDistance < LeftDistance)                  //If the distance on the right is less than that on the left then...
    {
    Left();                                      //Go to the moveLeft function
     delay(100);       //Pause the program for half a second to let the robot turn
   serv(512);
  }
    else if(LeftDistance < RightDistance)             //Else if the distance on the left is less than that on the right then...
    {
     Right();                                     //Go to the moveRight function
     delay(100);                                      //Pause the program for half a second to let the robot turn
   serv(512);
  }
}

Credits

Muhammad Anas

Muhammad Anas

6 projects • 36 followers

Comments