Multi-function Smart Car Manual

图片

1.	Introduction
Multi-function smart car uses an MCU development application system and a 4-wheel driver system, the core of which is arduino Atmega-328P.It can realize multifunction,such as tracking,obstacle avoidance,remote control and infrared remote control.
This kit contains a mass of interesting code,and extensible external circuit module to make the smart car.It is of great fun to keep away form boring theory knowledge and gain ability of development MCU system during the project making process.

2.	Functions
•	Ultrasonic Obstacle Avoidance
•	Line Tracking (in a black line) 
•	Infrared Remote Control
•	Bluetooth remote control

3.	Parameters
Motor Voltage: 6V
Motor Reduction Ratio: 1 to 48
 Motor driver module is L298N.
Three line tracking modules, detecting black and white line, high precision, can also be used in fall prevention.
Infrared remote module makes up the remote control system.
It can connect to many other sensor ,so it can complete many other function as you have in your mind.
Car length: 23cm; width: 12cm.
 Can be used under external 7 ~ 12V voltage, also with various sensor modules to realize various functions.

4.	Component List
1.	Gear Motor *4
2.	High-quality Tires *4
3.	Motor Fixing Part *4
3.	100 * 213 * 5MM Acrylic Board *1
4.	100 * 213 * 5MM Acrylic Board *1
5.	L298N Motor Driver Board *1
6.	ARDUINO UNO328 Mother Board *1
7.	ARDUINO Sensor Shield *1
8.	Holder*1
9.	Servo Motor *1
10.	Ultrasonic Sensor Module *1
11.	Three-channel Line Tracking Module
12.	IR Receiver Sensor
13.	MCU Remote Control
14.	Mini Breadboard *1
15.	6-cell AA Battery Case*1
16.	Dupont Wire *30
17.	18650 Charger *1
18.	1M USB Cable
19.	35MM Copper Bush *3
20.	20MM Copper Bush *2
21.	6MM Copper Bush *6
22.	3MM Screws & nuts *several

5. Installation Instructions
1.	Install the Motor
1)	Use two screws with two nuts to attach a fixing part to the motor. Tighten the nuts snugly but take care not to stress the plastic motor housing.

3) plug a tire into the motor.

2.	Install Motor Driver Board

Attach the motor driver board to chassis lower board with 3*20MM screws and nuts.

3.	Install Mother Board

Mount the mother board on the upper chassis with copper bushes, screws and nuts.

Then plug the sensor shield into the mother board.

4.	Install Battery Case

Fix the battery case to the upper chassis with screws and nuts.

5.	Install IR Receiver Sensor

Fix the IR receiver sensor to the upper board in front of mother board
using a screw and nut.

6.	Install Three Tracking Module

7.	Install Holder and Servo Motor

图片

6. Application of Arduino
1.	Introduction

Arduino is an open-source hardware project platform from Italy. This platform includes a circuit board with simple I/O function and program development environment software. It can be used to develop interactive products. For example, it can read signals of multiple switches and sensors, and control light, servo motor and other various physical devices. It can also be developed into PC peripherals to communicate with the software in PC.

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. To properly define Arduino, we can use some examples.
When the coffee is ready, do you want the coffee pot to give you a "squeak" sound for reminder?
When there is a new e-mail, will your cellphone inform you of a mail alert? Do you want a shiny fluffy toy?
Professor X's wheelchair with voice function and beverage distribution function? A buzzer that can do experimental test with a quick button?
Do you want to build your son an arm-cannon from Galaxy Warrior? DIY heart rates monitor or record cycling data into memory card?
Have you ever thought about making a robot that can draw and can wildly run in the snow?

Well, Arduino can help you realize all.

Arduino can convert analog input into digital input. That is to say, Arduino can identify analog input of sensors such as photo sensor, temperature, sound or any low-cost sensor in the market. For digital sensors, Arduino supports SPI (high-speed synchronous serial port) and I2C bus. This function makes it suitable for 99% sensors in the market. Other development platform cannot easily achieve this.

Traditional mother boards are often complicated and with a lot of accessories such as LCD screen, button, LEDs, 7-segment LED display etc. Mother

board has all these functions. The number of functions on an Arduino is an absolute minimum. If you want to add some functions, you can simply add an shield. There are hundreds of thousands of Arduino shield, from LCD screens to wireless
Internet technology. It's easy to increase functions on a shield, and profitable for people who make it.

