#include //Servo motor library. This is standard library #include //Ultrasonic sensor function library. You must install this library #define in1 7 //L298n Motor Driver pins for Bluetooth control #define in2 6 #define in3 4 #define in4 5 #define mode 8 int modeState = 0; //L298N control pins for Obstacle avoiding const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 4; const int RightMotorBackward = 5; //HC-SR04 sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2 //*********************Bluetooth Variables*************************// int command; //Int to store app command state. int Speed = 230; // 0 - 255. int Speedsec; int buttonState = 0; int lastButtonState = 0; int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed. int brakeTime = 45; int brkonoff = 1; //1 for the electronic braking system, 0 for normal. //variable for HC-05 char databtt = 0; //Variable for storing received data from HC 05 //*****************************************************************// #define maximum_distance 200 boolean goesForward = false; int distance = 100; NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function Servo servo_motor; // servo name #define LED 13 #define modeLED 2 //indicates the mode of the Robot void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(mode,INPUT); //***************************************************************// pinMode(RightMotorForward, OUTPUT); pinMode(LeftMotorForward, OUTPUT); pinMode(LeftMotorBackward, OUTPUT); pinMode(RightMotorBackward, OUTPUT); servo_motor.attach(9); //our servo pin servo_motor.write(115); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); //*****************************************************************// Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission pinMode(LED, OUTPUT); pinMode(modeLED, OUTPUT); //Sets digital pin 13 as output pin for the robot mode indicator } void loop() { // read the state of the pushbutton value to change the mode modeState = digitalRead(mode); databtt = Serial.read(); //Read the incoming data from the HC 05 and store it into variable data Serial.print(databtt); //Print Value inside data in Serial monitor Serial.print("\n"); //New line if(modeState == HIGH ) //Checks whether value of data from HC 05 is equal to 1 { digitalWrite(12, HIGH); //If value is HIGH then LED turns ON and the robot switches to Self driving mode //code for the Bluetooth Controlled Bot if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { case 'F': forward(); break; case 'B': back(); break; case 'L': left(); break; case 'R': right(); break; case 'G': forwardleft(); break; case 'I': forwardright(); break; case 'H': backleft(); break; case 'J': backright(); break; case '0': Speed = 100; break; case '1': Speed = 140; break; case '2': Speed = 153; break; case '3': Speed = 165; break; case '4': Speed = 178; break; case '5': Speed = 191; break; case '6': Speed = 204; break; case '7': Speed = 216; break; case '8': Speed = 229; break; case '9': Speed = 242; break; case 'q': Speed = 255; break; } Speedsec = Turnradius; if (brkonoff == 1) { brakeOn(); } else { brakeOff(); } } } //end of bluetooth controlled Bot else if(modeState == LOW) //Checks whether value of the switch is LOW { digitalWrite(12, LOW); //If value is 0 then LED turns OFF and the robot switches to Line Following mode //code for the Obstacle Avoiding Bot int distanceRight = 0; int distanceLeft = 0; delay(50); if (distance <= 15){ moveStop(); delay(300); moveBackward(); delay(400); moveStop(); delay(300); distanceRight = lookRight(); delay(300); distanceLeft = lookLeft(); delay(300); if (distance >= distanceLeft){ turnRight(); moveStop(); } else{ turnLeft(); moveStop(); } } else{ moveForward(); } distance = readPing(); }//End of obstacle avoiding code }//end of loop body //***************************************************************// void forward() { analogWrite(in1, Speed); analogWrite(in3, Speed); } void back() { analogWrite(in2, Speed); analogWrite(in4, Speed); } void left() { analogWrite(in3, Speed); analogWrite(in2, Speed); } void right() { analogWrite(in4, Speed); analogWrite(in1, Speed); } void forwardleft() { analogWrite(in1, Speedsec); analogWrite(in3, Speed); } void forwardright() { analogWrite(in1, Speed); analogWrite(in3, Speedsec); } void backright() { analogWrite(in2, Speed); analogWrite(in4, Speedsec); } void backleft() { analogWrite(in2, Speedsec); analogWrite(in4, Speed); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); } void brakeOn() { //Here's the future use: an electronic braking system! // read the pushbutton input pin: buttonState = command; // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == 'S') { if (lastButtonState != buttonState) { digitalWrite(in1, HIGH); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, HIGH); delay(brakeTime); Stop(); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState; } } void brakeOff() { } //****************************************************************// int lookRight(){ servo_motor.write(50); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); return distance; } int lookLeft(){ servo_motor.write(170); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); return distance; delay(100); } int readPing(){ delay(70); int cm = sonar.ping_cm(); if (cm==0){ cm=250; } return cm; } void moveStop(){ digitalWrite(RightMotorForward, LOW); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); digitalWrite(LeftMotorBackward, LOW); } void moveForward(){ if(!goesForward){ goesForward=true; digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } } void moveBackward(){ goesForward=false; digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorForward, LOW); } void turnRight(){ digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorForward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } void turnLeft(){ digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } //*****************************************************************//