
//SOCCER robot using android 
//___________________________
//andriod sends list of 7 2bytes data.
//1st 2byte data corresponds to kick.if we are shaking mobile then mobile will send '1'.if the we are not shaking the mobile then mobile sends '0'
//2nd 2byte data corresponds to sign of accelerometer 'x' value.(if x is +ve mobile sends '0',if x is -ve mobile sends '1')
//3rd 2byte data is accelerometer 'x' value (in general it is floating value .but mobile won't send floating value,it just sends integer part,'x' value is in range of '0' to '9')
//4th 2byte data corresponds to sign of accelerometer 'y' value.(if x is +ve mobile sends '0',if x is -ve mobile sends '1')
//5th 2byte data is accelerometer 'y' value (in general it is floating value .but mobile won't send floating value,it just sends integer part,'y' value is in range of '0' to '9')
//6th 2byte data corresponds to sign of accelerometer z value.(if x is +ve mobile sends '0',if x is -ve mobile sends '1')
//7th 2byte data is accelerometer 'z' value (in general it is floating value .but mobile won't send floating value,it just sends integer part,'z' value is in range of '0' to '9')
//___________________________________________________________________________________________________________________________________________________________________________________
//                                                                                             -by K.V.S.PHANI.ROHITH
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(8,9);// RX, TX
Servo myServo; //create Servo object to control a servo 
int servoPin = 10; //pin at which servo's logic pin is connected to arduino
int Kick = 170;//50 degree is the sevros position after kicking
int neutral = 50; //170 degree is the servo's postion intialy and finaly after kicking
long Wait = 750;
int xsign;// establish variables for reading received 2 byte data
int x;// establish variables for reading received 2 byte data
int ysign;// establish variables for reading received 2 byte data
int y;// establish variables for reading received 2 byte data
int zsign;// establish variables for reading received 2 byte data
int z;// establish variables for reading received 2 byte data
int kick=0;// establish variables for reading received 2 byte data
int m1f=2;
int m1b=4;
int m2f=5;
int m2b=7;
int m1=3;
int m2=6;
void setup()  
{ 
   myServo.attach(servoPin);// attaches the servo on pin 9 to the servo object
   pinMode(m1f, OUTPUT);//set mlf pin as output pin
  pinMode(m1b, OUTPUT);//set mlb pin as output pin
  pinMode(m2f, OUTPUT);//set m2f pin as output pin
  pinMode(m2b, OUTPUT);//set m2b pin as output pin
  myServo.write(neutral);//set servo position to 170 degrees
  mySerial.begin(57600); // set the data rate for the SoftwareSerial port
}
void loop() // run over and over
{
  
  if (mySerial.available())//if number of bytes available to read greater than 0
  {
    kick=mySerial.read();// read the incoming byte:
    xsign=mySerial.read();// read the incoming byte:
    x=mySerial.read();// read the incoming byte:
    ysign=mySerial.read();// read the incoming byte:
    y=mySerial.read();// read the incoming byte:
    zsign=mySerial.read();// read the incoming byte:
    z=mySerial.read();// read the incoming byte:
    
      if(x>2&&xsign==1&&kick==0)
      {
         if(y>3&&ysign==0&&kick==0)
         {
         frontright(y);
         }
         else if(y>3&&ysign==1&&kick==0)
        {
        frontleft(y);
        }
        else 
        {
          moveforward(x);
        }
      }
      else if(x>2&&xsign==0&&kick==0)
      {
        if(y>3&&ysign==0&&kick==0)
        {
         backright(y);
        }
        else if(y>3&&ysign==1&&kick==0)
        {
        backleft(y);
        }
        else
        {
        moveback(x);
        }
     }
     else
     {
        stops();
     }
   
    if(kick==1)
    {
    kicks();
    }
  }
  
}


 void kicks()
 {
  mySerial.print("kicked!!");
  myServo.write(Kick);// sets the servo position according to the kick value.we can change kick value. 
  delay(Wait);
  myServo.write(neutral);// sets the servo position according to the neutral value.we can change neutral value.
  delay(500);
 }
  
   void moveforward(int a)//y values are passed from the function call
 {
   int x=map(a,2,10,120,255);
    digitalWrite(m1f,LOW);
    digitalWrite(m2f,LOW);
    digitalWrite(m1b,HIGH);
    digitalWrite(m2b,HIGH);
    analogWrite(m1,x);
    analogWrite(m2,x);
    mySerial.print("Moving Forward");//prints "moving forward" below in the "robot status" label in the app
   
 }
 void moveback(int a)//y values are passed from the function call
 { 
   int x=map(a,3,10,120,255);
    digitalWrite(m1f,HIGH);
    digitalWrite(m2f,HIGH);
    digitalWrite(m1b,LOW);
    digitalWrite(m2b,LOW);
    analogWrite(m1, x);
    analogWrite(m2, x);
    mySerial.print("Moving Back");//prints "moving back" below in the "robot status" label in the app
   
 }
 void frontleft(int a)//y values are passed from the function call
 {
    int x=map(a,3,10,120,255);
    digitalWrite(m1f,LOW);
    digitalWrite(m2f,LOW);
    digitalWrite(m1b,LOW);
    digitalWrite(m2b,HIGH);
    analogWrite(m1,0);
    analogWrite(m2,x);
    mySerial.println("Turning front left");//prints "Turning left" below in the "robot status" label in the app
   
 }
 void frontright(int a)//y values are passed from the function call
 {
   int x=map(a,3,10,120,255);
   digitalWrite(m1f,LOW);
   digitalWrite(m2f,LOW);
   digitalWrite(m1b,HIGH);
   digitalWrite(m2b,LOW);
   analogWrite(m1,x);
   analogWrite(m2,0);
   mySerial.println("Turning front Right");//prints "Turning right" below in the "robot status" label in the app
   
 }
   
    void backleft(int a)//y values are passed from the function call
 {
    int x=map(a,3,10,120,255);
    digitalWrite(m1f,LOW);
    digitalWrite(m2f,HIGH);
    digitalWrite(m1b,LOW);
    digitalWrite(m2b,LOW);
    analogWrite(m1,0);
    analogWrite(m2,x);
    mySerial.println("Turning back left");//prints "Turning left" below in the "robot status" label in the app
    
 }
 void backright(int a)//y values  are passed from the function call
 {
    int x=map(a,3,10,120,255);
   digitalWrite(m1f,HIGH);
   digitalWrite(m2f,LOW);
   digitalWrite(m1b,LOW);
   digitalWrite(m2b,LOW);
   analogWrite(m1,x);
   analogWrite(m2,0);
   mySerial.println("Turning back Right");//prints "Turning right" below in the "robot status" label in the app
  
 }
   
 void stops()//no values are passed from the function call
 {
   digitalWrite(m1f,LOW);
   digitalWrite(m2f,LOW);
   digitalWrite(m1b,LOW);
   digitalWrite(m2b,LOW);
   analogWrite(m1, 0);
   analogWrite(m2,0); 
   mySerial.println("stopped");//prints"stopped" below in the "robot status" label in the app
 
 }