Not made by chip makers
Arduino development board is not designed by chip makers. Why do we emphasize this? Chip makers add strange things to their products to make them special. While Arduino stresses commonality between micro controller units rather than
differences. This means Arduino is a great platform for starters. Things you can do on an Arduino board can be done on micro controllers.

2.	Install Diver Software
For different operating system, there may be slight difference in installation method. Below is an example in WIN 7.
Download this driver software 

When you connect Arduino Uno to your computer the first time, right click “Computer”
—>“Properties”—> “Device manager”, you can see “Unknown devices”.

In this page, click “Browse my computer for driver software”.
Click “Unknown devices”, select “Update Driver software”.

21

After driver is installed, go to “Device manager” again. right click “Computer” —>	“Properties”—>

Click “Next”; select “Install this driver software anyway” to begin the installation.

Installation completed; click “Close”.

“Device manager”, you can see UNO device as below figure shows, also the correct Com port.

There is an example of uploading program, called "L" LED Blinking.
First, to select Board, running programming software, click [Tools], to select [Arduino UNO].

Next is to select COM port. In device manager, you can see the com port number COM21. So we choose Serial Port COM21.

To upload the sample program, click [File]. Move to [Examples].
Select [1.Basics]→[Blink]

Then you will see the program is showed in editor window as shown below.

Directly click Upload icon, in the compiling page below, you can see it says “Done uploading”. The “L” LED on the main board will be on for 1 second and off for 1 second.
So congratulations, your first program has been a success!

7. Project Details
1.	The Application of L298N Motor Driver Board
We would like to show you how to use L298N motor driver, for example, use it to drive two motors.

Connection: For the driver part, power supply can be external power, generally about 9V. For the
logic part, power supply can be from the board internally. Connect two motors to left and right blue terminal. EA and EB are connected to Arduino PWM interface for motor speed regulation. I1, I2, I3, I2 ports,
connected to Arduino digital interfaces, are used for controlling the motor going forward, backward, steering and braking. You can begin writing the program now. Here, the program for your reference

Program as follows:
int pinI1 = 8 ;/ / define interfaces I1

int pinI2 = 9 ;/ / define I2 interfaces
int speedpin = 11 ;/ / define EA (PWM control) Interface int pinI3 = 6 ;/ / define I3 Interface
int pinI4 = 7 ;/ / define I4 Interface
int speedpin1 = 10 ;/ / define EB (PWM control) Interface void setup ()
{
pinMode (pinI1, OUTPUT); pinMode (pinI2, OUTPUT); pinMode (speedpin, OUTPUT); pinMode (pinI3, OUTPUT); pinMode (pinI4, OUTPUT); pinMode (speedpin1, OUTPUT);
}
void loop ()
{
/ / Straight
analogWrite (speedpin, 100) ;/ / set the speed of the input analog value analogWrite (speedpin1, 100);
digitalWrite (pinI4, LOW) ;/ / make DC motor (right) turn counterclockwise digitalWrite (pinI3, HIGH);
digitalWrite (pinI1, LOW) ;/ / make DC motor (left) clockwise digitalWrite (pinI2, HIGH);
delay (2000);
/ / Back
analogWrite (speedpin, 100) ;/ / set the speed of the input analog value analogWrite (speedpin1, 100);
digitalWrite (pinI4, HIGH) ;/ / make DC motor (right) clockwise digitalWrite (pinI3, LOW);
digitalWrite (pinI1, HIGH) ;/ / make DC motor (left) turn counterclockwise digitalWrite (pinI2, LOW);
delay (2000);
/ / Left
analogWrite (speedpin, 60) ;/ / set the speed of the input analog value analogWrite (speedpin1, 60);
digitalWrite (pinI4, LOW) ;/ / make DC motor (right) turn counterclockwise digitalWrite (pinI3, HIGH);
digitalWrite (pinI1, HIGH) ;/ / make DC motor (left) turn counterclockwise digitalWrite (pinI2, LOW);
delay (2000);
/ / Right
analogWrite (speedpin, 60) ;/ / set the speed of the input analog value analogWrite (speedpin1, 60);
digitalWrite (pinI4, HIGH) ;/ / make DC motor (right) clockwise digitalWrite (pinI3, LOW);
digitalWrite (pinI1, LOW) ;/ / make DC motor (left) clockwise digitalWrite (pinI2, HIGH);
delay (2000);
/ / Brake
digitalWrite (pinI4, HIGH) ;/ / make DC motor (right) brake digitalWrite (pinI3, HIGH);
digitalWrite (pinI1, HIGH) ;/ / make DC motor (left) brake digitalWrite (pinI2, HIGH);
delay (2000);
}

