#include SoftwareSerial BT_Serial(2, 3); // RX, TX #define enA 6//Enable1 Pin enA #define in1 7 //Motor1 Pin in1 #define in4 8 //Motor2 Pin in1 #define enB 9 //Enable2 Pin enB #define R_S A1 //ir sensor Right #define L_S A0 //ir sensor Left int bt_data; // variable to receive data from the serial port int Speed = 0; //pwm motors int num = 350; // sensor value that limits the color white from black int mode=0; void setup(){ pinMode(R_S, INPUT); // declare if sensor as input pinMode(L_S, INPUT); // declare ir sensor as input pinMode(enA, OUTPUT); // declare as output for Pin enA pinMode(in1, OUTPUT); // declare as output for Pin in1 pinMode(in4, OUTPUT); // declare as output for Pin in4 pinMode(enB, OUTPUT); // declare as output for Pin enB Serial.begin(9600); // start serial communication at 9600bps BT_Serial.begin(9600); delay(500); } void loop(){ if(BT_Serial.available() > 0){ //if some date is sent, reads it and saves in state bt_data = BT_Serial.read(); if(bt_data > 20){Speed = bt_data;} } if(bt_data == 8){mode=1; Speed=100;} //Auto Line Follower Command else if(bt_data == 9){mode=0; Stop(0);} //Manual Android Application Control Command if(mode==0){ //=============================================================================== // Key Control Command //=============================================================================== if(bt_data == 1){forword(Speed); } // if the bt_data is '1' the DC motor will go forward else if(bt_data == 2){backword(Speed);} // if the bt_data is '2' the motor will Reverse else if(bt_data == 3){turnLeft(Speed);} // if the bt_data is '3' the motor will turn left else if(bt_data == 4){turnRight(Speed);} // if the bt_data is '4' the motor will turn right else if(bt_data == 5){Stop(0); } // if the bt_data '5' the motor will Stop }else{ //=============================================================================== // Line Follower Control //=============================================================================== if((analogRead(R_S) < num)&&(analogRead(L_S) < num)){forword(Speed);} //if Right Sensor and Left Sensor are at White color then it will call forword function if((analogRead(R_S) >= num)&&(analogRead(L_S) < num)){turnRight(Speed);}//if Right Sensor is Black and Left Sensor is White then it will call turn Right function if((analogRead(R_S) < num)&&(analogRead(L_S) >= num)){turnLeft(Speed);} //if Right Sensor is White and Left Sensor is Black then it will call turn Left function if((analogRead(R_S) >= num)&&(analogRead(L_S) >= num)){Stop(0);} //if Right Sensor and Left Sensor are at Black color then it will call Stop function } delay(10); } //=========================================================================== // movements functions //=========================================================================== void forword(int s){ //forword analogWrite(enA, s); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB, s); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed digitalWrite(in1, HIGH); //Right Motor forword Pin digitalWrite(in4, HIGH); //Left Motor forword Pin } void backword( int s){ //backword analogWrite(enA, s); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB, s); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed digitalWrite(in1, LOW); //Right Motor forword Pin digitalWrite(in4, LOW); //Left Motor forword Pin } void turnRight(int s){ //turnRight analogWrite(enA, s); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB, s); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed digitalWrite(in1, LOW); //Right Motor forword Pin digitalWrite(in4, HIGH); //Left Motor forword Pin } void turnLeft(int s){ //turnLeftç analogWrite(enA,s); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB,s); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed digitalWrite(in1, HIGH); //Right Motor forword Pin digitalWrite(in4, LOW); //Left Motor forword Pin } void Stop(int s){ //stop analogWrite(enA, s); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB, s); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed digitalWrite(in1, LOW); //Right Motor forword Pin digitalWrite(in4, LOW); //Left Motor forword Pin }