The Schematic:
Note: in the very very rough schematic, brown -> negative, red -> positive and the battery pack is 3S not 2S.
The Code
#include <IRremote.h>
#include <SoftwareSerial.h>
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
SoftwareSerial BTSerial(10, 11);
#define irPin A3
IRrecv irrecv(irPin);
decode_results rslts;
#define trig_pin A2 //analog input 1
#define echo_pin A1 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
//PWM 3, 5, 6, 9, 10, 11
char c = ' ';
String irNum = "";
String btNum = "";
String receivedData = "";
String mode = "obstacle"; //NRFL01, HC-05, IR, Eyeblink
// mode = "line";
char data;
// Motor A
int enA = 6;
int in1 = 7;
int in2 = 4;
// Motor B
int enB = 5;
int in3 = 3;
int in4 = 2;
void inits() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void setup() {
inits();
//------------------------------------
//----------manual---------------
Serial.begin(9600);
irrecv.enableIRIn();
BTSerial.begin(38400);
Serial.println("Enter AT commands:");
//------------------------------------
//----------obstacle servo---------------
servo_motor.attach(13); //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);
}
void loop() {
//------------------------------------
//----------manual---------------
if (mode == "manual") {
// if (BTSerial.available()) {
// // Serial.write(BTSerial.read()); //writes full words to serial monitor
// btNum = BTSerial.read();
// if (data == "OK") {
// Serial.println("Stopped");
// Stop();
// }
// Serial.println(btNum);
// }
// Check if data is available from the HC-05 module
while (BTSerial.available()) {
// Read a byte from the software serial port
char incomingByte = BTSerial.read();
// Append the byte to the string
if (isalnum(incomingByte))
receivedData += incomingByte;
}
// If data has been received, print it and clear the string
if (receivedData.length() > 0) {
Serial.print("Received data: ");
Serial.println(receivedData.length());
// move (receivedData);
btNum = receivedData;
// Clear the string after processing
receivedData = "";
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available()) {
BTSerial.write(Serial.read());
}
if (irrecv.decode()) {
Serial.println(rslts.value);
irNum = String(irrecv.decodedIRData.command, HEX);
Serial.println(irrecv.decodedIRData.command, HEX);
Serial.println(irNum);
irrecv.resume();
}
// Add a small delay to avoid overwhelming the serial buffer
delay(50);
if (irNum == "44" || btNum == "T")
Forward();
else if (irNum == "1d" || btNum == "B")
Back();
else if (irNum == "1c" || btNum == "L")
Left();
else if (irNum == "48" || btNum == "R")
Right();
else if (irNum == "5c" || btNum == "OK" || btNum == "O" || btNum == "K")
Stop();
}
//------------------------------------
//----------obstacle servo---------------
else if (mode == "obstacle") {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceLeft = lookLeft();
delay(300);
distanceRight = lookRight();
delay(300);
if (distance >= distanceLeft) {
turnRight();
moveStop();
} else {
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = readPing();
}
// else if (mode == "line") {
// }
// }
}
//------------------------------------
//----------manual---------------
void Forward() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 150);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
}
void Back() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 150);
}
void Right() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
}
void Left() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
}
void Stop() {
analogWrite(enA, 0);
analogWrite(enB, 0);
// digitalWrite(in1, LOW);
// digitalWrite(in2, LOW);
// digitalWrite(in3, LOW);
// digitalWrite(in4, LOW);
}
//------------------------------------
//----------obstacle servo---------------
int lookRight() {
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int lookLeft() {
servo_motor.write(180);
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(in3, LOW);
digitalWrite(in1, LOW);
digitalWrite(in4, LOW);
digitalWrite(in2, LOW);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 150);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
}
}
void moveBackward() {
goesForward = false;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 150);
}
void turnRight() {
//turning right little
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
delay(500);
//moving backward little
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 150);
}
void turnLeft() {
//turning left little
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enB, 150);
delay(500);
//moving backward little
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 150);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 150);
}