Note: in the program, there can be other ways to make the motor turning left or right.

2.	Line Tracking Function

One function of the car is to make it run around the black line.

TCRT5000 infrared double tube is widely applied in robot design, and industrial manufacture. It can be used for black and white line tracking robot and industrial counting sensors.

Parameter:
1.	Detection Distance: about 2cm for white paper floor. It depends on the color, furthest distance for white color.
2.	Power Voltage: 2.5V to 12V, no more than 12V. (note: it is better to use low voltage
power supply, high power supply voltage will result in shorter service life of the sensor. 5V power supply is recommended.)
3.	Working Current: 18~20ma when the supply voltage is 5V. After many tests, when the working current is 18~20ma, the sensor is at its best performance in anti-jamming capability.

Sample Program:

int pin = 7 ;/ / Defines interface Arduino digital pin7 int val ;/ / define variables
void setup ()
{
pinMode (ledPin, OUTPUT) ;/ / set the digital interface output interface 7 Serial.begin (9600) ;/ / set the serial port baud rate to 9600kbps
}
void loop ()
{
value val = digitalRead (pin) ;/ / read digital interface Serial.println (val) ;/ value / output digital interface
}

Infrared detection method: infrared light has a different reflection intensity on the surface of different colors. When the car keeps going, it starts to transmit infrared light to the ground; when the infrared light encountered white paper floor, there appears reflected light received by	receiving tube on the car; if IR encountered black paper floor, it is absorbed and the receiving tube on the car receives no infrared light. The main board bases on whether it can receive the reflected infrared light to determine the location of the black line and the car's walking route.
Note: you can use a knob potentiometer in the circuit to adjust the sensitivity of this tracking module.

The control system design is as below figure 2-1.

Figure2-1

Arduino Tacking Car Wiring Diagram

Tracking module
Power -
Power +

Tracking car Arduino program: int MotorRight1 = 5;
int MotorRight2 = 6; int MotorLeft1 = 10; int MotorLeft2 = 11;
const int SensorLeft = 7; const int SensorMiddle = 4; const int SensorRight = 3; int SL;
int SM; int SR;

void setup ()
{
Serial.begin (9600);
pinMode (MotorRight1, OUTPUT); pinMode (MotorRight2, OUTPUT); pinMode (MotorLeft1,
pinMode (MotorLeft2,
pinMode (SensorLeft, INPUT); / / define left Sensors pinMode (SensorMiddle, INPUT) ;/ / definition sensors
pinMode (SensorRight, INPUT); / / definition of the right sensor
}
void loop ()
{
SL = digitalRead (SensorLeft); SM = digitalRead (SensorMiddle); SR = digitalRead (SensorRight);

if (SM == HIGH) / / in sensors in black areas
{

if (SL == LOW & SR == HIGH) / / left and right black white, turn left
{
digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); analogWrite (MotorLeft1, 0);
analogWrite (MotorLeft2, 80);
}
else if (SR == LOW & SL == HIGH) / / left and right black white, turn right
{

}
else
{

}
}
analogWrite (MotorRight1, 0) ;/ / right turn analogWrite (MotorRight2, 80); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);

digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH); analogWrite (MotorLeft1, 200);
analogWrite (MotorLeft2, 200);
analogWrite (MotorRight1, 200);
analogWrite (MotorRight2, 200);

else / / the sensors in the white area
{
if (SL == LOW & SR == HIGH) / / left and right black white, fast turn left
{
digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, HIGH); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, LOW);
}
else if (SR == LOW & SL == HIGH) / / left and right black white, quick right turn
{
digitalWrite (MotorRight1, LOW); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, LOW); digitalWrite (MotorLeft2, HIGH);
}
else / / are white, stop
{
digitalWrite (MotorRight1, HIGH); digitalWrite (MotorRight2, LOW); digitalWrite (MotorLeft1, HIGH); digitalWrite (MotorLeft2, LOW);;
}}}

3.	Ultrasonic Obstacle Avoidance Function

In this experiment, the ultrasonic sensor is used as the detecting element for detecting the front obstacle. The ultrasonic sensor transmitter emits ultrasonic,If the car encounters the obstacle in the front, the ultrasonic signal is reflected by the obstacle and received by the receiver and processed by the single chip. So the car avoids obstacle automatically, walk and stop.
Below is the connection diagram for ultrasonic obstacle avoidance:

Connection
Motor 1 to L298N MOTOA Motor 2 to L298N MOTOB
Use 1 contact of battery case of 6 cells of AA batteries to supply power for L298N motor driver module, another contact for Arduino main board. The + of the power supply for L298N motor driver module is connected to the VMS of L298N; the - not connected to anything.

Motor and Steering Enabled int pinLB = 6;
int pinLF = 9; int pinRB = 10; int pinRF = 11;

Servo Motor

myservo.attach (5);

Ultrasonic Sensor VCC	+5 V
TRIQ signal input ECHO	signal output GND Ground

int inputPin = A0; int outputPin = A1;

Ultrasonic Smart car obstacle avoidance procedures (ARDUINO) L = Left
R = Right F = front B = after
*/
# Include <Servo.h> int pinLB = 6;
int pinLF = 9;

int pinRB = 10; int pinRF = 11;

int inputPin = A0; int outputPin = A1;

int Fspeedd = 0; int Rspeedd = 0; int Lspeedd = 0; int directionn = 0; Servo myservo;
int delay_time = 250; / / settling time after steering servo motors

int Fgo = 8; int Rgo = 6; int Lgo = 4; int Bgo = 2;

void setup ()
{
Serial.begin (9600);
pinMode (pinLB, OUTPUT); / / pin 8 (PWM) pinMode (pinLF, OUTPUT); / / pin 9 (PWM)

pinMode (pinRB, OUTPUT); / / pin 10 (PWM) pinMode (pinRF, OUTPUT); / / pin 11 (PWM)

pinMode (inputPin, INPUT); pinMode (outputPin, OUTPUT);

myservo.attach (5);
}
void advance (int a)
{
}

void right (int b)
{

}
void left (int c)
{
digitalWrite (pinRB, HIGH); digitalWrite (pinRF, HIGH);

}
void turnR (int d)
{

}
void turnL (int e)
{
digitalWrite (pinRB, HIGH);
}
void stopp (int f)
{
digitalWrite (pinRB, HIGH); digitalWrite (pinRF, HIGH); digitalWrite (pinLB, HIGH); digitalWrite (pinLF, HIGH); delay (f * 100);
}

void back (int g)
{}

void detection ()
{
int delay_time = 250; ask_pin_F ();

if (Fspeedd <10)
{
stopp (1);
back (2);
}

if (Fspeedd <25)
{

the right

if (Lspeedd <10 && Rspeedd <10) than 10 cm distance
{

}
void ask_pin_F ()
{
myservo.write (90); digitalWrite (outputPin, LOW); delayMicroseconds (2);

digitalWrite (outputPin, HIGH); where at least 10 μ s
delayMicroseconds (10); digitalWrite (outputPin, LOW);
float Fdistance = pulseIn (inputPin, HIGH);

speed)
}
void ask_pin_L ()
{
myservo.write (5); delay (delay_time);
digitalWrite (outputPin, LOW); delayMicroseconds (2); digitalWrite (outputPin, HIGH);
where at least 10 μ s
delayMicroseconds (10); digitalWrite (outputPin, LOW);
float Ldistance = pulseIn (inputPin, HIGH);

}
void ask_pin_R ()
{
myservo.write (177); delay (delay_time);
digitalWrite (outputPin, LOW); delayMicroseconds (2); digitalWrite (outputPin, HIGH);
where at least 10 μ s
delayMicroseconds (10); digitalWrite (outputPin, LOW);
float Rdistance = pulseIn (inputPin, HIGH); Rdistance = Rdistance/5.8/10;
Serial.print ("R distance:"); Serial.println (Rdistance); Rspeedd = Rdistance;
(Right-speed)
}

void loop ()
{
myservo.write (90); time measurement
detection ();

if (directionn == 2)
{
back (8);

alley)

38

}
if (directionn == 6)
{
back (1);
turnR (6);
Serial.print ("Right");
}
if (directionn == 4)
{
back (1);
turnL (6); Serial.print ("Left");
}
if (directionn == 8)
{
advance (1);
Serial.print ("Advance"); Serial.print ("
}
}

4.	Infrared Remote Control Function
Before the experiment:
1.	Copy and paste the library IRremote into the Arduino “libraries” directory.
2.	Run IrReceive.pde on your programming software ( IRcode will be displayed in Serial Monitor).

Code
/*
* IRRemote infrared remote control code test
*

Philips RC6 and other agreements
*/
# Include <IRremote.h> const int irReceiverPin = 2;
IRrecv irrecv (irReceiverPin);
IRrecv
decode_results results; variables in decode_results

void setup ()
{
Serial.begin (9600);
is 9600 bps
irrecv.enableIRIn ();
}

/ / Display the type of infrared protocol
void showIRProtocol (decode_results * results)
{
Serial.print ("Protocol:");

/ / Judgment infrared protocol types switch (results-> decode_type) {

case NEC:
Serial.print ("NEC"); break;
case SONY:
Serial.print ("SONY"); break;
case RC5:
Serial.print ("RC5"); break;
case RC6:
Serial.print ("RC6"); break;
default:
Serial.print ("Unknown encoding");
}

/ / Print the infrared codes to Serial port Serial.print (", irCode:");
Serial.print (results-> value, HEX); Serial.print (",
Serial.println (results-> bits);
}

void loop ()
{
if (irrecv.decode (& results)) { infrared signals
showIRProtocol (& results); irrecv.resume ();
}
}

Upload above program to your main board to test if your IR remote control sensor works well.

Infrared Remote Smart Car Program
/ / ****** Infrared remote smart car program ******* # Include <IRremote.h>
int RECV_PIN = A0;
int pinLB = 6 ;/ / define interfaces I1 int pinLF = 9 ;/ / define I2 interfaces int pinRB = 3 ;/ / define I3 Interface int pinRF = 5 ;/ / define I4 Interface
/ / ****** Infrared control section ******** long advence = 0x00EF
long back = 0x00EFA 05F long stop = 0x00EF
long left = 0x00EF00FF; long right = 0x00EF40BF;

IRrecv irrecv (RECV_PIN); decode_results results;

void dump (decode_results * results) { int count = results-> rawlen;
if (results-> decode_type == UNKNOWN)
{
Serial.println ("Could not decode message");
}
else
{
if (results-> decode_type == NEC)
{
Serial.print ("Decoded NEC:");
}
else if (results-> decode_type == SONY)
{
Serial.print ("Decoded SONY:");
}
else if (results-> decode_type == RC5)
{
Serial.print ("Decoded RC5:");
}
else if (results-> decode_type == RC6)
{
Serial.print ("Decoded RC6:");
}
Serial.print (results-> value, HEX); Serial.print ("(");
Serial.print (results-> bits, DEC); Serial.println ("bits)");
}
Serial.print ("Raw ("); Serial.print (count, DEC); Serial.print ("):");

for (int i = 0; i <count; i + +)
{
if ((i% 2) == 1) {
Serial.print (results-> rawbuf [i] * USECPERTICK, DEC);
}
else
{
Serial.print (- (int) results-> rawbuf [i] * USECPERTICK, DEC);
}
Serial.print ("");
}
Serial.println ("");
}

void setup ()
{
pinMode (RECV_PIN, INPUT);
pinMode (pinLB, OUTPUT); pinMode (pinLF, OUTPUT);

pinMode (pinRB, OUTPUT); pinMode (pinRF, OUTPUT);

Serial.begin (9600);

irrecv.enableIRIn (); / / Start the receiver
}

int on = 0;
unsigned long last = millis ();

void loop ()
{
if (irrecv.decode (& results))
{
/ / If it's been at least 1/4 second since the last
/ / IR received, toggle the relay if (millis () - last> 250)
{
on = on!;
//
digitalWrite (13, on HIGH:? LOW); dump (& results);
}
if (results.value == advence)
{DigitalWrite (pinRB, LOW) ;/ / make DC motor (right) GO digitalWrite (pinRF, HIGH);
digitalWrite (pinLB, LOW) ;/ / make DC motor (left) GO digitalWrite (pinLF, HIGH);}

if (results.value == back)

{DigitalWrite (pinRB, HIGH) ;/ / make DC motor (right) BACK digitalWrite (pinRF, LOW);}

if (results.value == left)
{DigitalWrite (pinRB, LOW) ;/ / make DC motor (right) STOP digitalWrite (pinRF, HIGH);
digitalWrite (pinLB, HIGH) ;/ / make DC motor (left) GO digitalWrite (pinLF, LOW);}

if (results.value == right)
{DigitalWrite (pinRB, HIGH) ;/ / make DC motor (right) GO digitalWrite (pinRF, LOW);
digitalWrite (pinLB, HIGH) ;/ / make DC motor (left) STOP digitalWrite (pinLF, HIGH);}

if (results.value == stop)
{
digitalWrite (pinRB, HIGH) ;/ / make DC motor (right) STOP digitalWrite (pinRF, HIGH);
digitalWrite (pinLB, HIGH) ;/ / make DC motor (left) STOP digitalWrite (pinLF, HIGH);

}

last = millis ();
irrecv.resume (); / / Receive the next value
}
}

5. Multi-function smart car
1.	Before the experiment
1)	1. Copy and paste the library IRremote into the Arduino “libraries” directory.
2)	Run IrReceive.pde on your programming software ( IRcode will be displayed in Serial Monitor).

Seven buttons will be needed:
<stop><line tracking mode><ultrasonic mode><go forward><go backward><turn left><turn right>

2.	Operation
Set your own IR code in the program and you are good yo go; you can adjust it to line tracking or ultrasonic mode. If you want to stop, just press the “stop” button and you can change the mode. For ultrasonic mode, press “stop” at the beginning of the detection direction.

3.	Hardware Required Arduino328 Controller Board *1 Arduino Sensor Shield *1 L298N Motor Driver Board *1
IR Remote Controller + IR Receiver Sensor Line Tracking Sensor *3
Chassis *1 Servo Motor *1 Holder *1
Ultrasonic Sensor Module *1

4.	Wire Connection

1)	Motor Connection
//*********************** define pin for motors
int MotorRight1=5; int MotorRight2=6; int MotorLeft1=10; int MotorLeft2=11;
Above pins are on controller board for L298N, ENA and ENB of L298N connect to V (power +)
ports of the shield; connect I1, I2, I3 and I4 to D5, 6, 10 and 11 of the shield (controlling the rotation direction of the motors).

(for the power supply and motor connection of L298N, pls refer to the introduction of L298N motor driver board)

2)	IR Receiver Sensor
const int irReceiverPin = 2; // IR receiver, signal pin connected to pin 2

To GND
To 5V
To signal

GND pin of IR receiver sensor connect to G port of the shield,
+5V pin of IR receiver sensor connect to +5V port of the shield, S pin of IR receiver sensor connect to D2 port of the shield.

3)	Line Tracking Sensor
const int SensorLeft = 7;
const int SensorMiddle= 4 ; // middle sensor input pin const int SensorRight = 3; // right sensor input pin

Three line tracking sensors:
Signal pin of first sensor to D7 of the shield, VCC to +5V of the shield, GND to G port of the shield. Signal pin of Second sensor to D4 of the shield, VCC to +5V of the shield, GND to G port of the shield. Signal pin of third sensor to D3 of the shield, VCC to +5V of the shield, GND to G port of the shield.

4)	Ultrasonic Sensor

int inputPin =13 ; // define signal input pin rx for ultrasonic sensor int outputPin =12; // define signal output pin tx for ultrasonic sensor Ultrasonic sensor has 4 pins, namely VCC, TRIG, ECHO, GND. VCC connect to +5V of the shield
TRIG connect to D13 of the shield as signal input ECHO connect to D12 of the shield as signal output GND connect to G of the shield

5)	Servo Motor
myservo.attach(9); // set motor to pin D9 (PWM)

The servo motor has three wires, gray, red and yellow. For gray---GND connect to G port of the shield
Red---+5V connect to +5V of the shield  Yellow ---signal pin connect to D9 of the shield

5.	Complete Program

#include <IRremote.h> #include <Servo.h>
//***********************define motor pin************************* int MotorRight1=5;
int MotorRight2=6; int MotorLeft1=10;

int MotorLeft2=11; int counter=0;
const int irReceiverPin = 2; // define IR receiver pin

//***********************set up IRcode************************* long IRfront= 0x00FF629D;
long IRback=0x00FFA857; long IRturnright=0x00FF22DD; long IRturnleft= 0x00FFC23D; long  IRstop=0x00FF02FD; long IRcny70=0x00FF6897; long IRAutorun=0x00FF9867;
long IRturnsmallleft= 0x00FFB04F;
//*********************define pins for CNY7******************************** const int SensorLeft = 7;
const int SensorMiddle= 4 ; const int SensorRight = 3; int SL;
int SM; int SR;
IRrecv irrecv(irReceiverPin); decode_results results;
//************************* define pin for ultrasonic ****************************** int inputPin =13 ; // define signal receiving pin rx
int outputPin =12; // define signal transmitting pin tx int Fspeedd = 0; // distance upfront
int Rspeedd = 0; // distance on the right int Lspeedd = 0; // distance on the left
int directionn = 0; // front=8 back=2 left=4 right=6 Servo myservo; // set myservo
int delay_time = 250; // stabling time of servo motor int Fgo = 8; // go forward
int Rgo = 6; // go right int Lgo = 4; // go left
int Bgo = 2; // go backward
//********************************************************************(SETUP) void setup()
{
Serial.begin(9600); pinMode(MotorRight1, OUTPUT); pinMode(MotorRight2, OUTPUT); pinMode(MotorLeft1, pinMode(MotorLeft2, irrecv.enableIRIn();
47

pinMode(SensorLeft, INPUT); // define left sensor pinMode(SensorMiddle, INPUT);// define middle sensor pinMode(SensorRight, INPUT); // define right sensor digitalWrite(2,HIGH);
pinMode(inputPin, INPUT); // define input pin for ultrasonic pinMode(outputPin, OUTPUT); // define output pin for ultrasonic myservo.attach(9); // define servo motor output pin as pin5 (PWM)

}
//******************************************************************(Void) void advance(int a) // go forward
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH); delay(a * 100);
}
void right(int b) // turn right (single wheel)
{
digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH); digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); delay(b * 100);
}
void left(int c) // turn left (single wheel)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); delay(c * 100);
}
void turnR(int d) // turn right (two wheels)
{
digitalWrite(MotorRight1,HIGH); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH); delay(d * 100);
}
void turnL(int e) // turn left (two wheels)

{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,HIGH); digitalWrite(MotorLeft2,LOW); delay(e * 100);
}
void stopp(int f) // stop
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); delay(f * 100);
}
void back(int g) // backward
{
digitalWrite(MotorRight1,HIGH); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,HIGH); digitalWrite(MotorLeft2,LOW);; delay(g * 100);
}
void detection() // measure 3 angles (front, left, right)
{
int delay_time = 250; // stabling time of servo motor ask_pin_F(); // read the upfront distance

if(Fspeedd < 10) // if distance less than 10CM
{
stopp(1); // clear output back(2); // backward 0.2S
}
if(Fspeedd < 25) // if distance less than 25CM
{
stopp(1); // clear output
ask_pin_L(); // read distance on the left delay(delay_time); // wait for servo motor to stabilize ask_pin_R(); // read the distance on the right delay(delay_time); // wait for servo motor to stabilize

if(Lspeedd > Rspeedd) // if distance on the left is greater than the distance on the right
{
directionn = Lgo; // turn left

}

right
if(Lspeedd <= Rspeedd) // if distance on the left is less than or equal to the distance on the

{
directionn = Rgo; // turn right
}

if (Lspeedd < 15 && Rspeedd < 15) // if distance both left and right is less than 10CM
{
directionn = Bgo; // go backward
}
}
else // if distance upfront is greater than 25CM
{
directionn = Fgo; // go forward
}
}
//*****************************************************************************
****
void ask_pin_F() // measure the distance upfront
{
myservo.write(90);
digitalWrite(outputPin, LOW); // ultrasonic transmit low voltage 2μs delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic transmit high voltage 10μs, here should be at least 10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // maintain low voltage
float Fdistance = pulseIn(inputPin, HIGH); // read time in between Fdistance= Fdistance/5.8/10; // convert time into distance (unit: cm) Serial.print("F distance:"); // output distance (unit: cm) Serial.println(Fdistance); // display distance
Fspeedd = Fdistance; // read distance into Fspeedd (forward speed)
}
//*****************************************************************************
***
void ask_pin_L() // measure distance on the left
{
myservo.write(177); delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic transmit low voltage 2μs delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic transmit high voltage 10μs, here should be at least

10μs delayMicroseconds(10);
digitalWrite(outputPin, LOW); // maintain low voltage float Ldistance = pulseIn(inputPin, HIGH); //
Ldistance= Ldistance/5.8/10; // convert time into distance (unit: cm) Serial.print("L distance:"); // output distance (unit: cm) Serial.println(Ldistance); // display distance
Lspeedd = Ldistance; // read distance into Lspeedd (left speed)
}
//*****************************************************************************
*
void ask_pin_R() // measure distance on the right
{
myservo.write(5); delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic transmit low voltage 2μs delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic transmit high voltage 10μs, here should be at least 10μs
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // maintain low voltage
float Rdistance = pulseIn(inputPin, HIGH); // read time in between Rdistance= Rdistance/5.8/10; // convert time into distance (unit: cm) Serial.print("R distance:"); // output distance (unit: cm) Serial.println(Rdistance); // display distance
Rspeedd = Rdistance; // read distance into Rspeedd (right speed)
}
//*****************************************************************************
*(LOOP)
void loop()
{
SL = digitalRead(SensorLeft); SM = digitalRead(SensorMiddle); SR = digitalRead(SensorRight);
//***************************************************************************nor   mal remote control mode
if (irrecv.decode(&results))
{
/***********************************************************************/      if (results.value == IRfront)// go forward
{
advance(10);// go forward
}
/***********************************************************************/

if (results.value ==
{
back(10);// backward
}
/***********************************************************************/      if (results.value == IRturnright)// turn right
{
right(6); // turn right
}
/***********************************************************************/      if (results.value == IRturnleft)// turn left
{
left(6); // turn left);
}
/***********************************************************************/      if (results.value == IRstop)// stop
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW);
}
//***********************************************************************cny70 mode black:LOW white: HIGH
if (results.value == IRcny70)
{
while(IRcny70)
{
SL = digitalRead(SensorLeft); SM = digitalRead(SensorMiddle); SR = digitalRead(SensorRight);

if (SM == HIGH)// middle sensor in black area
{
if (SL == LOW & SR == HIGH) // left sensor black, right sensor white, turn left
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); analogWrite(MotorLeft1,0); analogWrite(MotorLeft2,80);
}

right
else if (SR == LOW & SL == HIGH) // left sensor white, right sensor black, turn

{

}
else
{

}
}
analogWrite(MotorRight1,0);// turn right analogWrite(MotorRight2,80); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH);

digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH); analogWrite(MotorLeft1,200); analogWrite(MotorLeft2,200); analogWrite(MotorRight1,200); analogWrite(MotorRight2,200);

else // middle sensor in white area
{
if (SL == LOW & SR == HIGH)// left sensor black, right sensor white, turn left
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW);
}
else if (SR == LOW & SL == HIGH) // left sensor white, right sensor black, turn right
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,HIGH);
}
else // both sides are white, stop
{
digitalWrite(MotorRight1,HIGH); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,HIGH); digitalWrite(MotorLeft2,LOW);;
}
}
if (irrecv.decode(&results))
{
irrecv.resume();

Serial.println(results.value,HEX); if(results.value ==IRstop)
{
digitalWrite(MotorRight1,HIGH); digitalWrite(MotorRight2,HIGH); digitalWrite(MotorLeft1,HIGH); digitalWrite(MotorLeft2,HIGH); break;
}
}
}
results.value=0;
}
//*********************************************************************** ultrasonic mode
if (results.value ==IRAutorun )
{

{

measurement
while(IRAutorun)

myservo.write(90); // homeset servo motor, ready the position for next

detection(); // measure the angle and decide which way to move if(directionn == 8) // when directionn= 8(forward)

{
if (irrecv.decode(&results))
{
irrecv.resume(); Serial.println(results.value,HEX); if(results.value ==IRstop)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); break;
}
}
results.value=0; advance(1); // forward
Serial.print(" Advance "); // display direction(forward) Serial.print(" ");
}
if(directionn == 2) // when directionn= 2(backward)
{

if (irrecv.decode(&results))
{
irrecv.resume(); Serial.println(results.value,HEX); if(results.value ==IRstop)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); break;
}
}
results.value=0; back(8); // backward
turnL(3); // slightly turn left (prevent stuck in corner) Serial.print(" Reverse "); // display direction (backward)
}
if(directionn == 6) // when directionn = 6(turn right)
{
if (irrecv.decode(&results))
{
irrecv.resume(); Serial.println(results.value,HEX); if(results.value ==IRstop)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); break;
}
}
results.value=0; back(1);
turnR(6); // turn right
Serial.print(" Right "); // display direction (right)
}
if(directionn == 4) // when directionn= 4(turn right)
{
if (irrecv.decode(&results))
{
irrecv.resume(); Serial.println(results.value,HEX);

if(results.value ==IRstop)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); break;
}
}
results.value=0; back(1);
turnL(6); // turn left
Serial.print(" Left "); // display direction(left)
}

if (irrecv.decode(&results))
{
irrecv.resume(); Serial.println(results.value,HEX); if(results.value ==IRstop)
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW); break;
}
}
}
results.value=0;
}
/***********************************************************************/  else
{
digitalWrite(MotorRight1,LOW); digitalWrite(MotorRight2,LOW); digitalWrite(MotorLeft1,LOW); digitalWrite(MotorLeft2,LOW);
}

irrecv.resume();
}
}