Ks0428 keyestudio Mini Tank Robot V2: Difference between revisions

From Keyestudio Wiki
Jump to navigation Jump to search
 
(101 intermediate revisions by the same user not shown)
Line 3: Line 3:
'''*About keyestudio'''
'''*About keyestudio'''


Keyestudio is a best-selling brand owned by KEYES Corporation, our product lines range from controller boards, shields and sensor modules to smart car and complete starter kits for Arduino, Raspberry Pi and BBC micro:bit, which designed for customers of any level to learn electronics and programming knowledge. All of our products comply with international quality standards and are greatly appreciated in a variety of different markets throughout the world.
Keyestudio is a best-selling brand owned by KEYES Corporation, our product lines range from controller boards, shields and sensor modules to smart car and complete starter kits for Arduino, Raspberry Pi and BBC micro:bit, which designed for customers of any level to learn electronics and programming knowledge. All of our products comply with international quality standards and are greatly appreciated in a variety of different markets throughout the world.<br>
You can obtain the details and the latest information through visiting the following web sites:http://www.keyestudio.com
You can obtain the details and the latest information through visiting the following web sites:http://www.keyestudio.com<br>


'''*References and After-sales Service'''
'''*References and After-sales Service'''


1.Download Profile:https://fs.keyestudio.com/KS0428
1.Download Profile:https://fs.keyestudio.com/KS0428<br>
2.Feel free to contact us please, if there is missing part or you encounter some troubles. Welcome to send email to us:service@keyestudio.com. We will update projects and products continuously based on your sincere advice.
2.Feel free to contact us please, if there is missing part or you encounter some troubles. Welcome to send email to us:service@keyestudio.com. <br> We will update projects and products continuously based on your sincere advice.<br>


'''*Warning'''
'''*Warning'''


1.This product contains tiny parts(screws, copper pillars), keep it out of reach of children under 7 years old please.
1.This product contains tiny parts(screws, copper pillars), keep it out of reach of children under 7 years old please.<br>
2. This product contains conductive parts (control board and electronic module). Please operate according to the requirements of this tutorial. Improper operation may cause overheating and damage parts. Do not touch and immediately disconnect the circuit power.
2. This product contains conductive parts (control board and electronic module). Please operate according to the requirements of this tutorial. Improper operation may cause overheating and damage parts. Do not touch and immediately disconnect the circuit power.<br>




Line 26: Line 26:
== Introduction ==  
== Introduction ==  
   
   
Nowadays, technological education such as VR, kids programming, and artificial intelligence, has become mainstream in educational industry. Thereby, people attach importance to STEAM education. Arduino is pretty notable in Maker education.
Nowadays, technological education such as VR, kids programming, and artificial intelligence, has become mainstream in educational industry. Thereby, people attach importance to STEAM education. Arduino is pretty notable in Maker education.<br>


So what is Arduino? Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. Based on this, Keyestudio team has designed a mini tank robot. The tank robot has a processor which is programmable using the Arduino IDE, to mapped its pins to sensors and actuators by a shield that plug in the processor, it reads sensors and controls the actuators and decides how to operate.
So what is Arduino? Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. Based on this, Keyestudio team has designed a mini tank robot. The tank robot has a processor which is programmable using the Arduino IDE, to mapped its pins to sensors and actuators by a shield that plug in the processor, it reads sensors and controls the actuators and decides how to operate.<br>


15 learning projects, from simple to complex, will guide you how to make a smart tank robot on you own and introduce the detailed knowledge about sensors and modules. <br>
15 learning projects, from simple to complex, will guide you how to make a smart tank robot on you own and introduce the detailed knowledge about sensors and modules. <br>
Line 60: Line 60:
Make sure no any parts missing please when you get this robot kit. And  if it is, please feel free to contact us.
Make sure no any parts missing please when you get this robot kit. And  if it is, please feel free to contact us.


<br>[[File:0428-4.png|500px|frameless|thumb]]<br>
'''Please tear off the protective film on the acrylic board before mounting the kit'''
<br>[[File:0428-5.png|500px||frameless|thumb]]<br> 
<br>[[File:QQ图片20210113151033.png|500px|frameless|thumb]]<br>
<br>[[File:0428-6.png|500px|frameless|thumb]]<br>   
 
<br>[[File:0428-7.png|500px|frameless|thumb]]<br> 
<br>[[File:QQ图片20210422152531.png|800px|frameless|thumb]]<br>   
<br>[[File:0428-8.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片20210422152532.png|800px||frameless|thumb]]<br>   
<br>[[File:0428-9.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210422152543.png|800px|frameless|thumb]]<br>  
<br>[[File:0428-10.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210907152714.png|800px|frameless|thumb]]<br>  
<br>[[File:0428-11.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210422152605.png|800px|frameless|thumb]]<br>  
<br>[[File:0428-12.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片20210422152606.png|800px|frameless|thumb]]<br>   
<br>[[File:0428-13.png|500px|frameless|thumb]]<br>
<br>[[File:QQ图片20210422152607.png|800px|frameless|thumb]]<br>
<br>[[File:0428-14.png|500px|frameless|thumb]]<br>  
QQ图片20210422152554.png|
<br>[[File:0428-15.png|500px|frameless|thumb]]<br>


== Assembly Guide ==
== Assembly Guide ==
<span style=color:red> <big>Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code</big></span>
    
    
After making an inventory all the parts in this kit, then we need to mount the tank robot. Let’s install smart car in compliance with the following instructions.
<span style=color:red>'''Note: Peel the plastic film off the board first when installing the smart car. To be honest, we never intend to send wood to you.'''<br>


Step 1: Install the tank bottom motor
After making an inventory all the parts in this kit, then we need to mount the tank robot. Let’s install smart car in compliance with the following instructions.<br>
 
Step 1: Install Bottom Motor


Prepare the parts as follows:
Prepare the parts as follows:


* M4 nut * 2
* M4 Nut * 2
* Metal motor *2  
* Metal Motor *2  
* Metal holder *2
* Metal Holder *2
* Copper coupler *2
* Copper Coupler *2
* Blue supporting parts *2
* Blue Supportive Parts *2
* M4*12MM inner hex screw * 2
* M4*12MM Inner Hex Screw * 2
* L-type M1.5 inner hex wrench *1
* M1.5 Hex Key Nickel Plated Allen Wrench *1
* L-type M3 inner hex wrench *1
* M3 Hex Key Nickel Plated Allen Wrench *1
* L-type M2.5 inner hex wrench *1
* M2.5 Hex Key Nickel Plated Allen Wrench *1
* M3*8MM inner hex screw * 4
* M3*8MM Inner Hex Screw * 4


<br>[[File:0428-1.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片20210907153855.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-2.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-2.png|500px|frameless|thumb]]<br>   


Step 2: Install the tank driver wheel
Step 2: Install Driver Wheel


Prepare the parts as follows:
Prepare the parts as follows:


* M4*12MM inner hex screw * 2
* M4*12MM Inner Hex Screw * 2
* M4*50MM inner hex screw * 2
* M4*50MM Inner Hex Screw * 2
* Tank Load-bearing wheel * 2
* Tank Load-bearing Wheel * 2
* Flange Bearing * 4
* Flange Bearing * 4
* Copper bush *2
* Copper Bush *2
* Caterpillar Band *2
* Caterpillar Band *2
* M4 self-locking nut * 2
* M4 Self-locking Nut * 2
* L-type M3 inner hex wrench *1
* M3 Hex Key Nickel Plated Allen Wrench *1


<br>[[File:0428-16.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-16.png|500px|frameless|thumb]]<br>   
Line 113: Line 116:




Step 3: Install the battery holder
Step 3: Install the Battery Holder


Prepare the parts as follows:  
Prepare the parts as follows:  
Line 119: Line 122:
<br>[[File:0428-19.png|300px|right|frameless|thumb]]<br>   
<br>[[File:0428-19.png|300px|right|frameless|thumb]]<br>   


* Battery holder *1
 
* M3 nut * 2
* Battery Holder *1
* M3 Nut * 2
* Blue Metal holder *2
* Blue Metal holder *2
* M4 nut *8
* M4 Nut *8
* M3*10MM flat-head screw * 2
* M3*10MM Flat Head Screw * 2
* M4*40MM inner hex screw *4
* M4*40MM Inner Hex Screw *4
* L-type M2.5 inner hex wrench *1
* M2.5 Hex Key Nickel Plated Allen Wrench*1
* L-type M3 inner hex wrench *1
* M3 Hex Key Nickel Plated Allen Wrench *1
* M3*25MM inner hex screw *4
* M3*25MM Inner Hex Screw *4


Finish the above part. Go to fix the metal holder on the motor wheel with four M4*40MM inner hex screws and four M4 nuts.
Finish the mount process. Go to fix the metal holder on the motor wheel with four M4*40MM inner hex screws and four M4 nuts.
<br>[[File:0428-20.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-20.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-21.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-21.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-22.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-22.png|500px|frameless|thumb]]<br>   


Step 4: Mount the sensor module
Step 4: Mount Acrylic Board and Sensors
   
   
* Install Acrylic board and sensor modules:
* Acrylic Board * 2
* Acrylic Board * 2
* L- type black bracket *1
* L- type Black Bracket *1
* Photocell Sensor *2
* Photocell Sensor *2
* IR Receiver Module *1
* IR Receiver Module *1
Line 144: Line 147:
* M2 Nut *4  
* M2 Nut *4  
* M3 Nut *10
* M3 Nut *10
* M3*8MM inner hex screw * 4
* M3*6MM Inner Hex Screw * 8
* L-type M2.5 inner hex wrench *1
* M2.5 Hex Key Allen Wrench *1
* M3*12MM Round-head screw *7
* M3*12MM Round Head Screw *7
* M2*11MM dual-pass copper pillar *4
* M3*10MM Hexagon Copper Bush *8
* M2*10MM Round-head screw * 8
* M2*10MM Round Head Screw * 4


<br>[[File:0428-23.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片2020091718.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-24.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片2020091719.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-25.png|500px|frameless|thumb]]<br>   
<br>[[File:QQ图片202009171910.png|500px|frameless|thumb]]<br>   
<br>[[File:0428-26.png|500px|frameless|thumb]]<br>  
<br>[[File:QQ图片20200917191-11.png|500px|frameless|thumb]]<br>  


Step 5: Install the servo platform
Step 5: Install the Servo Platform 


Prepare the parts as follows:
Prepare the parts as follows:


* Servo *1
* Servo *1
* Black gimbal *1
* Black Gimbal *1
* Cable tie *2
* Cable Tie *2
* M2x8 Round head cross tapping screw *2
* M2x8 Round Head Cross Tapping Screw *2
* Ultrasonic *1
* Ultrasonic Sensor *1
* M2*4 screw *1
* M2*4 Screw *1
* M1.2*5 screw *4
* M1.2*5 Screw *4


Note: For convenient debugging, make sure that the ultrasonic module is in front of tank robot, and the angle of servo motor is 90°. Therefore, we need to set servo to 90° before installing the servo platform.


<br>[[File:0428-27.png|500px|frameless|thumb]]<br>  
Note: for convenient debugging, keep the ultrasonic module straight ahead and the angle of servo motor 90°. Therefore, we need to set servo to 90° before installing the servo platform.   
 
<br>[[File:图片123564.png|800px|frameless|thumb]]<br>  
 
<br>[[File:QQ图片20220121101851.png|800px|frameless|thumb]]<br>
 
<big><span style=color:red>'''You can find M1.2*5 Screws inside the bag of Plastic Platform.'''</big></span>
 
<br>[[File:0428-28.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-28.png|500px|frameless|thumb]]<br>  




Step 6: Install all parts of Acrylic plate
Step 6: Install Sensors and Boards


Prepare the parts as follows:
Prepare the parts as follows:


* M2*5MM round head screw *4
* M3*6MM Round Head Screw *12
* M3*6MM round head screw *8
* M3*12MM round head screw *2
* M3 nickel plating nut *2
* L298P Shield *1
* L298P Shield *1
* V4.0 Board *1
* V4.0 Board *1
* V5 Sensor Shield *1
* V5 Sensor Shield *1
* Screwdriver *1
* Screwdriver *1
* Bluetooth *1
* Bluetooth Module *1


<br>[[File:0428-29.png|500px|frameless|thumb]]<br>  
<br>[[File:QQ图片20200917191-12.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-30.png|700px|frameless|thumb]]<br>  
<br>[[File:QQ图片20200917191-13.png|700px|frameless|thumb]]<br>  
<br>[[File:0428-31.png|700px|frameless|thumb]]<br>  
<br>[[File:QQ图片20200917191-14.png|500px|frameless|thumb]] [[File:04288-1.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-32.png|700px|frameless|thumb]]<br>  
<br>[[File:QQ图片20200917191-15.png|700px|frameless|thumb]]<br>  


Step 7: Hook-up Guide
Step 7: Hook-up Guide


<br>[[File:0428-33.png|700px|frameless|thumb]]<br>  
<br>[[File:04288-2.png|700px|frameless|thumb]]<br>  
<br>[[File:0428-34.png|700px|frameless|thumb]]<br>  
<br>[[File:04288-3.png|700px|frameless|thumb]]<br>
<br>[[File:04288-4.png|700px|frameless|thumb]]<br>
<br>[[File:04288-5.png|700px|frameless|thumb]]<br>
<br>[[File:04288-6.png|700px|frameless|thumb]]<br>  


Step 8: Wire up LED panel


<br>[[File:0428-35.png|500px|frameless|thumb]]<br>
Step 8: Wire up LED Panel
<br>[[File:0428-36.png|500px|frameless|thumb]]<br>


<br>[[File:04288-7.png|500px|frameless|thumb]]<br>
<br>[[File:04288-8.png|500px|frameless|thumb]]<br>
<br>[[File:04288-9.png|500px|frameless|thumb]]<br>
<br>[[File:04288-10.png|500px|frameless|thumb]]<br>


Step 9: Install all parts of Acrylic plate


Step 9: install all parts of Acrylic plate
<br>[[File:0428-37.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-37.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-38.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-12.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-39.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-13.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-40.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-14.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-41.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-15.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-42.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-16.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-43.png|500px|frameless|thumb]]<br>  
<br>[[File:04288-17.png|500px|frameless|thumb]]<br>
<br>[[File:04288-18.png|500px|frameless|thumb]]<br>
 
<br>[[File:图片1888888-1.png|500px|frameless|thumb]]<br>
<br>[[File:图片1888888-2.png|500px|frameless|thumb]]<br>
<br>[[File:图片1888888-3.png|500px|frameless|thumb]]<br>
<br>[[File:图片1888888-4.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-44.png|500px|frameless|thumb]]<br>
<br>[[File:0428-44.png|500px|frameless|thumb]]<br>


Line 217: Line 234:


<br>[[File:0428-3.png|500px|frameless|thumb]]<br>
<br>[[File:0428-3.png|500px|frameless|thumb]]<br>
<span style=color:red>Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code.<br>


== Install Arduino IDE and Driver ==  
== Install Arduino IDE and Driver ==  
Line 237: Line 256:
You just need to click JUST DOWNLOAD, then click the downloaded file to install it. And when the ZIP file is downloaded, you can directly unzip and start it.
You just need to click JUST DOWNLOAD, then click the downloaded file to install it. And when the ZIP file is downloaded, you can directly unzip and start it.


=== Keyestudio V4.0 Development Board ===


We need to know keyestudio V4.0 development board, as a core of this smart car.


<br>[[File:0077-78--1.png|400px|frameless|thumb]]<br>


keyestudio V4.0 development board is an Arduino uno-compatible board, which is based on ATmega328P MCU, and with a cp2102 Chip as a UART-to-USB converter.  


<br>[[File:0470---42.png|500px|frameless|thumb]]<br>


It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, 2 ICSP headers and a reset button.


<br>[[File:0428---1000.png|500px|frameless|thumb]]<br>


It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it via an external DC power jack (DC 7-12V) or via female headers Vin/ GND(DC 7-12V) to get started.


<br>[[File:0470---40.png|500px|frameless|thumb]]<br>


=== Installing driver ===


Let’s install the driver of keyestudio PLUS control board. The USB-TTL chip on PLUS board adopts CP2102 serial chip. The driver program of this chip is included in Arduino 1.8 version and above, which is convenient. Plug on USB port of board, the computer can recognize the hardware and automatically install the driver of CP2102.


If install unsuccessfully, or you intend to install manually, open the device manager of computer. Right click Computer----- Properties----- Device Manager.


<br>[[File:0085=07.png|500px|frameless|thumb]]<br>


There is a yellow exclamation mark on the page, which implies installing the driver of CP2102 unsuccessfully. Then we double click the hardware and update the driver.


<br>[[File:0085=08.png|500px|frameless|thumb]]<br>


Click “OK” to enter the following page, click “browse my computer for updated driver software”, find out the installed or downloaded ARDUINO software. As shown below:


<br>[[File:0085=09.png|500px|frameless|thumb]]<br>


There is a DRIVERS folder in Arduino software installed package([[File:0085=10.png|500px|frameless|thumb]]), open driver folder and you can see the driver of CP210X series chips.




We click “Browse”, then find out the driver folder, or you could enter “driver” to search in rectangular box, then click “next”, the driver will be installed successfully. (I place Arduino software folder on the desktop, you could follow my way)


<br>[[File:0085=11.png|500px|frameless|thumb]]<br>




Open device manager, we will find the yellow exclamation mark disappear. The driver of CP2102 is installed successfully.
<br>[[File:0085=12.png|500px|frameless|thumb]]<br>
<br>[[File:0085=13.png|500px|frameless|thumb]]<br>




Line 265: Line 307:




=== Install other visions of driver ===


If your development board is Arduino UNO board, install the driver as follows:<br>
Step 1: Plug in the development board, click Computer----- Properties----- Device Manager, you could see the unknown device is shown.<br>


<br>[[Image:0428---100.png|500px|frameless]]<br>


Step 2: Update the driver


<br>[[Image:0428---101.png|500px|frameless]]<br>


Step 3: click“browse my computer for updated driver software”


<br>[[Image:0428---102.png|500px|frameless]]<br>
Step 4: find out the folder where the ARDUINO software is installed, click drivers folder and tap“Next”
<br>[[Image:0428---103.png|500px|frameless]]<br>


Step 5: the driver is installed successfully.


<br>[[Image:0428---104.png|500px|frameless]]<br>


The device manager shows the serial port of Arduino.
<br>[[Image:0428---105.png|500px|frameless]]<br>




=== '''Arduino IDE Setting''' ===


Click[[Image:0486-12.png|200px|frameless]] icon,open Arduino IDE.
<br>[[Image:0085=14.png|400px|frameless]]<br>


To avoid the errors when uploading the program to the board, you need to select the correct Arduino board that matches the board connected to your computer.
Then come back to the Arduino software, you should click Tools→Board, select the board. (as shown below)
<br>[[Image:0085=15.png|500px|frameless]]<br>




Then select the correct COM port (you can see the corresponding COM port after the driver is successfully installed)


<br>[[Image:0085=16.png|500px|frameless]]<br>
<br>[[Image:0085=17.png|500px|frameless]]<br>


Before uploading the program to the board, let’s demonstrate the function of each symbol in the Arduino IDE toolbar.<br>


A- Used to verify whether there is any compiling mistakes or not.<br>
B- Used to upload the sketch to your Arduino board.<br>
C- Used to create shortcut window of a new sketch.<br>
D- Used to directly open an example sketch.<br>
E- Used to save the sketch.<br>
F- Used to send the serial data received from board to the serial monitor.<br>


=== Start your first program ===


Open the file to select Example, choose BLINK from BASIC, as shown below:




<br>[[Image:048610png.png|500px|frameless]]<br>


<br>[[Image:048611png.png|500px|frameless]]<br>


Set board and COM port, the corresponding board and COM port are shown on the lower right of IDE.


<br>[[Image:048612png.png|500px|frameless]]<br>       


Click [[Image:0486-20.png|50px|frameless]] to start compiling the program, check errors.


<br>[[Image:048613png.png|500px|frameless]]<br>


Click  [[Image:0486-23.png|50px|frameless]]to upload the program, upload successfully.


<br>[[Image:048614png.png|500px|frameless]]<br>


Upload the program successfully, the onboard LED lights on for 1s, lights off for 1s. Congratulation, you finish the first program.








== How to Add a Library? ==




What are Libraries ?<br>
Libraries are a collection of code that makes it easy for you to connect to a sensor,display, module, etc. <br>
For example, the built-in LiquidCrystal library helps talk to LCD displays. There are hundreds of additional libraries available on the Internet for download. <br>
The built-in libraries and some of these additional libraries are listed in the reference. <br>


Here we will introduce the most simple way for you to add libraries .<br>
Step 1:After downloading well the Arduino IDE, you can right-click the icon of Arduino IDE.<br>
Find the option "Open file location" shown as below:<br>


<br>[[File:0085--200.png|500px|frameless|thumb]]<br>


Step 2: Enter it to find out libraries folder which is the library file of Arduino.<br>


<br>[[File:0428---81.png|800px|frameless|thumb]]<br>




Step 3:Next to find out the“libraries”of tank robot(seen in the link: https://fs.keyestudio.com/KS0428), you just need to replicate and paste into the libraries folder of Arduino IDE.


<br>[[File:图片1042801.png|800px|frameless|thumb]]<br>
<br>[[File:图片1042802.png|800px|frameless|thumb]]<br>


Then the libraries of tank robot are installed successfully, as shown below:
<br>[[File:图片1042803.png|800px|frameless|thumb]]<br>


== Projects ==


=== Installing driver ===
=== Project 1: LED Blink ===
 
Let’s install the driver of keyestudio PLUS control board. The USB-TTL chip on PLUS board adopts CP2102 serial chip. The driver program of this chip is included in Arduino 1.8 version and above, which is convenient. Plug on USB port of board, the computer can recognize the hardware and automatically install the driver of CP2102.


If install unsuccessfully, or you intend to install manually, open the device manager of computer. Right click Computer----- Properties----- Device Manager.
'''(1)Description'''
<br>[[File:0428-46.png|300px|frameless|thumb]]<br>


<br>[[File:0085=07.png|500px|frameless|thumb]]<br>
For the starter and enthusiast, this is a fundamental program---LED Blink.<br>  
LED, the abbreviation of light emitting diodes, consist of Ga, As, P, N chemical compound and so on. The LED can flash diverse color by altering the delay time in the test code. When in control, power on GND and VCC, the LED will be on if S end is high level; nevertheless, it will go off.


There is a yellow exclamation mark on the page, which implies installing the driver of CP2102 unsuccessfully. Then we double click the hardware and update the driver.
'''(2)Specification'''


<br>[[File:0085=08.png|500px|frameless|thumb]]<br>
<br>[[File:0428-47.png|500px|frameless|thumb]]<br>  


Click “OK” to enter the following page, click “browse my computer for updated driver software”, find out the installed or downloaded ARDUINO software. As shown below:
* Control interface: digital port
* Working voltage: DC 3.3-5V
* Pin spacing: 2.54mm
* LED display color: red


<br>[[File:0085=09.png|500px|frameless|thumb]]<br>
'''(3)Equipment'''


There is a DRIVERS folder in Arduino software installed package([[File:0085=10.png|500px|frameless|thumb]]), open driver folder and you can see the driver of CP210X series chips.
<br>[[File:0428-48.png|700px|frameless|thumb]]<br>




We click “Browse”, then find out the driver folder, or you could enter “driver” to search in rectangular box, then click “next”, the driver will be installed successfully. (I place Arduino software folder on the desktop, you could follow my way)
'''(4) V5 Sensor Shield''' <br>             
There will be troublesome when we combine Arduino development boards with numerous sensors. However, the V5 sensor shield, compatible with Arduino development board, address this problem perfectly. Just stack V5 board on it.
This sensor shield can be inserted into 3pin sensor modules and breaks out some some communication pins, like serial, IIC, and SPI communication as well.


<br>[[File:0085=11.png|500px|frameless|thumb]]<br>
'''Pins Description'''
<br>[[File:0428-49.png|700px|frameless|thumb]]<br>  




Open device manager, we will find the yellow exclamation mark disappear. The driver of CP2102 is installed successfully.
Connection Diagram:
<br>[[File:0085=12.png|500px|frameless|thumb]]<br>
<br>[[File:0085=13.png|500px|frameless|thumb]]<br>


<br>[[File:0428---1.png|700px|frameless|thumb]]<br>


Seen from the above diagram, LED is linked with D2


(5) Test Code


<pre>
/*
keyestudio Mini Tank Robot V2
lesson 1.1
Blink
http://www.keyestudio.com
*/
void setup()
{
    pinMode(2, OUTPUT);// initialize digital pin 2 as an output.
}
void loop() // the loop function runs over and over again forever


{
  digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(1000); // wait for a second
  digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
  delay(1000); // wait for a second
}
//****************************************************************


</pre>


'''(6) Test Result'''


(There will be contradict about serial communication between code and  Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.)
Upload the program on the development board, LED flicker with the interval of 1s.


<br>[[File:0428-50.png|500px|frameless|thumb]]<br>


'''(7)Code Explanation'''


pinMode(2,OUTPUT) - Set pin2 to OUTPUT


digitalWrite(2,HIGH) - When set pin2 to HIGH level(output 5V) or to LOW level(output 0V)


(8)Extension Practice
We succeed to blink LED. Next, let’s observe what LED will change if we modify pins and delay time.




'''Connection Diagram'''


<br>[[File:0428-51.png|500px|frameless|thumb]]<br>


We’ve altered pins and connected LED to D10.


Test Code:


<pre>


/*
keyestudio Mini Tank Robot V2
lesson 1.2
delay
http://www.keyestudio.com
*/
void setup() {  // initialize digital pin 10 as an output.
  pinMode(10, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
  digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(100); // wait for 0.1 second
  digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
  delay(100); // wait for 0.1 second
}
//****************************************************************


</pre>


The LED flickers faster than before through the test result, therefore, pins and delay time affect flash frequency.


=== Project 2: Adjust LED Brightness ===


(1)Description
In previous lesson, we control LED on and off and make it blink.
In this project, we will control LED brightness through PWM to simulate breathing effect. Similarly, you can change the step length and delay time in the code so as to demonstrate different breathing effect.
PWM is a means of controlling the analog output via digital means. Digital control is used to generate square waves with different duty cycles (a signal that constantly switches between high and low levels) to control the analog output.In general, the input voltage of port are 0V and 5V. What if the 3V is required? Or what if switch among 1V, 3V and 3.5V? We can’t change resistor constantly. For this situation, we need to control by PWM.


<br>[[File:0085=33.png|500px|frameless|thumb]]<br>


For the Arduino digital port voltage output, there are only LOW and HIGH, which correspond to the voltage output of 0V and 5V. You can define LOW as 0 and HIGH as 1, and let the Arduino output five hundred 0 or 1 signals within 1 second.
If output five hundred 1, that is 5V; if all of which is 1, that is 0V. If output 010101010101 in this way then the output port is 2.5V, which is like showing movie. The movie we watch are not completely continuous. It actually outputs 25 pictures per second. In this case, the human can’t tell it, neither does PWM. If want different voltage, need to control the ratio of 0 and 1. The more 0,1 signals output per unit time, the more accurately control.
 


(2) Specification
* Control interface: digital port
* Working voltage: DC 3.3-5V
* Pin spacing: 2.54mm
* Display color: red




=== '''Arduino IDE Setting''' ===
(3) Equipment
<br>[[File:0428-53.png|700px|frameless|thumb]]<br>


Click[[Image:0486-12.png|200px|frameless]] icon,open Arduino IDE.
(4) Connection Diagram
<br>[[Image:0085=14.png|400px|frameless]]<br>
<br>[[File:0428-51.png|500px|frameless|thumb]]<br>  


To avoid the errors when uploading the program to the board, you need to select the correct Arduino board that matches the board connected to your computer.
(5) Test Code
Then come back to the Arduino software, you should click Tools→Board, select the board. (as shown below)
<br>[[Image:0085=15.png|500px|frameless]]<br>


<pre>
/*
keyestudio Mini Tank Robot V2
lesson 2.1
pwm
http://www.keyestudio.com
*/
int ledPin = 10; // Define the LED pin at D10
int value;
void setup ()
{
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop ()
{
for (value = 0; value <255; value = value + 1)
{
analogWrite (ledPin, value); // LED lights gradually light up
delay (5); // delay 5MS
}
for (value = 255; value> 0; value = value-1)
{
analogWrite (ledPin, value); // LED gradually goes out
delay (5); // delay 5MS
}}//**********************************************************


Then select the correct COM port (you can see the corresponding COM port after the driver is successfully installed)
</pre>
 
<br>[[Image:0085=16.png|500px|frameless]]<br>
<br>[[Image:0085=17.png|500px|frameless]]<br>
 
 
=== Start your first program ===
 
Open the file to select Example, choose BLINK from BASIC, as shown below:


(6)Test Result<br>


<br>[[Image:048610png.png|500px|frameless]]<br>
Upload test code successfully, LED gradually becomes brighter then darker, like human breath, rather than light on and off immediately.


<br>[[Image:048611png.png|500px|frameless]]<br>
(7)Code Explanation


Set board and COM port, the corresponding board and COM port are shown on the lower right of IDE.
When we need to repeat some statements, we could use FOR statement.<br>
FOR statement format is shown below:


<br>[[Image:048612png.png|500px|frameless]]<br>        
<br>[[File:0085=36.png|500px|frameless|thumb]]<br>  


Click [[Image:0486-20.png|50px|frameless]] to start compiling the program, check errors.


<br>[[Image:048613png.png|500px|frameless]]<br>


Click  [[Image:0486-23.png|50px|frameless]]to upload the program, upload successfully.
FOR cyclic sequence:
 
Round 1:1 → 2 → 3 → 4
<br>[[Image:048614png.png|500px|frameless]]<br>
Round 2:2 → 3 → 4
Until number 2 is not established, “for”loop is over,
After knowing this order, go back to code:
for (int value = 0; value < 255; value=value+1){
        ...
}
for (int value = 255; value >0; value=value-1){
      ...
}


Upload the program successfully, the onboard LED lights on for 1s, lights off for 1s. Congratulation, you finish the first program.
The two“for”statements make value increase from 0 to 255, then reduce from 255 to 0, then increase to 255,....infinitely loop
There is a new function in the following ----- analogWrite()
We know that digital port only has two state of 0 and 1. So how to send an analog value to a digital value? Here,this function is needed. Let’s observe the Arduino board and find 6 pins marked“~”which can output PWM signals.<br>


Function format as follows:
analogWrite(pin,value)
analogWrite() is used to write an analog value from 0~255 for PWM port, so the value is in the range of 0~255. Attention that you only write the digital pins with PWM function, such as pin 3, 5, 6, 9, 10, 11.<br>
PWM is a technology to obtain analog quantity through digital method. Digital control forms a square wave, and the square wave signal only has two states of turning on and off (that is, high or low levels). By controlling the ratio of the duration of turning on and off, a voltage varying from 0 to 5V can be simulated. The time turning on(academically referred to as high level) is called pulse width, so PWM is also called pulse width modulation.<br>
Through the following five square waves, let’s acknowledge more about PWM. <br>


<br>[[File:0085=37.png|500px|frameless|thumb]]<br>


In the above figure, the green line represents a period, and value of analogWrite() corresponds to a percentage which is called Duty Cycle as well. Duty cycle implies that high-level duration is divided by low-level duration in a cycle. From top to bottom, the duty cycle of first square wave is 0% and its corresponding value is 0. The LED brightness is lowest, that is, turn off. The more time high level lasts, the brighter the LED. <br> Therefore, the last duty cycle is 100%, which correspond to 255, LED is brightest. 25% means darker.<br>
PWM mostly is used for adjusting the LED brightness or rotation speed of motor.<br>
It plays vital role in controlling smart robot car. I believe that you can’t wait to enter next project.<br>




(8) Extension Practice:


Let’s modify the value of delay and remain the pin unchanged, then observe how LED changes.


<pre>
/*
keyestudio Mini Tank Robot V2
lesson 2.2
pwm-slow
http://www.keyestudio.com
*/
int ledPin = 10; // Define the LED pin at D10
int value;
void setup ()
{
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop ()
{
for (value = 0; value <255; value = value + 1)
{
analogWrite (ledPin, value); // LED lights gradually light up
delay (30); // delay 30MS
}
for (value = 255; value> 0; value = value-1)
{
analogWrite (ledPin, value); // LED gradually goes out
delay (30); // delay 30MS
}}//**********************************************************


</pre>


Upload code on the development board and LED gradually get brighter then darker.


=== Project 3: Photoresistor Sensor ===


<br>[[File:0428-56.png|200px|frameless|thumb]]<br>
(1)Description<br>


The photoresistor is a special resistor made of semiconductor materials such as CdS or Selenide septum. The surface is also coated with moisture-proof resin, which has a photoconductive effect. It is sensitive to ambient light. Its resistance varies from different light intensities.<br>


Before uploading the program to the board, let’s demonstrate the function of each symbol in the Arduino IDE toolbar.<br>
We use the characteristics of the photo-resistor to design the circuit and generate the photo-resistor module.<br>  
 
Connecting the signal pin of photocell module to Analog port, when the stronger the light intensity, the greater the voltage of analog port, the greater the analog value is. <br>  
A- Used to verify whether there is any compiling mistakes or not.<br>
On the contrary, the weaker the light intensity, the smaller the voltage of analog port, the smaller the analog value is.  
B- Used to upload the sketch to your Arduino board.<br>
Based on that, we can use the photocell module to read the analog value, so get the ambient light intensity.
C- Used to create shortcut window of a new sketch.<br>
D- Used to directly open an example sketch.<br>
E- Used to save the sketch.<br>
F- Used to send the serial data received from board to the serial monitor.<br>
 
== Projects ==
 
=== Project 1: LED Blink === 
 
<br>[[File:0428-45.png|300px|frameless|thumb]]<br>
 
'''(1)Description'''
 
For the starter and enthusiast, this is a fundamental program---LED Blink.<br>  
LED, the abbreviation of light emitting diodes, consist of Ga, As, P, N chemical compound and so on. The LED can flash diverse color by altering the delay time in the test code. When in control, power on GND and VCC, the LED will be on if S end is high level; nevertheless, it will go off.
 
'''(2)Specification'''


<br>[[File:0428-46.png|300px|frameless|thumb]]<br>
(2)Specification


Control interface: digital port
<br>[[File:0428-57.png|300px|frameless|thumb]]<br>
Working voltage: DC 3.3-5V
Pin spacing: 2.54mm
LED display color: red
'''
(3)Equipment'''


<br>[[File:0428-47.png|500px|frameless|thumb]]<br>
* Resistance:5K ohm-0.5Mohm
* Interface Type: analog
* Working Voltage: 3.3V-5V
* Easy installation: with screw fixing holes
* Pin spacing: 2.54mm


'''(4) V5 Sensor Shield''' <br>             
(3)Equipment
There will be troublesome when we combine Arduino development boards with numerous sensors. However, the V5 sensor shield, compatible with Arduino development board, address this problem perfectly. Just stack V5 board on it.
This sensor shield can be inserted into 3pin sensor modules and breaks out some some communication pins, like serial, IIC, and SPI communication as well.


'''Pins Description'''
<br>[[File:0428-58.png|700px|frameless|thumb]]<br>


<br>[[File:0428-48.png|700px|frameless|thumb]]<br>
'''Connection Diagram:'''


Connection Diagram:
<br>[[File:0428-59.png|500px|frameless|thumb]]<br>
The two photoresistor sensors are linked with A1 and A2, then finish the experiment via photoresistor connected to A1. Then finish the following experiment via photoresistor connected A1. Let’s read its analog value.


<br>[[File:0428-49.png|700px|frameless|thumb]]<br>


Seen from the above diagram, LED is linked with D2
(4)Test Code


(5) Test Code
<pre>


<pre>
/*
/*
  keyestudio Mini Tank Robot V2
  keyestudio Mini Tank Robot V2
  lesson 1.1
  lesson 3.1
  Blink
  photocell
  http://www.keyestudio.com
  http://www.keyestudio.com
*/
*/
void setup()
int sensorPin = A1;    // select the input pin for the photocell
{  
int sensorValue = 0;  // variable to store the value coming from the sensor
    pinMode(2, OUTPUT);// initialize digital pin 2 as an output.
void setup() {
Serial.begin(9600);
}
}
void loop() // the loop function runs over and over again forever
void loop() {
 
sensorValue = analogRead(sensorPin); // read the value from the sensor:
{
Serial.println(sensorValue); //Serial port prints the resistance value
  digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500);
  delay(1000); // wait for a second
  digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
  delay(1000); // wait for a second
}
}
//****************************************************************
//******************************************************
 
</pre>
</pre>


'''(6) Test Result'''
(5)Test Result


(There will be contradict about serial communication between code and  Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.)
<br>[[File:0428-60.png|500px|frameless|thumb]]<br>
Upload the program on the development board, LED flicker with the interval of 1s.


<br>[[File:0428-50.png|500px|frameless|thumb]]<br>


'''(7)Code Explanation'''
Upload code on development board, open serial monitor, check if its value diminishes when covering photoresistor. However, the value increases when uncovered.


pinMode(2,OUTPUT) - Set pin2 to OUTPUT
(6)Code Explanation
analogRead(sensorPin):read the analog value of photoresistor via analog ports.
Serial.begin(9600): Initialize the serial port, baud rate of serial communication is 9600
Serial.println : Serial port prints and word wrap.


digitalWrite(2,HIGH) - When set pin2 to HIGH level(output 5V) or to LOW level(output 0V)


(8)Extension Practice
(7)Extension Practice
We succeed to blink LED. Next, let’s observe what LED will change if we modify pins and delay time.
We’ve known how to read the value of photoresistor. Let’s combine photoresistor with LED and view the status of LED.


<br>[[File:0428-61.png|500px|frameless|thumb]]<br>
PWM restrains the brightness, so LED is linked with PWM pins, connect LED to pin 10, keep pin of photoresistor unchanged, then design the code:


'''Connection Diagram'''
<pre>
/*keyestudio Mini Tank Robot V2
lesson 3.2
photocell-analog output
http://www.keyestudio.com
*/
int analogInPin = A1;  // Analog input pin that the photocell is attached to
int analogOutPin = 10; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}
void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);
  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
Serial.println(sensorValue);  //serial port prints the value of photoresistor
delay(2);
}
//***************************************************************


<br>[[File:0428-51.png|500px|frameless|thumb]]<br>  
</pre>


We’ve altered pins and connected LED to D10.
Upload code, press it by hand and see the the LED brightness.


Test Code:


<pre>
=== Project 4: Servo Control ===


/*
'''(1)Description'''
keyestudio Mini Tank Robot V2
lesson 1.2
delay
http://www.keyestudio.com
*/
void setup() {  // initialize digital pin 10 as an output.
  pinMode(10, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
  digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(100); // wait for 0.1 second
  digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
  delay(100); // wait for 0.1 second
}
//****************************************************************


</pre>
<br>[[File:Ks0194- servo.png|300px|frameless|thumb]]<br>  


The LED flickers faster than before through the test result, therefore, pins and delay time affect flash frequency.
Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtain the voltage difference output.<br>


== Project 2: Adjust LED Brightness ==
When the motor speed is constant, the potentiometer is driven to rotate through the cascade reduction gear, which leads that the voltage difference is 0, and the motor stops rotating. Generally, the angle range of servo rotation is 0° --180 °


(1)Description
The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle.  
In previous lesson, we control LED on and off and make it blink.
In this project, we will control LED brightness through PWM to simulate breathing effect. Similarly, you can change the step length and delay time in the code so as to demonstrate different breathing effect.
PWM is a means of controlling the analog output via digital means. Digital control is used to generate square waves with different duty cycles (a signal that constantly switches between high and low levels) to control the analog output.In general, the input voltage of port are 0V and 5V. What if the 3V is required? Or what if switch among 1V, 3V and 3.5V? We can’t change resistor constantly. For this situation, we need to control by PWM.  


<br>[[File:0085=33.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-69.png|500px|frameless|thumb]]<br>  


For the Arduino digital port voltage output, there are only LOW and HIGH, which correspond to the voltage output of 0V and 5V. You can define LOW as 0 and HIGH as 1, and let the Arduino output five hundred 0 or 1 signals within 1 second.
The corresponding servo angles are shown below:
If output five hundred 1, that is 5V; if all of which is 1, that is 0V. If output 010101010101 in this way then the output port is 2.5V, which is like showing movie. The movie we watch are not completely continuous. It actually outputs 25 pictures per second. In this case, the human can’t tell it, neither does PWM. If want different voltage, need to control the ratio of 0 and 1. The more 0,1 signals output per unit time, the more accurately control.
<br>[[File:0428-70.png|500px|frameless|thumb]]<br>
 
 
 
'''(2)Specification''' <br>
 
* Working voltage: DC 4.8V ~ 6V
* Operating angle range: about 180 ° (at 500 → 2500 μsec)
* Pulse width range: 500 → 2500 μsec
* No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V) 0.1 ± 0.01 sec / 60 (DC 6V)
* No-load current: 200 ± 20mA (DC 4.8V) 220 ± 20mA (DC 6V)
* Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) 1.5 ± 0.1kg · cm (DC 6V)
* Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V)
* Standby current: 3 ± 1mA (DC 4.8V) 4 ± 1mA (DC 6V)
 
 
'''(3)Equipment'''
 
<br>[[File:0428-71.png|500px|frameless|thumb]]<br>
 
'''(4)Connection Diagram:'''


(2) Specification
<br>[[File:0428-72.png|500px|frameless|thumb]]<br>
* Control interface: digital port
* Working voltage: DC 3.3-5V
* Pin spacing: 2.54mm
* Display color: red


Wiring note: the brown line of servo is linked with Gnd(G), the red line is connected to 5v(V) and orange line is attached to digital 9.


(3) Equipment
The servo have to be connected to external power, due to its high demand for driving servo current. Generally, the current of development board is not enough. If without connected power, the development board could be burnt.
<br>[[File:0428-53.png|700px|frameless|thumb]]<br>


(4) Connection Diagram
<br>[[File:0428-51.png|500px|frameless|thumb]]<br>


(5) Test Code
'''(5)Test Code1'''


<pre>
<pre>
/*
/*
keyestudio Mini Tank Robot V2
keyestudio Mini Tank Robot V2
lesson 2.1
lesson 4.1
pwm
Servo
http://www.keyestudio.com
http://www.keyestudio.com
*/
*/
int ledPin = 10; // Define the LED pin at D10
#define servoPin 9  //servo Pin
int value;
int pos; //angle variable of servo
void setup ()  
int pulsewidth; // pulse width variable of servo
{
void setup() {
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
  pinMode(servoPin, OUTPUT); //set servo pin to OUTPUT
  procedure(0); //set the angle of servo to 0°
}
}
void loop ()  
void loop() {
{
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
for (value = 0; value <255; value = value + 1)
    // in steps of 1 degree
{
    procedure(pos);             // tell servo to go to position in variable 'pos'
analogWrite (ledPin, value); // LED lights gradually light up
    delay(15);                   //control the rotation speed of servo
delay (5); // delay 5MS
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    procedure(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                   
  }
}
}
for (value = 255; value> 0; value = value-1)
// function to control servo
{
void procedure(int myangle) {
analogWrite (ledPin, value); // LED gradually goes out
  pulsewidth = myangle * 11 + 500; //calculate the value of pulse width
delay (5); // delay 5MS
  digitalWrite(servoPin,HIGH);
}}//**********************************************************
  delayMicroseconds(pulsewidth);  //The duration of high level is pulse width
 
  digitalWrite(servoPin,LOW);
  delay((20 - pulsewidth / 1000)); // the cycle is 20ms, the low level last for the rest of time
}
//********************************************************************
</pre>
</pre>


(6)Test Result<br>
Upload code successfully, servo swings back in the range of 0° to 180°


Upload test code successfully, LED gradually becomes brighter then darker, like human breath, rather than light on and off immediately.
There is another guide for restraining servo---- servo library file, the following link of official website is for your reference.
https://www.arduino.cc/en/Reference/Servo


(7)Code Explanation
'''<big>Servo library file</big>'''


When we need to repeat some statements, we could use FOR statement.<br>
'''(6)Test Code2'''
FOR statement format is shown below:


<br>[[File:0085=36.png|500px|frameless|thumb]]<br>  
<pre>
 
/*
 
keyestudio Mini Tank Robot V2
 
lesson 4.2
FOR cyclic sequence:
servo
Round 1:1 → 2 → 3 → 4
http://www.keyestudio.com
Round 2:2 → 3 → 4
*/
#include <Servo.h>
Until number 2 is not established, “for”loop is over,
Servo myservo;  // create servo object to control a servo
After knowing this order, go back to code:
// twelve servo objects can be created on most boards
for (int value = 0; value < 255; value=value+1){
int pos = 0;   // variable to store the servo position
        ...
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
}
for (int value = 255; value >0; value=value-1){
void loop() {
      ...
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
  }
}
}
The two“for”statements make value increase from 0 to 255, then reduce from 255 to 0, then increase to 255,....infinitely loop
//****************************************************************
There is a new function in the following ----- analogWrite()
</pre>
We know that digital port only has two state of 0 and 1. So how to send an analog value to a digital value? Here,this function is needed. Let’s observe the Arduino board and find 6 pins marked“~”which can output PWM signals.
Function format as follows:
analogWrite(pin,value)
analogWrite() is used to write an analog value from 0~255 for PWM port, so the value is in the range of 0~255. Attention that you only write the digital pins with PWM function, such as pin 3, 5, 6, 9, 10, 11.
PWM is a technology to obtain analog quantity through digital method. Digital control forms a square wave, and the square wave signal only has two states of turning on and off (that is, high or low levels). By controlling the ratio of the duration of turning on and off, a voltage varying from 0 to 5V can be simulated. The time turning on(academically referred to as high level) is called pulse width, so PWM is also called pulse width modulation.
Through the following five square waves, let’s acknowledge more about PWM.


<br>[[File:0085=37.png|500px|frameless|thumb]]<br>
'''(7)Test Result'''


In the above figure, the green line represents a period, and value of analogWrite() corresponds to a percentage which is called Duty Cycle as well. Duty cycle implies that high-level duration is divided by low-level duration in a cycle. From top to bottom, the duty cycle of first square wave is 0% and its corresponding value is 0. The LED brightness is lowest, that is, turn off. The more time high level lasts, the brighter the LED. Therefore, the last duty cycle is 100%, which correspond to 255, LED is brightest. 25% means darker.
Upload code successfully and power on, servo rotates in the range of to 180°. The result is same. We usually control it by library file.
PWM mostly is used for adjusting the LED brightness or rotation speed of motor.
It plays vital role in controlling smart robot car. I believe that you can’t wait to enter next project.


'''
(8)Code Explanation'''


(8) Extension Practice:
Arduino comes with #include <Servo.h> (servo function and statement)
The following are some common statements of the servo function:
1. attach(interface)——Set servo interface, port 9 and 10 are available


Let’s modify the value of delay and remain the pin unchanged, then observe how LED changes.
3.write(angle)——The statement to set rotation angle of servo, the angle range is from 0° to 180°
3. read()——The statement to read angle of servo, read the command value of “write()”
4. Note: The above written format is “servo variable name, specific statement()”, for instance: myservo.attach(9)


<pre>
=== Project 5: Ultrasonic Sensor ===
/*
keyestudio Mini Tank Robot V2
lesson 2.2
pwm-slow
http://www.keyestudio.com
*/
int ledPin = 10; // Define the LED pin at D10
int value;
void setup ()
{
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop ()
{
for (value = 0; value <255; value = value + 1)
{
analogWrite (ledPin, value); // LED lights gradually light up
delay (30); // delay 30MS
}
for (value = 255; value> 0; value = value-1)
{
analogWrite (ledPin, value); // LED gradually goes out
delay (30); // delay 30MS
}}//**********************************************************


</pre>
'''(1)Description'''


Upload code on the development board and LED gradually get brighter then darker.
<br>[[File:0428-73.png|300px|frameless|thumb]]<br>


=== Project 3: Photoresistor Sensor ===
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules.<br>
The HC-SR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with arduino and ultrasonic sensor and how to use ultrasonic sensor with arduino.<br>


<br>[[File:0428-56.png|200px|frameless|thumb]]<br>
'''(2)Specification'''
(1)Description<br>


The photoresistor is a special resistor made of semiconductor materials such as CdS or Selenide septum. The surface is also coated with moisture-proof resin, which has a photoconductive effect. It is sensitive to ambient light. Its resistance varies from different light intensities.<br>  
<br>[[File:0428-74.png|500px|right|frameless|thumb]]<br>  


We use the characteristics of the photo-resistor to design the circuit and generate the photo-resistor module.<br>
* Power Supply :+5V DC
Connecting the signal pin of photocell module to Analog port, when the stronger the light intensity, the greater the voltage of analog port, the greater the analog value is. <br>
* Quiescent Current : <2mA
On the contrary, the weaker the light intensity, the smaller the voltage of analog port, the smaller the analog value is.  
* Working Current: 15mA
Based on that, we can use the photocell module to read the analog value, so get the ambient light intensity.
* Effectual Angle: <15°
* Ranging Distance : 2cm – 400 cm
* Resolution : 0.3 cm
* Measuring Angle: 30 degree
* Trigger Input Pulse width: 10uS


(2)Specification


<br>[[File:0428-57.png|300px|frameless|thumb]]<br>
'''(3)Equipment'''


* Resistance:5K ohm-0.5Mohm
<br>[[File:0428-75.png|600px|frameless|thumb]]<br>
* Interface Type: analog
* Working Voltage: 3.3V-5V
* Easy installation: with screw fixing holes
* Pin spacing: 2.54mm


(3)Equipment
'''(4)The principle of ultrasonic sensor'''


<br>[[File:0428-58.png|700px|frameless|thumb]]<br>
As the above picture shown, it is like two eyes. One is transmitting end, the other is receiving end.


'''Connection Diagram:'''
The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.
The t is the time that emitting signal meets obstacle and returns.
and the propagation speed of sound in the air is about 343m/s, therefore,  distance = speed * time, because the ultrasonic wave emits and comes back, which is 2 times of distance, so it needs to be divided by 2, the distance measured by ultrasonic wave = (speed * time)/2
 
1.Use method and timing chart of ultrasonic module:
Setting the delay time of Trig pin of SR04 to 10μs at least, which can trigger it to detect distance.
2. After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return. This step will be completed automatically by the module.
3. If the signal returns, the Echo pin will output a high level, and the duration of the high level is the time from the transmission of the ultrasonic wave to the return.


<br>[[File:0428-59.png|500px|frameless|thumb]]<br>
The two photoresistor sensors are linked with A1 and A2, then finish the experiment via photoresistor connected to A1. Then finish the following experiment via photoresistor connected A1. Let’s read its analog value.


<br>[[File:0428-84.png|500px|frameless|thumb]]<br>


(4)Test Code
Circuit diagram of ultrasonic sensor:


<pre>
<br>[[File:0428---106.png|500px|frameless|thumb]]<br> 


/*
keyestudio Mini Tank Robot V2
lesson 3.1
photocell
http://www.keyestudio.com
*/
int sensorPin = A1;    // select the input pin for the photocell
int sensorValue = 0;  // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);  // read the value from the sensor:
Serial.println(sensorValue);  //Serial port prints the resistance value
delay(500);
}
//******************************************************
</pre>


(5)Test Result
'''(5)Connection Diagram'''


<br>[[File:0428-60.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-83.png|500px|frameless|thumb]]<br>  


<br>[[File:0428-85.png|500px|frameless|thumb]]<br>


Upload code on development board, open serial monitor, check if its value diminishes when covering photoresistor. However, the value increases when uncovered.


(6)Code Explanation
'''(6)Test Code'''
analogRead(sensorPin):read the analog value of photoresistor via analog ports.
Serial.begin(9600): Initialize the serial port, baud rate of serial communication is 9600
Serial.println : Serial port prints and word wrap.
 
 
(7)Extension Practice
We’ve known how to read the value of photoresistor. Let’s combine photoresistor with LED and view the status of LED.
 
<br>[[File:0428-61.png|500px|frameless|thumb]]<br>
PWM restrains the brightness, so LED is linked with PWM pins, connect LED to pin 10, keep pin of photoresistor unchanged, then design the code:


<pre>
<pre>
/*keyestudio Mini Tank Robot V2
/*
lesson 3.2
keyestudio Mini Tank Robot V2
photocell-analog output
lesson 5
http://www.keyestudio.com
Ultrasonic sensor
*/
http://www.keyestudio.com
int analogInPin = A1; // Analog input pin that the photocell is attached to
*/  
int analogOutPin = 10; // Analog output pin that the LED is attached to
int trigPin = 5;   // Trigger
int sensorValue = 0;       // value read from the pot
int echoPin = 4;   // Echo
int outputValue = 0;        // value output to the PWM (analog out)
long duration, cm, inches;
void setup() {
void setup() {
Serial.begin(9600);
   //Serial Port begin
}
   Serial.begin (9600);
void loop() {
   //Define inputs and outputs
   // read the analog in value:
   pinMode(trigPin, OUTPUT);
   sensorValue = analogRead(analogInPin);
   pinMode(echoPin, INPUT);
   // map it to the range of the analog out:
   outputValue = map(sensorValue, 0, 1023, 0, 255);
   // change the analog out value:
  analogWrite(analogOutPin, outputValue);
  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
Serial.println(sensorValue);  //serial port prints the value of photoresistor
delay(2);
}
}
//***************************************************************
void loop() {
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
</pre>
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
  digitalWrite(trigPin, LOW);
Upload code, press it by hand and see the the LED brightness.
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
 
  delayMicroseconds(10);
=== Project 4: Servo Control ===
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
'''(1)Description'''
  // duration is the time (in microseconds) from the sending
 
  // of the ping to the reception of its echo off of an object.
<br>[[File:Ks0194- servo.png|300px|frameless|thumb]]<br>  
  duration = pulseIn(echoPin, HIGH);
  // Convert the time into a distance
  cm = (duration/2) / 29.1;    // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;  // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
}
//****************************************************************
</pre>


Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtain the voltage difference output.<br>
(7)Test Result
Upload test code on the development board, open serial monitor and set baud rate to 9600. The detected distance will be displayed, unit is cm and inch. Hinder the ultrasonic sensor by hand, the displayed distance value is smaller.


When the motor speed is constant, the potentiometer is driven to rotate through the cascade reduction gear, which leads that the voltage difference is 0, and the motor stops rotating. Generally, the angle range of servo rotation is 0° --180 °


The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle.  
<br>[[File:0428-86.png|500px|frameless|thumb]]<br>


<br>[[File:0428-69.png|500px|frameless|thumb]]<br>


The corresponding servo angles are shown below:
(8)Code Explanation
<br>[[File:0428-70.png|500px|frameless|thumb]]<br>
int trigPin = 5; this pin is defined pin to transmit ultrasonic waves, generally output.
int echoPin = 4; this is defined as the pin of reception, generally input
cm = (duration/2) / 29.1;  inches = (duration/2) / 74;  by 0.0135
We can calculate the distance by using the following formula:
distance = (traveltime/2) x speed of sound
The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS
Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS
We need to divide the traveltime by 2 because we have to take into account that the wave was sent, hit the object, and then returned back to the sensor.
(9)Extension Practice:
We have just measured the distance displayed by the ultrasonic. How about controlling the LED with the measured distance? Let's try it, connect an LED light module to the D10 pin.


<br>[[File:0428-87.png|500px|frameless|thumb]]<br>


'''(2)Specification''' <br>  
<pre>
 
/*
* Working voltage: DC 4.8V ~ 6V
keyestudio Mini Tank Robot V2
* Operating angle range: about 180 ° (at 500 → 2500 μsec)
lesson 5
* Pulse width range: 500 → 2500 μsec
Ultrasonic LED
* No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V) 0.1 ± 0.01 sec / 60 (DC 6V)
http://www.keyestudio.com
* No-load current: 200 ± 20mA (DC 4.8V) 220 ± 20mA (DC 6V)
*/
* Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) 1.5 ± 0.1kg · cm (DC 6V)
int trigPin = 5;    // Trigger
* Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V)
int echoPin = 4;    // Echo
* Standby current: 3 ± 1mA (DC 4.8V) 4 ± 1mA (DC 6V)
long duration, cm, inches;
 
void setup() {
 
  //Serial Port begin
'''(3)Equipment'''
  Serial.begin (9600);
 
  //Define inputs and outputs
<br>[[File:0428-71.png|500px|frameless|thumb]]<br>
  pinMode(trigPin, OUTPUT);
 
  pinMode(echoPin, INPUT);
'''(4)Connection Diagram:'''
}
 
void loop()  
<br>[[File:0428-72.png|500px|frameless|thumb]]<br>  
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);
  // Convert the time into a distance
  cm = (duration/2) / 29.1;    // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;  // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
if (cm>=2 && cm<=10)
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
}
//****************************************************************
 
</pre>


Wiring note: the brown line of servo is linked with Gnd(G), the red line is connected to 5v(V) and orange line is attached to digital 9.
Upload test code to development board and block ultrasonic sensor by hand, then check if LED is on


The servo have to be connected to external power, due to its high demand for driving servo current. Generally, the current of development board is not enough. If without connected power, the development board could be burnt.
=== Project 6: IR Reception ===


(1)Description<br>
There is no doubt that infrared remote control is ubiquitous in daily life. It is used to control various household appliances, such as TVs, stereos, video recorders and satellite signal receivers. Infrared remote control is composed of infrared transmitting and infrared receiving systems, that is, an infrared remote control and infrared receiving module and a single-chip microcomputer capable of decoding.​    <br>
The 38K infrared carrier signal emitted by remote controller is encoded by the encoding chip in the remote controller. It is composed of a section of pilot code, user code, user inverse code, data code, and data inverse code. The time interval of the pulse is used to distinguish whether it is a 0 or 1 signal and the encoding is made up of these 0, 1 signals. <br>
The user code of the same remote control is unchanged. The data code can distinguish the key.<br>
When the remote control button is pressed, the remote control sends out an infrared carrier signal. When the IR receiver receives the signal, the program will decode the carrier signal and determines which key is pressed. The MCU decodes the received 01 signal, thereby judging what key is pressed by the remote control.<br>
Infrared receiver we use is an infrared receiver module. Mainly composed of an infrared receiver head, it is a device that integrates reception, amplification, and demodulation. Its internal IC has completed demodulation, and can achieve from infrared reception to output and be compatible with TTL signals. Additionally, it is suitable for infrared remote control and infrared data transmission. The infrared receiving module made by the receiver has only three pins, signal line, VCC and GND. It is very convenient to communicate with arduino and other microcontrollers.<br>


'''(5)Test Code1'''
<br>[[File:0428-88.png|300px|frameless|thumb]]<br>


<pre>
 
/*
(2)Specification
 
<br>[[File:0428-89.png|200px|frameless|thumb]]<br>
 
* Operating Voltage: 3.3-5V(DC)
* Interface: 3PIN
* Output Signal: Digital signal
* Receiving Angle: 90 degrees
* Frequency: 38khz
* Receiving Distance: 10m
 
 
 
(3)Equipment
 
<br>[[File:0428-90.png|700px|frameless|thumb]]<br>
 
(4)Connection Diagram
 
<br>[[File:0428-91.png|500px|frameless|thumb]]<br>
 
Respectively link “-”、“+” and S of IR receiver module with G(GND), V(VCC)and A0 of keyestudio development board.<br>
Attention: On the condition that digital ports are not available, analog ports can be regarded as digital ports. A0 equals to D14, A1 is equivalent to digital 15.<br>
 
 
(5)Test Code
 
Firstly import library file of IR receiver module(refer to how to import Arduino library file) before designing code.<br>
 
<pre>
/*
keyestudio Mini Tank Robot V2
keyestudio Mini Tank Robot V2
lesson 4.1
lesson 6
Servo
IRremote
http://www.keyestudio.com
http://www.keyestudio.com
*/
*/  
#define servoPin 9  //servo Pin
#include <IRremoteTank.h>    // IRremote library statement
int pos; //angle variable of servo
int RECV_PIN = A0;       //define the pins of IR receiver as A0
int pulsewidth; // pulse width variable of servo
IRrecv irrecv(RECV_PIN); 
void setup() {
decode_results results;   // decode results exist in the “result” of “decode results”
  pinMode(servoPin, OUTPUT);  //set servo pin to OUTPUT
void setup()
  procedure(0); //set the angle of servo to 0°
{
}
      Serial.begin(9600);   
void loop() {
      irrecv.enableIRIn(); //Enable receiver
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
}
    // in steps of 1 degree
void loop() {
    procedure(pos);              // tell servo to go to position in variable 'pos'
  if (irrecv.decode(&results))//decode successfully, receive a set of infrared signals
    delay(15);                  //control the rotation speed of servo
  {
  }
    Serial.println(results.value, HEX);//Wrap word in 16 HEX to output and receive code
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    irrecv.resume(); // Receive the next value
    procedure(pos);             // tell servo to go to position in variable 'pos'
  } 
    delay(15);                   
  delay(100);   
  }
}
}
//*******************************************************
// function to control servo
 
void procedure(int myangle) {
  pulsewidth = myangle * 11 + 500; //calculate the value of pulse width
  digitalWrite(servoPin,HIGH);
  delayMicroseconds(pulsewidth);  //The duration of high level is pulse width
  digitalWrite(servoPin,LOW);
  delay((20 - pulsewidth / 1000));  // the cycle is 20ms, the low level last for the rest of time
}
//********************************************************************
</pre>
</pre>


Upload code successfully, servo swings back in the range of 0° to 180°


There is another guide for restraining servo---- servo library file, the following link of official website is for your reference.
(6)Test Result
https://www.arduino.cc/en/Reference/Servo
 
Upload test code, open serial monitor and set baud rate to 9600, point remote control to IR receiver and the corresponding value will be shown, if pressing so long, the error codes will appear.<br>
 
<br>[[File:0428-92.png|500px|frameless|thumb]]<br>
 
Below we have listed out each button value of keyestudio remote control. So you can keep it for reference. <br>
 
<br>[[File:0428-93.png|500px|frameless|thumb]]<br>
 
(7)Code Explanation<br>
irrecv.enableIRIn(): after enabling IR decoding, the IR signals will be received, then function“decode()”will check continuously if decode successfully.<br>
 
irrecv.decode(&results): after decoding successfully, this function will come back to “true”, and keep result in “results”. After decoding a IR signals, run the resume()function and receive the next signal
(8)Extension Practice
We decoded the key value of IR remote control. How about restrain LED by the measured value? We could operate an experiment to affirm. Attach a LED to D10. When keep the pin of IR receiver unchanged, LED will be on when pressing the key of remote control; press again, LED will be off.


'''<big>Servo library file</big>'''
<br>[[File:0428-94.png|500px|frameless|thumb]]<br>  


'''(6)Test Code2'''


<pre>
<pre>
/*
/* keyestudio Mini Tank Robot V2
keyestudio Mini Tank Robot V2
lesson 6.2
lesson 4.2
IRremote
servo
http://www.keyestudio.com
http://www.keyestudio.com
*/  
*/
#include <IRremoteTank.h>
#include <Servo.h>
int RECV_PIN = A0;//define the pin of IR receiver as A0
Servo myservo; // create servo object to control a servo
int LED_PIN=10;//define the pin of LED
// twelve servo objects can be created on most boards
int a=0;
int pos = 0;   // variable to store the servo position
IRrecv irrecv(RECV_PIN);
void setup() {
decode_results results;
   myservo.attach(9); // attaches the servo on pin 9 to the servo object
void setup()
{
   Serial.begin(9600);
  irrecv.enableIRIn(); // Initialize the IR receiver
  pinMode(LED_PIN,OUTPUT);//set LED_pin to OUTPUT
}
}
void loop() {
void loop() {
   for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
   if (irrecv.decode(&results)) {
    // in steps of 1 degree
if(results.value==0xFF02FD &a==0) // according to the above key value, press“OK”on remote control , LED will be controlled
     myservo.write(pos);             // tell servo to go to position in variable 'pos'
{
    delay(15);                       // waits 15ms for the servo to reach the position
     digitalWrite(LED_PIN,HIGH);//LED will be on
  }
a=1;
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
}
    myservo.write(pos);             // tell servo to go to position in variable 'pos'
else if(results.value==0xFF02FD &a==1) //press again
    delay(15);                       // waits 15ms for the servo to reach the position
{
  }
digitalWrite(LED_PIN,LOW);//LED will go off
a=0;
}
}
//****************************************************************
    irrecv.resume(); //receive the next value
  }
}
//*******************************************************
</pre>
</pre>


'''(7)Test Result'''
Upload code to development board, press“OK”key on remote control to make LED on and off.


Upload code successfully and power on, servo rotates in the range of 0° to 180°. The result is same. We usually control it by library file.
=== Project 7: Bluetooth Remote Control ===


'''
(1)Description
(8)Code Explanation'''


Arduino comes with #include <Servo.h> (servo function and statement)
<br>[[File:0428-95.png|300px|frameless|thumb]]<br>
The following are some common statements of the servo function:
1. attach(interface)——Set servo interface, port 9 and 10 are available


3.write(angle)——The statement to set rotation angle of servo, the angle range is from 0° to 180°
Bluetooth, a simple wireless communication module most popular since the last few decades and easy to use are being used in most of the battery-powered devices.<br>
3. read()——The statement to read angle of servo, read the command value of “write()”
Over the years, there have been many upgrades of Bluetooth standard to keep fulfil the demand of customers and technology according to the need of time and situation.
4. Note: The above written format is “servo variable name, specific statement()”, for instance: myservo.attach(9)
Over the few years, there are many things changed including data transmission rate, power consumption with wearable and IoT Devices and Security System.<br>
Here we are going to learn about HM-10 BLE 4.0 with Arduino Board. The HM-10 is a readily available Bluetooth 4.0 module. This module is used for establishing wireless data communication. The module is designed by using the Texas Instruments CC2540 or CC2541 Bluetooth low energy (BLE) System on Chip (SoC). <br>


== Project 5: Ultrasonic Sensor ==


'''(1)Description'''
(2)Specification


<br>[[File:0428-73.png|300px|frameless|thumb]]<br>
* Bluetooth protocol: Bluetooth Specification V4.0 BLE
* No byte limit in serial port Transceiving
* In open environment, realize 100m ultra-distance communication with iphone4s
* Working frequency: 2.4GHz ISM band
* Modulation method: GFSK(Gaussian Frequency Shift Keying)
* Transmission power: -23dbm, -6dbm, 0dbm, 6dbm, can be modified by AT command.
* Sensitivity: ≤-84dBm at 0.1% BER
* Transmission rate: Asynchronous: 6K bytes ; Synchronous: 6k Bytes
* Security feature: Authentication and encryption
* Supporting service: Central & Peripheral UUID FFE0, FFE1
* Power consumption: Auto sleep mode, stand by current 400uA~800uA, 8.5mA during transmission.
* Power supply: 5V DC
* Working temperature: –5 to +65 Centigrade


The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules.
The HC-SR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with arduino and ultrasonic sensor and how to use ultrasonic sensor with arduino.


'''(2)Specification'''
(3)Equipment


<br>[[File:0428-74.png|500px|right|frameless|thumb]]<br>  
<br>[[File:0428-96.png|600px|frameless|thumb]]<br>  


* Power Supply :+5V DC
(4)Connection Diagram
* Quiescent Current : <2mA
* Working Current: 15mA
* Effectual Angle: <15°
* Ranging Distance : 2cm – 400 cm
* Resolution : 0.3 cm
* Measuring Angle: 30 degree
* Trigger Input Pulse width: 10uS


* 1. STATE: state test pins, connected to internal LED, generally keep it unconnected.
* 2. RXD: serial interface, receiving terminal.
* 3. TXD: serial interface, transmitting terminal.
* 4. GND: Ground.
* 5. VCC: positive pole of the power source.
* 6. EN/BRK: break connect, it means breaking the Bluetooth connection, generally, keep it unconnected.


'''(3)Equipment'''
<br>[[File:0428-97.png|500px|frameless|thumb]]<br>


<br>[[File:0428-75.png|600px|frameless|thumb]]<br>
(5)Test Code


'''(4)The principle of ultrasonic sensor'''
<pre>
/*
keyestudio Mini Tank Robot v2.0
lesson 7.1
bluetooth
http://www.keyestudio.com
*/


As the above picture shown, it is like two eyes. One is transmitting end, the other is receiving end.
char ble_val; //character variable: save the value of Bluetooth reception


The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal.  
void setup() {
The t is the time that emitting signal meets obstacle and returns.
  Serial.begin(9600);
and the propagation speed of sound in the air is about 343m/s, therefore, distance = speed * time, because the ultrasonic wave emits and comes back, which is 2 times of distance, so it needs to be divided by 2, the distance measured by ultrasonic wave = (speed * time)/2
}
void loop() {
  if(Serial.available() > 0)  //Judge if there is data in serial buffer
  {
    ble_val = Serial.read();  //Read data from serial buffer
    Serial.println(ble_val);  //Print
  }}
//*******************************************
</pre>
 
 
(There will be contradiction between serial communication of code and communication of Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.)<br>
After uploading code on development board, then insert Bluetooth module, wait for the command from cellphone.<br>
 
(6)Download APP


1.Use method and timing chart of ultrasonic module:
<span style=color:red>Allow APP to access “location” in settings of your cellphone when connecting to Bluetooth module.</span>
Setting the delay time of Trig pin of SR04 to 10μs at least, which can trigger it to detect distance.
2. After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return. This step will be completed automatically by the module.
3. If the signal returns, the Echo pin will output a high level, and the duration of the high level is the time from the transmission of the ultrasonic wave to the return.


The code is reading the received signal, and we also need a stuff to send signal. In this project, we send signal to control robot car via cellphone.<br>
Then we need to download the APP.<br>


<br>[[File:0428-84.png|500px|frameless|thumb]]<br>
'''1.For iOS system'''


Circuit diagram of ultrasonic sensor:
Enter APP STORE to search BLE Scanner 4.0, then download it.


<br>[[File:0428-82.png|500px|frameless|thumb]]<br>
<br>[[File:0428-98.png|300px|frameless|thumb]]<br>  


'''2.For Android system'''


'''(5)Connection Diagram'''
Enter Google Play to find out BLE Scanner, then download.<br>
And allow APP to access“location”, you could enable “location”in settings of your cellphone.<br>


<br>[[File:0428-83.png|500px|frameless|thumb]]<br>


<br>[[File:0428-85.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-99.png|300px|frameless|thumb]]<br>  


* 3.After installation, open App and enable “Location and Bluetooth” permission.
* 4.We take iOS version as example. The operation method of Android version is almost same as it.
* 5.Scan Bluetooth module to get Bluetooth BLE 4.0. Its name is HMSoft.
Then click“connect”to link with Bluetooth and use it.


'''(6)Test Code'''
<br>[[File:0428-100.png|300px|frameless|thumb]]<br>


<pre>
6. After connecting to HMSoft, click it to get multiple options, such as device information, access permission, general and custom service. Choose “CUSTOM SERVICE”
/*
 
  keyestudio Mini Tank Robot V2
<br>[[File:0428-101.png|300px|frameless|thumb]]<br>
  lesson 5
 
  Ultrasonic sensor
7.Then pop up the following page
 
<br>[[File:0428-102.png|300px|frameless|thumb]]<br>
 
8. Click(Read,Notify,WriteWithoutResponse)to enter the following page
 
<br>[[File:0428-103.png|300px|frameless|thumb]]<br>
 
9.Click Write Value, appear the interface to enter HEX or Text.<br>
 
<br>[[File:0428-104.png|300px|frameless|thumb]]<br>
 
10.Open the serial monitor on Arduino,enter a 0 or other character at Text interface.<br>
 
<br>[[File:0428-105.png|300px|frameless|thumb]]<br>
 
Then click“Write”, open serial monitor to view if there is a “0” signal
<br>[[File:0428-106.png|300px|frameless|thumb]]<br>
 
 
(7)Code Explanation
 
Serial.available() : The current rest characters when return to buffer area. Generally, this function is used to judge if there is data in buffer. When Serial.available()>0, it means that serial receives the data and can be read<br>
Serial.read():Read a data of a Byte in buffer of serial port, for instance, device sends data to Arduino via serial port, then we could read data by “Serial.read()”
(8)Extension Practice<br>
We could send a command via cellphone to turn on and off a LED. <br>
D10 is connected to a LED, as shown below:<br>
 
<br>[[File:0428-107.png|500px|frameless|thumb]]<br>
 
<pre>
/*
  keyestudio Mini Tank Robot v2.0
  lesson 7.2
  Bluetooth
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/  
int trigPin = 5;    // Trigger
int ledpin=11;
int echoPin = 4;    // Echo
void setup()
long duration, cm, inches;
{Serial.begin(9600);
void setup() {
pinMode(ledpin,OUTPUT);
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
}
void loop() {
void loop()
   // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
{ int i;
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   if (Serial.available())
  digitalWrite(trigPin, LOW);
   {i=Serial.read();
   delayMicroseconds(2);
    Serial.println("DATA RECEIVED:");
  digitalWrite(trigPin, HIGH);
    if(i=='1')
  delayMicroseconds(10);
    { digitalWrite(ledpin,1);
  digitalWrite(trigPin, LOW);
      Serial.println("led on");
  // Read the signal from the sensor: a HIGH pulse whose
    }
  // duration is the time (in microseconds) from the sending
    if(i=='0')
  // of the ping to the reception of its echo off of an object.
    { digitalWrite(ledpin,0);
  duration = pulseIn(echoPin, HIGH);
      Serial.println("led off");
  // Convert the time into a distance
}}}//*******************************************
  cm = (duration/2) / 29.1;    // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;  // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
}
//****************************************************************
</pre>
</pre>


(7)Test Result
<br>[[File:0428-108.png|300px|frameless|thumb]] [[File:0428-105.png|300px|frameless|thumb]]<br>
Upload test code on the development board, open serial monitor and set baud rate to 9600. The detected distance will be displayed, unit is cm and inch. Hinder the ultrasonic sensor by hand, the displayed distance value is smaller.
 
Click “Write” on APP, when you enter 1, LED will be on, when you input 0, LED will be off. (Remember to remove the Bluetooth module after finishing experiment, otherwise, burning code will be affected)
 
=== Project 8: Motor Driving and Speed Control ===


(1) Description


<br>[[File:0428-86.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-110.png|300px|frameless|thumb]]<br>  


There are many ways to drive the motor. Our tank robot uses the most commonly used L298P solution. L298P is an excellent high-power motor driver IC produced by STMicroelectronics. It can directly drive DC motors, two-phase and four-phase stepping motors. The driving current up to 2A, and output terminal of motor adopts eight high-speed Schottky diodes as protection.<br>
We designed a shield based on the circuit of L298p. <br>
The stacked design reduces the technical difficulty of using and driving the motor.<br>


(8)Code Explanation
(2)Specification
int trigPin = 5; this pin is defined pin to transmit ultrasonic waves, generally output.
int echoPin = 4; this is defined as the pin of reception, generally input
cm = (duration/2) / 29.1;  inches = (duration/2) / 74;  by 0.0135
We can calculate the distance by using the following formula:
distance = (traveltime/2) x speed of sound
The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS
Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS
We need to divide the traveltime by 2 because we have to take into account that the wave was sent, hit the object, and then returned back to the sensor.
(9)Extension Practice:
We have just measured the distance displayed by the ultrasonic. How about controlling the LED with the measured distance? Let's try it, connect an LED light module to the D10 pin.


<br>[[File:0428-87.png|500px|frameless|thumb]]<br>
Circuit Diagram for L298P Board


<pre>
<br>[[File:0428-111.png|500px|frameless|thumb]]<br>
/*
 
  keyestudio Mini Tank Robot V2
* 1)Logic part input voltage: DC5V
  lesson 5
* 2)Driving part input voltage: DC 7-12V
  Ultrasonic LED
* 3)Logic part working current: <36mA
* 4)Driving part working current: <2A
* 5)Maximum power dissipation: 25W (T=75℃)
* 6)Working temperature: -25℃~+130℃
* 7)Control signal input level: high level 2.3V<Vin<5V, low level -0.3V<Vin<1.5V
 
<br>[[File:0428-112.png|500px|frameless|thumb]]<br>
 
(3)Drive Robot to Move
 
Through the above circuit diagram, the direction pin of A motor is D12, and speed pin is D3; D13 is the direction pin of B motor, D11 is speed pin.<br>
We know how to control digital ports according to the following chart.<br>
PWM decides 2 motors to turn so as to drive robot car. The PWM value is in the range of 0-255, the larger the number, the faster the motor rotates.<br>
 
<br>[[File:0428-113.png|500px|frameless|thumb]]<br>
 
(4)Equipment
 
<br>[[File:0428-114.png|700px|frameless|thumb]]<br>
 
(5)Connection Diagram
 
<br>[[File:04288-2.png|500px|frameless|thumb]]<br>
 
Note: the 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4. <br>
 
(6)Test Code
 
<pre>
/*
  keyestudio Mini Tank Robot v2.0
  lesson 8.1
  motor driver
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/  
int trigPin = 5;    // Trigger
 
int echoPin = 4;    // Echo
#define ML_Ctrl 13  //define the direction control pin of left motor
long duration, cm, inches;
#define ML_PWM 11  //define the PWM control pin of left motor
void setup() {
#define MR_Ctrl 12  //define direction control pin of right motor
   //Serial Port begin
#define MR_PWM 3  // define the PWM control pin of right motor
   Serial.begin (9600);
 
  //Define inputs and outputs
void setup()
   pinMode(trigPin, OUTPUT);
{
   pinMode(echoPin, INPUT);
   pinMode(ML_Ctrl, OUTPUT);//define direction control pin of left motor as output
   pinMode(ML_PWM, OUTPUT);//define PWM control pin of left motor as output
   pinMode(MR_Ctrl, OUTPUT);//define direction control pin of right motor as output.
   pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as output
}
}
void loop()
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);
  // Convert the time into a distance
  cm = (duration/2) / 29.1;    // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;  // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
if (cm>=2 && cm<=10)
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
}
//****************************************************************


</pre>
void loop()
{
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
 
  //front
  delay(2000);//delay in 2s
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200 
digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200


Upload test code to development board and block ultrasonic sensor by hand, then check if LED is on
  //back
  delay(2000);//delay in 2s
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200


== Project 6: IR Reception ==
    //left
 
  delay(2000);//delay in 2s
(1)Description
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
There is no doubt that infrared remote control is ubiquitous in daily life. It is used to control various household appliances, such as TVs, stereos, video recorders and satellite signal receivers. Infrared remote control is composed of infrared transmitting and infrared receiving systems, that is, an infrared remote control and infrared receiving module and a single-chip microcomputer capable of decoding.​    
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
The 38K infrared carrier signal emitted by remote controller is encoded by the encoding chip in the remote controller. It is composed of a section of pilot code, user code, user inverse code, data code, and data inverse code. The time interval of the pulse is used to distinguish whether it is a 0 or 1 signal and the encoding is made up of these 0, 1 signals.
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
The user code of the same remote control is unchanged. The data code can distinguish the key.
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
When the remote control button is pressed, the remote control sends out an infrared carrier signal. When the IR receiver receives the signal, the program will decode the carrier signal and determines which key is pressed. The MCU decodes the received 01 signal, thereby judging what key is pressed by the remote control.
Infrared receiver we use is an infrared receiver module. Mainly composed of an infrared receiver head, it is a device that integrates reception, amplification, and demodulation. Its internal IC has completed demodulation, and can achieve from infrared reception to output and be compatible with TTL signals. Additionally, it is suitable for infrared remote control and infrared data transmission. The infrared receiving module made by the receiver has only three pins, signal line, VCC and GND. It is very convenient to communicate with arduino and other microcontrollers.


<br>[[File:0428-88.png|300px|frameless|thumb]]<br>
  //right
  delay(2000);//delay in 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);//set the PWM control speed of right motor to 0


    //stop
  delay(2000);//delay in 2s
}//*****************************************


(2)Specification
</pre>


<br>[[File:0428-89.png|200px|frameless|thumb]]<br>


* Operating Voltage: 3.3-5V(DC)
(7)Test Result
* Interface: 3PIN
* Output Signal: Digital signal
* Receiving Angle: 90 degrees
* Frequency: 38khz
* Receiving Distance: 10m


Hook up by connection diagram, upload code and power on, smart car goes forward and back for 2s, turns left and right for 2s, stops for 2s and alternately.<br>
(8) Code Explanation<br>
digitalWrite(ML_Ctrl,LOW): the rotation direction of motor is decided by the high/low level and and the pins that decide rotation direction are general pins.<br>
analogWrite(ML_PWM,200): the speed of motor is regulated by PWM, The speed of motor is regulated by PWM, and the pins that decide the speed of motor have to be PWM pins.<br>




(3)Equipment
(9)Extension Practice


<br>[[File:0428-90.png|700px|frameless|thumb]]<br>
Hook up in same way


(4)Connection Diagram
<br>[[File:04288-2.png|500px|frameless|thumb]]<br>  
 
<br>[[File:0428-91.png|500px|frameless|thumb]]<br>  
 
Respectively link “-”、“+” and S of IR receiver module with G(GND), V(VCC)and A0 of keyestudio development board.
Attention: On the condition that digital ports are not available, analog ports can be regarded as digital ports. A0 equals to D14, A1 is equivalent to digital 15.
 
 
(5)Test Code
 
Firstly import library file of IR receiver module(refer to how to import Arduino library file) before designing code.


<pre>
<pre>
/*
/*
keyestudio Mini Tank Robot V2
keyestudio Mini Tank Robot v2.0
lesson 6
lesson 8.2
IRremote
motor driver pwm
http://www.keyestudio.com
http://www.keyestudio.com
*/  
*/  
#include <IRremote.h>    // IRremote library statement
#define ML_Ctrl 13  //define the direction control pin of left motor
int RECV_PIN = A0;        //define the pins of IR receiver as A0
#define ML_PWM 11  //define the PWM control pin of left motor
IRrecv irrecv(RECV_PIN); 
#define MR_Ctrl 12  //define the control pin of right motor
decode_results results;   // decode results exist in the “result” of “decode results”
#define MR_PWM 3   //define the PWM control pin of right motor
void setup()
void setup()
{
{ pinMode(ML_Ctrl, OUTPUT);//define the direction control pin of left motor as OUTPUT
      Serial.begin(9600); 
  pinMode(ML_PWM, OUTPUT);//define the PWM control pin of left motor as OUTPUT
      irrecv.enableIRIn(); //Enable receiver
  pinMode(MR_Ctrl, OUTPUT);//define the direction control pin of right motor as OUTPUT
  pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as OUTPUT
void loop() { 
  if (irrecv.decode(&results))//decode successfully, receive a set of infrared signals
  { 
    Serial.println(results.value, HEX);//Wrap word in 16 HEX to output and receive code
    irrecv.resume(); // Receive the next value
  } 
  delay(100);
//*******************************************************


</pre>
}
void loop()
{ digitalWrite(ML_Ctrl,LOW);//Set direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);// Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
  //front
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,HIGH);//Set direction control pin of right motor to HIGH level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
  //back
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set direction control pin of right motor to LOW level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
    //left
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,100);//set the PWM control speed of right motor to 100
  //right
  delay(2000);//define 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);// set the PWM control speed of right motor to 0


    //stop
  delay(2000);//define 2s
}//******************************************************************


(6)Test Result
</pre>
Upload code successfully, the motors rotate faster.


Upload test code, open serial monitor and set baud rate to 9600, point remote control to IR receiver and the corresponding value will be shown, if pressing so long, the error codes will appear.
=== Project 9: LED Expression Panel ===


<br>[[File:0428-92.png|500px|frameless|thumb]]<br>
(1)Description


Below we have listed out each button value of keyestudio remote control. So you can keep it for reference.  
<br>[[File:0428-120.png|700px|frameless|thumb]]<br>


<br>[[File:0428-93.png|500px|frameless|thumb]]<br>
If we add an expression panel to the robot, it will be amazing. Keyestudio's 8*16 dot matrix can meet your requirements. You can create facial expressions, patterns or other interesting displays yourself. 8*16 LED light board comes with 128 LEDs. The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface, so as to control the 128 LEDs on the module, which produce the patterns you need on dot matrix. To facilitate wiring, we also provide a HX-2.54 4Pin wiring.


(7)Code Explanation
irrecv.enableIRIn(): after enabling IR decoding, the IR signals will be received, then function“decode()”will check continuously if decode successfully.


irrecv.decode(&results): after decoding successfully, this function will come back to “true”, and keep result in “results”. After decoding a IR signals, run the resume()function and receive the next signal
(2)Specification
(8)Extension Practice
 
We decoded the key value of IR remote control. How about restrain LED by the measured value? We could operate an experiment to affirm. Attach a LED to D10. When keep the pin of IR receiver unchanged, LED will be on when pressing the key of remote control; press again, LED will be off.
* Working voltage: DC 3.3-5V
* Power loss: 400mW
* Oscillation frequency: 450KHz     
* Drive current: 200mA
* Working temperature: -40~80℃
* Communication method: two-wire bus
 
(3)Equipment
 
<br>[[File:0428-121.png|700px|frameless|thumb]]<br>
 
(4)8*16 Dot Matrix Display
 
Circuit Graphic
 
<br>[[File:0428-122.png|700px|frameless|thumb]]<br>


<br>[[File:0428-94.png|500px|frameless|thumb]]<br>  
The principle of 8*16 dot matrix:<br>  
How to control each led light of 8*16 dot matrix? We know that a byte has 8 bits, each bit is 0 or 1. When a bit is 0, turn off LED and when a bit is 0, turn on LED. <br> Thereby, one byte can control the LED in a row of dot matrix, so 16 bytes can control 16 columns of led lights, that is, 8*16 dot matrix.<br>  


Interface Description and Communication Protocol:<br>
The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface.<br>
The communication protocol diagram is shown below:
(SCLK) is SCL, (DIN) is SDA:


<pre>
<br>[[File:0428-123.png|700px|frameless|thumb]]<br>  
/* keyestudio Mini Tank Robot V2
lesson 6.2
IRremote
http://www.keyestudio.com
*/
#include <IRremote.h>
int RECV_PIN = A0;//define the pin of IR receiver as A0
int LED_PIN=10;//define the pin of LED
int a=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Initialize the IR receiver
  pinMode(LED_PIN,OUTPUT);//set LED_pin to OUTPUT
}
void loop() {
  if (irrecv.decode(&results)) {
if(results.value==0xFF02FD &a==0) // according to the above key value, press“OK”on remote control , LED will be controlled
{
    digitalWrite(LED_PIN,HIGH);//LED will be on
a=1;
}
else if(results.value==0xFF02FD &a==1) //press again
{
digitalWrite(LED_PIN,LOW);//LED will go off
a=0;
}
    irrecv.resume(); //receive the next value
  }
}
//*******************************************************
</pre>


Upload code to development board, press“OK”key on remote control to make LED on and off.
①The starting condition for data input: SCL is high level and SDA changes from high to low.<br>
②For data command setting, there are methods as shown in the figure below. <br>
In our sample program, select the way to add 1 to the address automatically, the binary value is 0100 0000 and the corresponding hexadecimal value is 0x40.<br>


== Project 7: Bluetooth Remote Control ==
<br>[[File:0428-124.png|700px|frameless|thumb]]<br>


(1)Description
③For address command setting, the address can be selected as shown below.
The first 00H is selected in our sample program, and the binary number 1100 0000 corresponds to the hexadecimal 0xc0


<br>[[File:0428-95.png|300px|frameless|thumb]]<br>  
<br>[[File:0428-125.png|700px|frameless|thumb]]<br>  


Bluetooth, a simple wireless communication module most popular since the last few decades and easy to use are being used in most of the battery-powered devices.
* ④The requirement for data input is that SCL is high level when inputting data, the signal on SDA must remain unchanged. Only when the clock signal on SCL is low level, the signal on SDA can be altered. The data input is low-order first, high-order is behind
Over the years, there have been many upgrades of Bluetooth standard to keep fulfil the demand of customers and technology according to the need of time and situation.
* ⑤ The condition to end data transmission is that when SCL is low, SDA is low, and when SCL is high, the SDA level also becomes high.
Over the few years, there are many things changed including data transmission rate, power consumption with wearable and IoT Devices and Security System.
* ⑥ Display control, set different pulse width, the pulse width can be selected as shown below
Here we are going to learn about HM-10 BLE 4.0 with Arduino Board. The HM-10 is a readily available Bluetooth 4.0 module. This module is used for establishing wireless data communication. The module is designed by using the Texas Instruments CC2540 or CC2541 Bluetooth low energy (BLE) System on Chip (SoC).
* In the example, we choose pulse width 4/16, and the hexadecimal corresponds to 1000 1010 is 0x8A




(2)Specification
<br>[[File:0428-126.png|700px|frameless|thumb]]<br>


* Bluetooth protocol: Bluetooth Specification V4.0 BLE
4. Introduction for Modulus Tool
* No byte limit in serial port Transceiving
 
* In open environment, realize 100m ultra-distance communication with iphone4s
The online version of dot matrix modulus tool:
* Working frequency: 2.4GHz ISM band
http://dotmatrixtool.com/#
* Modulation method: GFSK(Gaussian Frequency Shift Keying)
* Transmission power: -23dbm, -6dbm, 0dbm, 6dbm, can be modified by AT command.
* Sensitivity: ≤-84dBm at 0.1% BER
* Transmission rate: Asynchronous: 6K bytes ; Synchronous: 6k Bytes
* Security feature: Authentication and encryption
* Supporting service: Central & Peripheral UUID FFE0, FFE1
* Power consumption: Auto sleep mode, stand by current 400uA~800uA, 8.5mA during transmission.
* Power supply: 5V DC
* Working temperature: –5 to +65 Centigrade


①Open links to enter the following page.


(3)Equipment
<br>[[File:0428-127.png|700px|frameless|thumb]]<br>


<br>[[File:0428-96.png|600px|frameless|thumb]]<br>
②The dot matrix is 8*16 in this project, so set the height to 8, width to 16, as shown below.


(4)Connection Diagram
<br>[[File:0428-128.png|700px|frameless|thumb]]<br>


* 1. STATE: state test pins, connected to internal LED, generally keep it unconnected.
③ Generate hexadecimal data from the pattern
* 2. RXD: serial interface, receiving terminal.
As shown below, press the left mouse button to select, the right button to cancel, draw the pattern you want, click Generate, and the hexadecimal data we need will be produced.
* 3. TXD: serial interface, transmitting terminal.
* 4. GND: Ground.
* 5. VCC: positive pole of the power source.
* 6. EN/BRK: break connect, it means breaking the Bluetooth connection, generally, keep it unconnected.


<br>[[File:0428-97.png|500px|frameless|thumb]]<br>  
<br>[[File:0428-129.png|700px|frameless|thumb]]<br>  


(5)Test Code


<pre>
(5)Connection Diagram
/*
keyestudio Mini Tank Robot v2.0
lesson 7.1
bluetooth
http://www.keyestudio.com
*/


char ble_val; //character variable: save the value of Bluetooth reception
<br>[[File:0428-130.png|700px|frameless|thumb]]<br>


void setup() {
  Serial.begin(9600);
}
void loop() {
  if(Serial.available() > 0)  //Judge if there is data in serial buffer
  {
    ble_val = Serial.read();  //Read data from serial buffer
    Serial.println(ble_val);  //Print
  }}
//*******************************************
</pre>


Wiring note: The GND, VCC, SDA, and SCL of the 8x16 LED panel are respectively connected to -(GND), + (VCC), A4 and A5 of the keyestudio sensor expansion board for two-wire serial communication. (Note: This pin is connected to arduino IIC, but this module is not IIC communication, it can be linked with any two pins.)


(There will be contradiction between serial communication of code and communication of Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.)
After uploading code on development board, then insert Bluetooth module, wait for the command from cellphone.


(6)Download APP
(6)Test Code


The code is reading the received signal, and we also need a stuff to send signal. In this project, we send signal to control robot car via cellphone.
The code that shows smile face
Then we need to download the APP.


'''1.For iOS system'''
<pre>
/*
keyestudio Mini Tank Robot v2.0
lesson 9.1
Matrix  face
http://www.keyestudio.com
*/
//the data of smiley from modulus tool
unsigned char smile[] = {0x00, 0x00, 0x1c, 0x02, 0x02, 0x02, 0x5c, 0x40, 0x40, 0x5c, 0x02, 0x02, 0x02, 0x1c, 0x00, 0x00};


Enter APP STORE to search BLE Scanner 4.0, then download it.
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4


<br>[[File:0428-98.png|300px|frameless|thumb]]<br>
void setup(){
  //Set pin to output
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  //clear the display
  //matrix_display(clear);
}
void loop(){
  matrix_display(smile);  // display smile face
}
// the function for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();  // use the function of the data transmission start condition
  IIC_send(0xc0);  //select address
 
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
    IIC_send(matrix_value[i]); //convey the pattern data
  }


'''2.For Android system'''
  IIC_end();  //end the transmission of pattern data 
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16 s
  IIC_end();
}


Enter Google Play to find out BLE Scanner, then download.
//the condition to start conveying data
And allow APP to access“location”, you could enable “location”in settings of your cellphone.
void IIC_start()
 
{
 
  digitalWrite(SCL_Pin,HIGH);
<br>[[File:0428-99.png|300px|frameless|thumb]]<br>
  delayMicroseconds(3);
 
  digitalWrite(SDA_Pin,HIGH);
3.After installation, open App and enable “Location and Bluetooth” permission.
  delayMicroseconds(3);
4.We take iOS version as example. The operation method of Android version is almost same as it.
  digitalWrite(SDA_Pin,LOW);
5.Scan Bluetooth module to get Bluetooth BLE 4.0. Its name is HMSoft.
  delayMicroseconds(3);
Then click“connect”to link with Bluetooth and use it.
}
//Convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bit for every character
  {
      digitalWrite(SCL_Pin,LOW);  // pull down clock pin SCL_Pin to change the signal of SDA
      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up the clock pin SCL_Pin to stop transmission
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, shift the data to the right by one
  }
}


<br>[[File:0428-100.png|300px|frameless|thumb]]<br>
//The sign of ending data transmission
 
void IIC_end()
6. After connecting to HMSoft, click it to get multiple options, such as device information, access permission, general and custom service. Choose “CUSTOM SERVICE”
{
 
  digitalWrite(SCL_Pin,LOW);
<br>[[File:0428-101.png|300px|frameless|thumb]]<br>
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
//******************************************************


7.Then pop up the following page
</pre>


<br>[[File:0428-102.png|300px|frameless|thumb]]<br>  
(7)Test Result<br>
Wire according to connection diagram. The DIP switch is dialed to right end and power on, the smile face appears on dot matrix.<br>
<br>[[File:0428-131.png|500px|frameless|thumb]]<br>  


8. Click(Read,Notify,WriteWithoutResponse)to enter the following page


<br>[[File:0428-103.png|300px|frameless|thumb]]<br>
(8)Extension Practice


9.Click Write Value, appear the interface to enter HEX or Text.
We use the modulo tool (http://dotmatrixtool.com/#)to make the dot matrix alternately display start, go front and stop patterns then clear the patterns, the time interval is 2000 milliseconds.


<br>[[File:0428-104.png|300px|frameless|thumb]]<br>  
<br>[[File:0428-132.png|500px|frameless|thumb]]<br>  


10.Open the serial monitor on Arduino,enter a 0 or other character at Text interface.
Get the graphical code to be displayed via modulus tool<br>


<br>[[File:0428-105.png|300px|frameless|thumb]]<br>
* Start:
* 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
* Go front:
* 0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00
* Go back:
* 0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00
* Turn left:
* 0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00
* Turn left:
* 0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00
* Stop:
* 0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00
* Clear the displayed code:0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00


Then click“Write”, open serial monitor to view if there is a “0” signal
The code that the multiple patterns shift:
<br>[[File:0428-106.png|300px|frameless|thumb]]<br>
 
 
(7)Code Explanation
 
Serial.available() : The current rest characters when return to buffer area. Generally, this function is used to judge if there is data in buffer. When Serial.available()>0, it means that serial receives the data and can be read
Serial.read():Read a data of a Byte in buffer of serial port, for instance, device sends data to Arduino via serial port, then we could read data by “Serial.read()”
(8)Extension Practice
We could send a command via cellphone to turn on and off a LED.
D10 is connected to a LED, as shown below:
 
<br>[[File:0428-107.png|500px|frameless|thumb]]<br>


<pre>
<pre>
/*
/* keyestudio Mini Tank Robot v2.0
keyestudio Mini Tank Robot v2.0
  lesson 9.2
  lesson 7.2
  Matrix loop
  Bluetooth
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/  
int ledpin=11;
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
void setup()
unsigned char start01[] =
{Serial.begin(9600);
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
pinMode(ledpin,OUTPUT);
unsigned char front[] =
{0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] =
{0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] =
{0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] =  
{0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] =
{0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] =
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
void setup(){
  //Set pins to output
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  //Clear the display
  matrix_display(clear);
}
}
void loop()
void loop(){
{ int i;
  matrix_display(start01);  // Display start pattern
   if (Serial.available())
  delay(2000);
   {i=Serial.read();
  matrix_display(front);    //Front pattern
    Serial.println("DATA RECEIVED:");
  delay(2000);
    if(i=='1')
  matrix_display(STOP01);  //Stop pattern
    { digitalWrite(ledpin,1);
  delay(2000);
       Serial.println("led on");
  matrix_display(clear);    //Clear the display Clear the screen
    }
  delay(2000);
    if(i=='0')
}
    { digitalWrite(ledpin,0);
// This function is used to display of dot matrix
       Serial.println("led off");
void matrix_display(unsigned char matrix_value[])
}}}//*******************************************
{
</pre>
  IIC_start();  //call the function that data transmission start 
 
  IIC_send(0xc0);  //Choose address
<br>[[File:0428-108.png|300px|frameless|thumb]] [[File:0428-105.png|300px|frameless|thumb]]<br>
 
 
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
Click “Write” on APP, when you enter 1, LED will be on, when you input 0, LED will be off. (Remember to remove the Bluetooth module after finishing experiment, otherwise, burning code will be affected)
  {
 
    IIC_send(matrix_value[i]); //data to convey patterns
== Project 8: Motor Driving and Speed Control ==
  }
 
  IIC_end();  //end the transmission of pattern dataEnd
(1) Description
  IIC_start();
 
  IIC_send(0x8A);  //display control, set pulse width to 4/16
<br>[[File:0428-110.png|300px|frameless|thumb]]<br>
  IIC_end();
 
}
There are many ways to drive the motor. Our tank robot uses the most commonly used L298P solution. L298P is an excellent high-power motor driver IC produced by STMicroelectronics. It can directly drive DC motors, two-phase and four-phase stepping motors. The driving current up to 2A, and output terminal of motor adopts eight high-speed Schottky diodes as protection.
//The condition starting to transmit data
We designed a shield based on the circuit of L298p.
void IIC_start()
The stacked design reduces the technical difficulty of using and driving the motor.
{
 
  digitalWrite(SCL_Pin,HIGH);
(2)Specification
   delayMicroseconds(3);
 
  digitalWrite(SDA_Pin,HIGH);
Circuit Diagram for L298P Board
  delayMicroseconds(3);
 
   digitalWrite(SDA_Pin,LOW);
<br>[[File:0428-111.png|500px|frameless|thumb]]<br>
  delayMicroseconds(3);
 
}
* 1)Logic part input voltage: DC5V
//Convey data
* 2)Driving part input voltage: DC 7-12V
void IIC_send(unsigned char send_data)
* 3)Logic part working current: <36mA
{
* 4)Driving part working current: <2A
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
* 5)Maximum power dissipation: 25W (T=75℃)
  {
* 6)Working temperature: -25℃~+130℃
      digitalWrite(SCL_Pin,LOW); //pull down clock pin SCL Pin to change the signals of SDA     
* 7)Control signal input level: high level 2.3V<Vin<5V, low level -0.3V<Vin<1.5V
delayMicroseconds(3);
 
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
<br>[[File:0428-112.png|500px|frameless|thumb]]<br>  
      {
        digitalWrite(SDA_Pin,HIGH);
       }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
       delayMicroseconds(3);
      send_data = send_data >> 1;  //detect bit by bit, so shift the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);}  
//*****************************************************
</pre>


(3)Drive Robot to Move
Upload code on development board, 8*16 dot matrix display go front and back and stop patterns, alternately.


Through the above circuit diagram, the direction pin of A motor is D12, and speed pin is D3; D13 is the direction pin of B motor, D11 is speed pin.
<br>[[File:0428-117.png|400px|frameless|thumb]][[File:0428-118.png|400px|frameless|thumb]][[File:0428-119.png|400px|frameless|thumb]]<br>
We know how to control digital ports according to the following chart.
PWM decides 2 motors to turn so as to drive robot car. The PWM value is in the range of 0-255, the larger the number, the faster the motor rotates.


<br>[[File:0428-113.png|500px|frameless|thumb]]<br>
=== Project 10: Light Follow Robot ===


(4)Equipment
(1)Description


<br>[[File:0428-114.png|700px|frameless|thumb]]<br>  
[[File:0428-137.png|500px|frameless|thumb]]<br>


(5)Connection Diagram
We’ve introduce how to use various sensors, modules. <br>
In this lesson, we combine with hardware knowledge -- photoresistor module, motor driving, to build a light-following robot!<br>
Just need to use 2 photoresistor modules to detect the light intensity at the both side of robot. Read the analog value to rotate the 2 motors, thus drive the tank robot run. <br>


<br>[[File:0428-115.png|500px|frameless|thumb]]<br>


Note: the 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4.
The specific logic of light following robot is shown as the table below:<br>


(6)Test Code
[[File:0428-133.png|500px|frameless|thumb]]<br>
 
[[File:0428-134.png|500px|frameless|thumb]]<br>
 
We make a flow chart based on the above logic table, as shown below:
 
(2)Connection Diagram
 
[[File:04288-24.png|500px|frameless|thumb]]<br>
 
Hook-up Attention:
The 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4. <br>
 
 
[[File:0428-136.png|500px|frameless|thumb]]<br>
 
 
(3)Test Code


<pre>
<pre>
 
/*
/*
  keyestudio Mini Tank Robot v2.0
  keyestudio Mini Tank Robot v2.0
  lesson 8.1
  lesson 10
  motor driver
  Light-following tank
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/  
 
#define light_L_Pin A1  //define the pin of left photo resistor
#define light_R_Pin A2  //define the pin of right photo resistor
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11  //define the PWM control pin of left motor
#define ML_PWM 11  //define the PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3  // define the PWM control pin of right motor
#define MR_PWM 3  //define the PWM control pin of right motor
 
int left_light;
void setup()
int right_light;
{
void setup(){
   pinMode(ML_Ctrl, OUTPUT);//define direction control pin of left motor as output
  Serial.begin(9600);
   pinMode(ML_PWM, OUTPUT);//define PWM control pin of left motor as output
  pinMode(light_L_Pin, INPUT);
   pinMode(MR_Ctrl, OUTPUT);//define direction control pin of right motor as output.
  pinMode(light_R_Pin, INPUT);
   pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as output
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
   pinMode(MR_Ctrl, OUTPUT);
   pinMode(MR_PWM, OUTPUT);
}
}
void loop(){
  left_light = analogRead(light_L_Pin);
  right_light = analogRead(light_R_Pin);
  Serial.print("left_light_value = ");
  Serial.println(left_light);
  Serial.print("right_light_value = ");
  Serial.println(right_light);
  if (left_light > 650 && right_light > 650) //the value detected photo resistor,go front
  { 
    Car_front();
  }
  else if (left_light > 650 && right_light <= 650)  //the value detected photo resistor,turn left
  {
    Car_left();
  }
  else if (left_light <= 650 && right_light > 650) //the value detected photo resistor,turn right
  {
    Car_right();
  }
  else  //other situations, stop
  {
    Car_Stop();
  }
}
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
//****************************************************************
</pre>


void loop()
(4)Test Result
{
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
 
  //front
  delay(2000);//delay in 2s
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200 
digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
 
  //back
  delay(2000);//delay in 2s
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
 
    //left
  delay(2000);//delay in 2s
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200
 
  //right
  delay(2000);//delay in 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);//set the PWM control speed of right motor to 0
 
    //stop
  delay(2000);//delay in 2s
}//*****************************************


</pre>
Upload code on keyestudio V4.0 development board, DIP switch is dialed to right end and power on, the smart robot follows light to move.


=== Project 11: Ultrasonic Avoid Tank ===
 


(7)Test Result
[[File:0428-138.png|500px|frameless|thumb]]<br>


Hook up by connection diagram, upload code and power on, smart car goes forward and back for 2s, turns left and right for 2s, stops for 2s and alternately.
(1)Description
(8) Code Explanation
digitalWrite(ML_Ctrl,LOW): the rotation direction of motor is decided by the high/low level and and the pins that decide rotation direction are general pins.
analogWrite(ML_PWM,200): the speed of motor is regulated by PWM, The speed of motor is regulated by PWM, and the pins that decide the speed of motor have to be PWM pins.


In this program, the ultrasonic sensor detects the distance of obstacle to send signals that control the robot car. Next, let’s show you how to make an obstacle avoidance car. <br>


(9)Extension Practice
The specific logic of ultrasonic avoiding robot is as shown below:
 
[[File:0428-137.png|500px|frameless|thumb]]<br>
 
(2)Flow chart
 
[[File:0428-139.png|500px|frameless|thumb]]<br>
[[File:0428-140.png|500px|frameless|thumb]]<br>
 
(3)Connection Diagram:


Hook up in same way
[[File:04288-25.png|500px|frameless|thumb]]<br>
 
Note: “-”、“+” and “S” pins of servo are respectively attached to G(GND), V(VCC)and D9 of expansion board. The VCC, Trig, Echo and Gnd of ultrasonic sensor are linked with 5v(V), 5(S), Echo and Gnd(G) of expansion board.


<br>[[File:0428-115.png|500px|frameless|thumb]]<br>
(4)Test Code:


<pre>
<pre>
/*
/*
  keyestudio Mini Tank Robot v2.0
  keyestudio Mini Tank Robot v2.0
  lesson 8.2
  lesson 11
  motor driver pwm
  ultrasonic_avoid_tank
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/
int random2;
int a;
int a1;
int a2;
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11  //define the PWM control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define the control pin of right motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3  //define the PWM control pin of right motor
#define MR_PWM 3  //define PWM control pin of right motor
void setup()
{ pinMode(ML_Ctrl, OUTPUT);//define the direction control pin of left motor as OUTPUT
  pinMode(ML_PWM, OUTPUT);//define the PWM control pin of left motor as OUTPUT
  pinMode(MR_Ctrl, OUTPUT);//define the direction control pin of right motor as OUTPUT
  pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as OUTPUT


#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance;
#define servoPin 9  //servo Pin
int pulsewidth;
/************the function to run motor**************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
}
void loop()
{ digitalWrite(ML_Ctrl,LOW);//Set direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);// Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
  //front
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,HIGH);//Set direction control pin of right motor to HIGH level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
  //back
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set direction control pin of right motor to LOW level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
    //left
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,100);//set the PWM control speed of right motor to 100
  //right
  delay(2000);//define 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);// set the PWM control speed of right motor to 0


     //stop
//The function to control servo
   delay(2000);//define 2s
void procedure(int myangle) {
}//******************************************************************
  for (int i = 0; i <= 50; i = i + (1)) {
 
     pulsewidth = myangle * 11 + 500;
</pre>
    digitalWrite(servoPin,HIGH);
 
    delayMicroseconds(pulsewidth);
Upload code successfully, the servo turns slowly.
    digitalWrite(servoPin,LOW);
 
    delay((20 - pulsewidth / 1000));
== Project 9: LED Expression Panel ==
  }
 
}
(1)Description
//The function to control ultrasonic sensor
 
float checkdistance() {
<br>[[File:0428-120.png|700px|frameless|thumb]]<br>
  digitalWrite(Trig, LOW);
 
  delayMicroseconds(2);
If we add an expression panel to the robot, it will be amazing. Keyestudio's 8*16 dot matrix can meet your requirements. You can create facial expressions, patterns or other interesting displays yourself. 8*16 LED light board comes with 128 LEDs. The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface, so as to control the 128 LEDs on the module, which produce the patterns you need on dot matrix. To facilitate wiring, we also provide a HX-2.54 4Pin wiring.
  digitalWrite(Trig, HIGH);
 
  delayMicroseconds(10);
 
  digitalWrite(Trig, LOW);
(2)Specification
   float distance = pulseIn(Echo, HIGH) / 58.00; //58.20, that is, 2*29.1=58.2
 
  delay(10);
* Working voltage: DC 3.3-5V
  return distance;
* Power loss: 400mW
}
* Oscillation frequency: 450KHz        
  //****************************************************************
* Drive current: 200mA
void setup(){
* Working temperature: -40~80℃
  pinMode(servoPin, OUTPUT);
* Communication method: two-wire bus
  procedure(90); //set servo to 90°
 
 
(3)Equipment
  pinMode(Trig, OUTPUT);
 
  pinMode(Echo, INPUT);
<br>[[File:0428-121.png|700px|frameless|thumb]]<br>
  pinMode(ML_Ctrl, OUTPUT);
 
  pinMode(ML_PWM, OUTPUT);
(4)8*16 Dot Matrix Display
  pinMode(MR_Ctrl, OUTPUT);
 
  pinMode(MR_PWM, OUTPUT);
Circuit Graphic
}
 
void loop(){
<br>[[File:0428-122.png|700px|frameless|thumb]]<br>
  random2 = random(1, 100);
 
  a = checkdistance();  //assign the front distance detected by ultrasonic sensor to variable a
The principle of 8*16 dot matrix:
 
How to control each led light of 8*16 dot matrix? We know that a byte has 8 bits, each bit is 0 or 1. When a bit is 0, turn off LED and when a bit is 0, turn on LED. Thereby, one byte can control the LED in a row of dot matrix, so 16 bytes can control 16 columns of led lights, that is, 8*16 dot matrix.
  if (a < 20) //when the front distance detected is less than 20
 
  {
Interface Description and Communication Protocol:
      Car_Stop();  //robot stops
The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface.
      delay(500); //delay in 500ms
The communication protocol diagram is shown below:
       procedure(160);  //Ultrasonic platform turns left
(SCLK) is SCL, (DIN) is SDA:
      for (int j = 1; j <= 10; j = j + (1)) { //for statement, the data will be more accurate if ultrasonic sensor detect a few times.
 
        a1 = checkdistance();  //assign the left distance detected by ultrasonic sensor to variable a1
<br>[[File:0428-123.png|700px|frameless|thumb]]<br>
      }
 
      delay(300);
①The starting condition for data input: SCL is high level and SDA changes from high to low.
      procedure(20); //Ultrasonic platform turns right
②For data command setting, there are methods as shown in the figure below
      for (int k = 1; k <= 10; k = k + (1)) {
In our sample program, select the way to add 1 to the address automatically, the binary value is 0100 0000 and the corresponding hexadecimal value is 0x40
        a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
 
      }
<br>[[File:0428-124.png|700px|frameless|thumb]]<br>
     
 
      if (a1 < 50 || a2 < 50)  //robot will turn to the longer distance side when left or right distance is less than 50cm.  
③For address command setting, the address can be selected as shown below.
      {
The first 00H is selected in our sample program, and the binary number 1100 0000 corresponds to the hexadecimal 0xc0
        if (a1 > a2) //left distance is greater than right side     
 
        {
<br>[[File:0428-125.png|700px|frameless|thumb]]<br>
          procedure(90);  //Ultrasonic platform turns back to right ahead       
 
Car_left();  //robot turns left
④The requirement for data input is that SCL is high level when inputting data, the signal on SDA must remain unchanged. Only when the clock signal on SCL is low level, the signal on SDA can be altered. The data input is low-order first, high-order is behind
          delay(500);  //turn left for 500ms
⑤ The condition to end data transmission is that when SCL is low, SDA is low, and when SCL is high, the SDA level also becomes high.
          Car_front(); //go front
⑥ Display control, set different pulse width, the pulse width can be selected as shown below
        }
In the example, we choose pulse width 4/16, and the hexadecimal corresponds to 1000 1010 is 0x8A
        else
 
        {
 
          procedure(90);
<br>[[File:0428-126.png|700px|frameless|thumb]]<br>
          Car_right(); //robot turns right
 
          delay(500);
4. Introduction for Modulus Tool
          Car_front();  //go front
 
        }
The online version of dot matrix modulus tool:
      }
http://dotmatrixtool.com/#
      else  //If both side is greater than or equal to 50cm, turn left or right randomly
      {
        if ((long) (random2) % (long) (2) == 0)  //When the random number is even
        {
          procedure(90);
          Car_left(); //tank robot turns left
          delay(500);
          Car_front(); //go front
        }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); //go front
      }
    }
  }
  else  //If the front distance is greater than or equal to 20cm, robot car will go front
  {
      Car_front(); //go front
  }
}
  //****************************************************************


①Open links to enter the following page.
</pre>
 
 
(5)Test Result
 
Upload code successfully, DIP switch is dialed to right end and power on, tank robot goes forward. It can automatically avoid barrier.
 
=== Project 12: Ultrasonic Follow Tank ===


<br>[[File:0428-127.png|700px|frameless|thumb]]<br>


②The dot matrix is 8*16 in this project, so set the height to 8, width to 16, as shown below.
(1)Description


<br>[[File:0428-128.png|700px|frameless|thumb]]<br>  
[[File:0428-143.png|500px|frameless|thumb]]<br>


③ Generate hexadecimal data from the pattern
In project 11, we made an obstacle avoidance car. In fact, we only need to alter a test code to transform an obstacle avoidance car into following car. In this lesson, we will make an ultrasonic follow robot. The ultrasonic sensor detects the distance between smart car and the obstacle to drive tank car to move.
As shown below, press the left mouse button to select, the right button to cancel, draw the pattern you want, click Generate, and the hexadecimal data we need will be produced.


<br>[[File:0428-129.png|700px|frameless|thumb]]<br>
The specific logic of ultrasonic follow robot is as shown below:


[[File:0428-144.png|500px|frameless|thumb]]<br>


(5)Connection Diagram
(2)Flow chart


<br>[[File:0428-130.png|700px|frameless|thumb]]<br>  
[[File:0428-146.png|500px|frameless|thumb]]<br>


(3)Connection Diagram


Wiring note: The GND, VCC, SDA, and SCL of the 8x16 LED panel are respectively connected to -(GND), + (VCC), A4 and A5 of the keyestudio sensor expansion board for two-wire serial communication. (Note: This pin is connected to arduino IIC, but this module is not IIC communication, it can be linked with any two pins.)
[[File:04288-26.png|500px|frameless|thumb]]<br>


Wire-up note:


(6)Test Code
[[File:0428-145.png|500px|frameless|thumb]]<br>


The code that shows smile face
(4)Test Code


<pre>
<pre>
/*
/*
  keyestudio Mini Tank Robot v2.0
  keyestudio Mini Tank Robot v2.0
  lesson 9.1
  lesson 12
  Matrix  face
  ultrasonic follow tank
  http://www.keyestudio.com
  http://www.keyestudio.com
*/  
*/  
//the data of smiley from modulus tool
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char smile[] = {0x00, 0x00, 0x1c, 0x02, 0x02, 0x02, 0x5c, 0x40, 0x40, 0x5c, 0x02, 0x02, 0x02, 0x1c, 0x00, 0x00};
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
 
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
#define SDA_Pin  A4  //Set data pin to A4


#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3  //define PWM control pin of right motor
#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance;
int pulsewidth;
#define servoPin 9  //servo Pin
void setup(){
void setup(){
   //Set pin to output
   Serial.begin(9600);
   pinMode(SCL_Pin,OUTPUT);
   pinMode(SCL_Pin,OUTPUT);
   pinMode(SDA_Pin,OUTPUT);
   pinMode(SDA_Pin,OUTPUT);
   //clear the display
   matrix_display(clear); //Clear the display
   //matrix_display(clear);
   matrix_display(start01);  //display start pattern
  pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}
}
void loop(){
void loop(){
   matrix_display(smile);  // display smile face
   distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
}
  if (distance >= 20 && distance <= 60) //range to go front
// the function for dot matrix display
  {
void matrix_display(unsigned char matrix_value[])
    Car_front();
{
  }
   IIC_start(); // use the function of the data transmission start condition
   else if (distance > 10 && distance < 20)  //range to stop
   IIC_send(0xc0);  //select address
   {
    
    Car_Stop();
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  }
  else if (distance <= 10) //range to go back
   {
    Car_back();
  }
  else  //other situations, stop
   {
   {
    IIC_send(matrix_value[i]); //convey the pattern data
    Car_Stop();
   }
   }
  IIC_end();  //end the transmission of pattern data 
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16 s
  IIC_end();
}
}
 
/***********the function for motor running****************/
//the condition to start conveying data
void Car_front()
void IIC_start()
{
{
   digitalWrite(SCL_Pin,HIGH);
   digitalWrite(MR_Ctrl,LOW);
   delayMicroseconds(3);
   analogWrite(MR_PWM,200);
  digitalWrite(SDA_Pin,HIGH);
   digitalWrite(ML_Ctrl,LOW);
  delayMicroseconds(3);
   analogWrite(ML_PWM,200);
   digitalWrite(SDA_Pin,LOW);
   delayMicroseconds(3);
}
}
//Convey data
void Car_back()
void IIC_send(unsigned char send_data)
{
{
   for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bit for every character
   digitalWrite(MR_Ctrl,HIGH);
  {
  analogWrite(MR_PWM,200);
      digitalWrite(SCL_Pin,LOW);  // pull down clock pin SCL_Pin to change the signal of SDA
  digitalWrite(ML_Ctrl,HIGH);
      delayMicroseconds(3);
  analogWrite(ML_PWM,200);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up the clock pin SCL_Pin to stop transmission
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, shift the data to the right by one
  }
}
}
 
void Car_left()
//The sign of ending data transmission
void IIC_end()
{
{
   digitalWrite(SCL_Pin,LOW);
   digitalWrite(MR_Ctrl,LOW);
   delayMicroseconds(3);
   analogWrite(MR_PWM,200);
   digitalWrite(SDA_Pin,LOW);
  digitalWrite(ML_Ctrl,HIGH);
   delayMicroseconds(3);
  analogWrite(ML_PWM,200);
   digitalWrite(SCL_Pin,HIGH);
}
   delayMicroseconds(3);
void Car_right()
   digitalWrite(SDA_Pin,HIGH);
{
   delayMicroseconds(3);
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
   digitalWrite(ML_Ctrl,LOW);
   analogWrite(ML_PWM,200);
}
void Car_Stop()
{
   digitalWrite(MR_Ctrl,LOW);
   analogWrite(MR_PWM,0);
   digitalWrite(ML_Ctrl,LOW);
   analogWrite(ML_PWM,0);
}
}
//******************************************************
</pre>
(7)Test Result
Wire according to connection diagram. The DIP switch is dialed to right end and power on, the smile face appears on dot matrix.
<br>[[File:0428-131.png|500px|frameless|thumb]]<br>


/******************dot matrix********************/
// the function for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start(); // call the function that data transmission start
  IIC_send(0xc0);  //Choose address
 
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
    IIC_send(matrix_value[i]); //data to convey patterns
  }


(8)Extension Practice
  IIC_end();  //end to convey data pattern
 
  IIC_start();
  IIC_send(0x8A);  //select pulse width4/16, control display
  IIC_end();
}


We use the modulo tool (http://dotmatrixtool.com/#)to make the dot matrix alternately display start, go front and stop patterns then clear the patterns, the time interval is 2000 milliseconds.
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}


<br>[[File:0428-132.png|500px|frameless|thumb]]<br>
// transmit data
 
void IIC_send(unsigned char send_data)
Get the graphical code to be displayed via modulus tool
{
 
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
Start:
  {
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
      digitalWrite(SCL_Pin,LOW); //pull down clock pin SCL Pin to change the signals of SDA     
Go front:
delayMicroseconds(3);
0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
Go back:
      {
0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00
        digitalWrite(SDA_Pin,HIGH);
Turn left:
      }
0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00
      else
Turn left:
      {
0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00
        digitalWrite(SDA_Pin,LOW);
Stop:
      }
0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00
      delayMicroseconds(3);
Clear the displayed code:0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
 
      delayMicroseconds(3);
The code that the multiple patterns shift:
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
 
  }
<pre>
}
/* keyestudio Mini Tank Robot v2.0
//The sign that data transmission ends
lesson 9.2
void IIC_end()
  Matrix loop
{
http://www.keyestudio.com
   digitalWrite(SCL_Pin,LOW);
*/
  delayMicroseconds(3);
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
  digitalWrite(SDA_Pin,LOW);
unsigned char start01[] =
  delayMicroseconds(3);
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
   digitalWrite(SCL_Pin,HIGH);
unsigned char front[] =
   delayMicroseconds(3);
{0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
  digitalWrite(SDA_Pin,HIGH);
unsigned char back[] =
   delayMicroseconds(3);
{0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] =
{0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] =
{0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] =
{0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] =  
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5 //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
void setup(){
   //Set pins to output
   pinMode(SCL_Pin,OUTPUT);
   pinMode(SDA_Pin,OUTPUT);
   //Clear the display
  matrix_display(clear);
}
}
void loop(){
/***************end dot matrix display******************/
   matrix_display(start01); // Display start pattern
//The function to control servo
   delay(2000);
void procedure(int myangle) {
   matrix_display(front);   //Front pattern
   for (int i = 0; i <= 50; i = i + (1)) {
   delay(2000);
    pulsewidth = myangle * 11 + 500;
   matrix_display(STOP01);   //Stop pattern
    digitalWrite(servoPin,HIGH);
   delay(2000);
    delayMicroseconds(pulsewidth);
   matrix_display(clear);   //Clear the display Clear the screen
    digitalWrite(servoPin,LOW);
   delay(2000);
    delay((20 - pulsewidth / 1000));
  }}
//The function to control ultrasonic sensor function controlling ultrasonic
float checkdistance() {
   digitalWrite(Trig, LOW);
   delayMicroseconds(2);
   digitalWrite(Trig, HIGH);
   delayMicroseconds(10);
   digitalWrite(Trig, LOW);
   float distance = pulseIn(Echo, HIGH) / 58.20; //58.20, that is , 2*29.1=58.2
   delay(10);
  return distance;
}
}
// This function is used to display of dot matrix
//****************************************************************
void matrix_display(unsigned char matrix_value[])
 
{
</pre>
  IIC_start();  //call the function that data transmission start  IIC_send(0xc0);  //Choose address
 
 
(5)Test Result
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
 
  {
Upload code successfully, DIP switch is dialed to right end, the servo rotates to 90°, “V” is shown on 8X16 LED panel and smart car moves as the obstacle moves.
    IIC_send(matrix_value[i]); //data to convey patterns
 
  }
=== Project 13: IR Remote Robot Tank ===
  IIC_end();  //end the transmission of pattern dataEnd
 
  IIC_start();
[[File:0428-148.png|500px|frameless|thumb]]<br>
  IIC_send(0x8A);  //display control, set pulse width to 4/16
 
  IIC_end();
'''(1)Description'''
}
 
//The condition starting to transmit data
IR remote control is one of most ubiquitous control, applied in TV, electric fan and some household appliances. In this project, we will make an IR remote smart car. And we’ve known every key value on IR remote control. Thus, we could control smart car via and display the patterns on dot matrix via corresponding key value
void IIC_start()
 
{
The specific logic of infrared remote control robot is shown below:
  digitalWrite(SCL_Pin,HIGH);
 
  delayMicroseconds(3);
[[File:0428-149.png|500px|frameless|thumb]]<br>
  digitalWrite(SDA_Pin,HIGH);
[[File:0428-150.png|500px|frameless|thumb]]<br>
  delayMicroseconds(3);
 
  digitalWrite(SDA_Pin,LOW);
 
  delayMicroseconds(3);
'''(2)Flow Chart'''
}
 
//Convey data
[[File:0428-151.png|500px|frameless|thumb]]<br>
void IIC_send(unsigned char send_data)
 
{
 
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
 
  {
'''(3)Connection Diagram'''
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA     
 
delayMicroseconds(3);
[[File:04288-27.png|500px|frameless|thumb]]<br>
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
 
      {
Attention:GND,VCC, SDA, SCL of 8x16 LED panel are respectively linked with -(GND), +(VCC), SDA ,SCL. And “-”、“+” and S of IR receiver module are attached to G(GND), V(VCC) and A0 on sensor shield. On the condition of insufficient digital ports, the analog ports can be treat as digital ports. A0 equals to digital 14, A1 is like digital 15.
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  //detect bit by bit, so shift the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);}
//*****************************************************
</pre>
 
Upload code on development board, 8*16 dot matrix display go front and back and stop patterns, alternately.


<br>[[File:0428-117.png|400px|frameless|thumb]][[File:0428-118.png|400px|frameless|thumb]][[File:0428-119.png|400px|frameless|thumb]]<br>


== Project 10: Light Follow Robot ==
(4)Test Code


(1)Description
<pre>


[[File:0428-137.png|500px|frameless|thumb]]<br>
/*
keyestudio Mini Tank Robot v2.0
lesson 13
IR remote tank
http://www.keyestudio.com
*/


We’ve introduce how to use various sensors, modules.
#include <IRremoteTank.h>
In this lesson, we combine with hardware knowledge -- photoresistor module, motor driving, to build a light-following robot!
IRrecv irrecv(A0);  //set IRrecv irrecv to A0
Just need to use 2 photoresistor modules to detect the light intensity at the both side of robot. Read the analog value to rotate the 2 motors, thus drive the tank robot run.
decode_results results;
long ir_rec;  //save the IR value received


//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4


The specific logic of light following robot is shown as the table below:
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3    //define PWM control pin of right motor


[[File:0428-133.png|500px|frameless|thumb]]<br>
#define servoPin 9 //pin of servo
int pulsewidth; //save the pulse width value of servo


[[File:0428-134.png|500px|frameless|thumb]]<br>
void setup(){
 
  Serial.begin(9600);
We make a flow chart based on the above logic table, as shown below:
  irrecv.enableIRIn();  //Initialize the IR reception library
 
 
(2)Connection Diagram
  pinMode(ML_Ctrl, OUTPUT);
 
  pinMode(ML_PWM, OUTPUT);
[[File:0428-135.png|500px|frameless|thumb]]<br>
  pinMode(MR_Ctrl, OUTPUT);
 
  pinMode(MR_PWM, OUTPUT);
Hook-up Attention:
 
The 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4.
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear); //Clear Screen
  matrix_display(start01);  //show start picture
 
  pinMode(servoPin, OUTPUT);
  procedure(90);  //Servo rotates to 90°
}


 
void loop(){
[[File:0428-136.png|500px|frameless|thumb]]<br>
  if (irrecv.decode(&results)) //receive the IR remote value
 
  {
 
    ir_rec=results.value;
(3)Test Code
    String type="UNKNOWN";
 
    String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
<pre>
    if(results.decode_type>=1&&results.decode_type<=13){
      type=typelist[results.decode_type];
    }
    Serial.print("IR TYPE:"+type+"  ");
    Serial.println(ir_rec,HEX);
    irrecv.resume();
  }
    
    
/*
  if (ir_rec == 0xFF629D) //Go forward
keyestudio Mini Tank Robot v2.0
   {
lesson 10
    Car_front();
Light-following tank
    matrix_display(front); //Display front image
http://www.keyestudio.com
   }
*/
  if (ir_rec == 0xFFA857) //Robot car goes back
#define light_L_Pin A1   //define the pin of left photo resistor
   {
#define light_R_Pin A2  //define the pin of right photo resistor
    Car_back();
#define ML_Ctrl 13 //define the direction control pin of left motor
    matrix_display(front); //Go back
#define ML_PWM 11   //define the PWM control pin of left motor
   }
#define MR_Ctrl 12 //define the direction control pin of right motor
   if (ir_rec == 0xFF22DD)   //Robot car turns left
#define MR_PWM 3   //define the PWM control pin of right motor
   {
int left_light;
    Car_T_left();
int right_light;
    matrix_display(left); //Display left-turning image
void setup(){
   }
  Serial.begin(9600);
  if (ir_rec == 0xFFC23D//Robot car turns right
  pinMode(light_L_Pin, INPUT);
   {
   pinMode(light_R_Pin, INPUT);
    Car_T_right();
   pinMode(ML_Ctrl, OUTPUT);
    matrix_display(right); //Display right-turning image
   pinMode(ML_PWM, OUTPUT);
   }
  pinMode(MR_Ctrl, OUTPUT);
   if (ir_rec == 0xFF02FD)   //Robot car stops
   pinMode(MR_PWM, OUTPUT);
   {  
}
     Car_Stop();
void loop(){
    matrix_display(STOP01); //show stop image
  left_light = analogRead(light_L_Pin);
   }
   right_light = analogRead(light_R_Pin);
   if (ir_rec == 0xFF30CF)   //robot car rotates anticlockwise
   Serial.print("left_light_value = ");
  Serial.println(left_light);
  Serial.print("right_light_value = ");
   Serial.println(right_light);
   if (left_light > 650 && right_light > 650) //the value detected photo resistor,go front
   {
     Car_front();
   }  
   else if (left_light > 650 && right_light <= 650) //the value detected photo resistor,turn left
   {
   {
     Car_left();
     Car_left();
   }  
    matrix_display(left);  //show anticlockwise rotation picture
   else if (left_light <= 650 && right_light > 650) //the value detected photo resistor,turn right
   }
   if (ir_rec == 0xFF7A85) //robot car rotates clockwise
   {
   {
     Car_right();
     Car_right();
  }  
    matrix_display(right);  //show clockwise rotation picture
  else  //other situations, stop
}
   {
}
     Car_Stop();
/******************Control Servo*******************/
void procedure(int myangle) {
   for (int i = 0; i <= 50; i = i + (1)) {
     pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
   }
   }
}
}
void Car_front()
 
/******************Dot Matrix****************/
// this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
{
   digitalWrite(MR_Ctrl,LOW);
   IIC_start();
   analogWrite(MR_PWM,200);
   IIC_send(0xc0);  //Choose address
   digitalWrite(ML_Ctrl,LOW);
  for(int i = 0;i < 16;i++) //The picture has 16 bits
   analogWrite(ML_PWM,200);
  {
    IIC_send(matrix_value[i]); //data to convey patterns
  }
  IIC_end();   //end to convey data pattern
 
   IIC_start();
   IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
}
void Car_left()
 
//The condition starting to transmit data
void IIC_start()
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(SCL_Pin,HIGH);
   analogWrite(MR_PWM,200);
   delayMicroseconds(3);
   digitalWrite(ML_Ctrl,HIGH);
   digitalWrite(SDA_Pin,HIGH);
   analogWrite(ML_PWM,200);
   delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
}
void Car_right()
//传输数据
void IIC_send(unsigned char send_data)
{
{
   digitalWrite(MR_Ctrl,HIGH);
   for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bits for every character
   analogWrite(MR_PWM,200);
  {
   digitalWrite(ML_Ctrl,LOW);
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA     
   analogWrite(ML_PWM,200);
      delayMicroseconds(3);
}
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
void Car_Stop()
      {
{
        digitalWrite(SDA_Pin,HIGH);
   digitalWrite(MR_Ctrl,LOW);
      }
   analogWrite(MR_PWM,0);
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }
}
//The sign that data transmission ends
void IIC_end()
{
   digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
   digitalWrite(SDA_Pin,LOW);
   delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/***************the function to run motor***************/
void Car_front()
{
   digitalWrite(MR_Ctrl,LOW);
   analogWrite(MR_PWM,200);
   digitalWrite(ML_Ctrl,LOW);
   digitalWrite(ML_Ctrl,LOW);
   analogWrite(ML_PWM,0);
   analogWrite(ML_PWM,200);
}
}
//****************************************************************
void Car_back()
</pre>
{
 
  digitalWrite(MR_Ctrl,HIGH);
(4)Test Result
  analogWrite(MR_PWM,200);
 
  digitalWrite(ML_Ctrl,HIGH);
Upload code on keyestudio V4.0 development board, DIP switch is dialed to right end and power on, the smart robot follows light to move.
  analogWrite(ML_PWM,200);
 
}
== Project 11: Ultrasonic Avoid Tank ==
void Car_left()
 
{
 
  digitalWrite(MR_Ctrl,LOW);
[[File:0428-138.png|500px|frameless|thumb]]<br>
  analogWrite(MR_PWM,255);
 
  digitalWrite(ML_Ctrl,HIGH);
(1)Description
  analogWrite(ML_PWM,255);
In this program, the ultrasonic sensor detects the distance of obstacle to send signals that control the robot car. Next, let’s show you how to make an obstacle avoidance car.
}
 
void Car_right()
The specific logic of ultrasonic avoiding robot is as shown below:
{
 
  digitalWrite(MR_Ctrl,HIGH);
[[File:0428-137.png|500px|frameless|thumb]]<br>
  analogWrite(MR_PWM,255);
 
  digitalWrite(ML_Ctrl,LOW);
(2)Flow chart
  analogWrite(ML_PWM,255);
 
}
[[File:0428-139.png|500px|frameless|thumb]]<br>
void Car_Stop()
[[File:0428-140.png|500px|frameless|thumb]]<br>
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
//****************************************************************
 
</pre>
 
 
(5)Test Result


(3)Connection Diagram:


[[File:0428-141.png|500px|frameless|thumb]]<br>
Upload code successfully and power on, the smart robot can be controlled by IR remote, at same time, the corresponding pattern is shown on 8X16 LED panel.


Note: “-”、“+” and “S” pins of servo are respectively attached to G(GND), V(VCC)and D9 of expansion board. The VCC, Trig, Echo and Gnd of ultrasonic sensor are linked with 5v(V), 5(S), Echo and Gnd(G) of expansion board.
===Project 14: Bluetooth Control Robot ===


(4)Test Code:
[[File:0428-153.png|500px|frameless|thumb]]<br>


<pre>


/*
(1)Description
  keyestudio Mini Tank Robot v2.0
 
  lesson 11
We’ve learned the basic knowledge of Bluetooth, in this lesson, we will make a Bluetooth remote smart car. In the experiment, we default the HM-10 Bluetooth module as a Slave and the cellphone as a Host. <br>
  ultrasonic_avoid_tank
keyes BT car is an APP rolled out by keyestudio team. You could control the robot car by it readily.<br>
There is a guide to how to download and install APP in the document as for your reference. The interface is shown below.<br>
<br>
 
[[File:0428-154.png|500px|frameless|thumb]]<br>
 
 
'''APP DOWNLOAD'''
 
https://play.google.com/store/apps/details?id=com.keyestudio.tankcar3
 
 
(2)Test Code
 
<pre>
 
/*
  keyestudio Mini Tank Robot v2.0
  lesson 14.1
  bluetooth test
  http://www.keyestudio.com
  http://www.keyestudio.com
*/
*/
int random2;
char ble_val; //character variables, used to save the value of Bluetooth reception
int a;
void setup() {
int a1;
  Serial.begin(9600);
int a2;
}
#define ML_Ctrl 13 //define the direction control pin of left motor
void loop() {
#define ML_PWM 11   //define PWM control pin of left motor
  if(Serial.available() > 0) //judge if there is data in buffer area
#define MR_Ctrl 12 //define the direction control pin of right motor
   { ble_val = Serial.read();  //read the data from serial buffer
#define MR_PWM 3   //define PWM control pin of right motor
    Serial.println(ble_val); //print out
   }}
</pre>
 


#define Trig 5  //ultrasonic trig Pin
Pull off the Bluetooth module, upload test code, reconnect Bluetooth module, open serial monitor and set baud rate to 9600. Point at Bluetooth module and press keys on APP, the corresponding character is shown below.
#define Echo 4  //ultrasonic echo Pin
 
int distance;
 
#define servoPin 9  //servo Pin
[[File:Ks0446图片97.png|500px|frameless|thumb]]<br>
int pulsewidth;
 
/************the function to run motor**************/
 
void Car_front()
The detected character and corresponding function:
{
 
  digitalWrite(MR_Ctrl,LOW);
[[File:0428-156.png|800px|frameless|thumb]]<br>
  analogWrite(MR_PWM,200);
 
  digitalWrite(ML_Ctrl,LOW);
 
  analogWrite(ML_PWM,200);
In compliance with the above table, we make a flow chart to assist you to understand well. The flow chart is below:
}
 
void Car_back()
[[File:0428-157.png|700px|frameless|thumb]]<br>
{
 
  digitalWrite(MR_Ctrl,HIGH);
(3)Connection Diagram
  analogWrite(MR_PWM,200);
 
  digitalWrite(ML_Ctrl,HIGH);
[[File:04288-28.png|500px|frameless|thumb]]<br>
  analogWrite(ML_PWM,200);
 
}
 
void Car_left()
Wiring Attention:
{
 
  digitalWrite(MR_Ctrl,LOW);
[[File:0428-158.png|500px|frameless|thumb]]<br>
  analogWrite(MR_PWM,255);
 
  digitalWrite(ML_Ctrl,HIGH);
(4)Test Code
  analogWrite(ML_PWM,255);
 
}
<span style=color:red> <big>Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code</big></span>
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}


//The function to control servo
<pre>
void procedure(int myangle) {
/*
  for (int i = 0; i <= 50; i = i + (1)) {
keyestudio Robot Car v2.0
    pulsewidth = myangle * 11 + 500;
lesson 14.2
    digitalWrite(servoPin,HIGH);
bluetooth car
    delayMicroseconds(pulsewidth);
http://www.keyestudio.com
    digitalWrite(servoPin,LOW);
*/
    delay((20 - pulsewidth / 1000));
 
  }
//Array, used to store the data of pattern, can be calculated by yourself or obtained from the modulus tool
}
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//The function to control ultrasonic sensor
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
float checkdistance() {
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
  digitalWrite(Trig, LOW);
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
  delayMicroseconds(2);
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
  digitalWrite(Trig, HIGH);
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
  delayMicroseconds(10);
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  digitalWrite(Trig, LOW);
#define SCL_Pin  A5  //Set clock pin to A5
  float distance = pulseIn(Echo, HIGH) / 58.00; //58.20, that is, 2*29.1=58.2
#define SDA_Pin  A4  //Set data pin to A4
   delay(10);
 
  return distance;
#define ML_Ctrl 13 //define direction control pin of left motor
}
#define ML_PWM 11   //define PWM control pin of left motor
  //****************************************************************
#define MR_Ctrl 12  //define direction control pin of right motor
void setup(){
#define MR_PWM 3    //define PWM control pin of right motor
   pinMode(servoPin, OUTPUT);
 
  procedure(90); //set servo to 90°
char bluetooth_val; //save the value of Bluetooth reception
 
void setup(){
   Serial.begin(9600);
    
    
   pinMode(Trig, OUTPUT);
   pinMode(SCL_Pin,OUTPUT);
   pinMode(Echo, INPUT);
   pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear);    //Clear the display
  matrix_display(start01); //display start pattern
 
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
Line 2,089: Line 2,730:
   pinMode(MR_PWM, OUTPUT);
   pinMode(MR_PWM, OUTPUT);
}
}
void loop(){
void loop(){
   random2 = random(1, 100);
   if (Serial.available())
  a = checkdistance(); //assign the front distance detected by ultrasonic sensor to variable a
  {
    
    bluetooth_val = Serial.read();
   if (a < 20) //when the front distance detected is less than 20
    Serial.println(bluetooth_val);
   }
   switch (bluetooth_val)  
   {
   {
      Car_Stop(); //robot stops
    case 'F': //forward command
      delay(500); //delay in 500ms
        Car_front();
      procedure(160); //Ultrasonic platform turns left
         matrix_display(front);  // show forward design
      for (int j = 1; j <= 10; j = j + (1)) { //for statement, the data will be more accurate if ultrasonic sensor detect a few times.
         break;
         a1 = checkdistance();  //assign the left distance detected by ultrasonic sensor to variable a1
    case 'B': //Back command
      }
         Car_back();
      delay(300);
         matrix_display(back);  //show back pattern
      procedure(20); //Ultrasonic platform turns right
         break;
      for (int k = 1; k <= 10; k = k + (1)) {
    case 'L': // left-turning instruction
         a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
        Car_left();
      }
         matrix_display(left);  //show “left-turning” sign
     
         break;
      if (a1 < 50 || a2 < 50) //robot will turn to the longer distance side when left or right distance is less than 50cm.
    case 'R': //right-turning instruction
      {
         Car_right();
         if (a1 > a2) //left distance is greater than right side     
         matrix_display(right); //display right-turning sign
         {
      break;
          procedure(90);  //Ultrasonic platform turns back to right ahead          
    case 'S':  //stop command
Car_left(); //robot turns left
         Car_Stop();
          delay(500); //turn left for 500ms
        matrix_display(STOP01);  //show stop picture
          Car_front(); //go front
        break;
         }
   }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front();  //go front
         }
      }
      else //If both side is greater than or equal to 50cm, turn left or right randomly
      {
         if ((long) (random2) % (long) (2) == 0)  //When the random number is even
         {
          procedure(90);
          Car_left(); //tank robot turns left
          delay(500);
          Car_front(); //go front
         }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); //go front
      }
    }
  }
  else //If the front distance is greater than or equal to 20cm, robot car will go front
  {
      Car_front(); //go front
   }
}
}
  //****************************************************************


</pre>
/**************The function of dot matrix****************/
 
//this function is used for dot matrix display
 
void matrix_display(unsigned char matrix_value[])
(5)Test Result
{
 
  IIC_start();
Upload code successfully, DIP switch is dialed to right end and power on, tank robot goes forward. It can automatically avoid barrier.
  IIC_send(0xc0);  //Choose address
 
 
== Project 12: Ultrasonic Follow Tank ==
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
 
  {
 
    IIC_send(matrix_value[i]); //data to convey patterns
(1)Description
  }
 
  IIC_end();  //end to convey data pattern
[[File:0428-143.png|500px|frameless|thumb]]<br>
 
 
  IIC_start();
In project 11, we made an obstacle avoidance car. In fact, we only need to alter a test code to transform an obstacle avoidance car into following car. In this lesson, we will make an ultrasonic follow robot. The ultrasonic sensor detects the distance between smart car and the obstacle to drive tank car to move.
  IIC_send(0x8A);  //display control, set pulse width to 4/16
 
  IIC_end();
The specific logic of ultrasonic follow robot is as shown below:
}
 
//The condition starting to transmit data
[[File:0428-144.png|500px|frameless|thumb]]<br>
void IIC_start()
 
{
(2)Flow chart
  digitalWrite(SCL_Pin,HIGH);
 
  delayMicroseconds(3);
[[File:0428-146.png|500px|frameless|thumb]]<br>
  digitalWrite(SDA_Pin,HIGH);
 
  delayMicroseconds(3);
(3)Connection Diagram
  digitalWrite(SDA_Pin,LOW);
 
  delayMicroseconds(3);
[[File:0428-147.png|500px|frameless|thumb]]<br>
}
 
//transmit data
Wire-up note:
void IIC_send(unsigned char send_data)
 
{
[[File:0428-145.png|500px|frameless|thumb]]<br>
  for(char i = 0;i < 8;i++) //Each byte has 8 bits
 
  {
(4)Test Code
      digitalWrite(SCL_Pin,LOW); //pull down clock pin SCL Pin to change the signals of SDA     
 
      delayMicroseconds(3);
<pre>
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
  /*
      {
keyestudio Mini Tank Robot v2.0
        digitalWrite(SDA_Pin,HIGH);
lesson 12
      }
ultrasonic follow tank
      else
  http://www.keyestudio.com
      {
*/
        digitalWrite(SDA_Pin,LOW);
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
      }
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
      delayMicroseconds(3);
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
      delayMicroseconds(3);
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
      send_data = send_data >> 1; // Detect bit by bit, so move the data right by one
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
   }
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
}
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//The sign that data transmission ends
#define SCL_Pin  A5  //Set clock pin to A5
void IIC_end()
#define SDA_Pin  A4  //Set data pin to A4
{
 
   digitalWrite(SCL_Pin,LOW);
#define ML_Ctrl 13 //define the direction control pin of left motor
   delayMicroseconds(3);
#define ML_PWM 11  //define PWM control pin of left motor
   digitalWrite(SDA_Pin,LOW);
#define MR_Ctrl 12  //define the direction control pin of right motor
   delayMicroseconds(3);
#define MR_PWM 3   //define PWM control pin of right motor
   digitalWrite(SCL_Pin,HIGH);
#define Trig 5  //ultrasonic trig Pin
   delayMicroseconds(3);
#define Echo 4  //ultrasonic echo Pin
   digitalWrite(SDA_Pin,HIGH);
int distance;
   delayMicroseconds(3);
int pulsewidth;
}
#define servoPin 9  //servo Pin
/*************the function to run motor**************/
void setup(){
void Car_front()
   Serial.begin(9600);
{
   pinMode(SCL_Pin,OUTPUT);
   digitalWrite(MR_Ctrl,LOW);
   pinMode(SDA_Pin,OUTPUT);
   analogWrite(MR_PWM,200);
   matrix_display(clear); //Clear the display
   digitalWrite(ML_Ctrl,LOW);
   matrix_display(start01); //display start pattern
   analogWrite(ML_PWM,200);
   pinMode(servoPin, OUTPUT);
   procedure(90); //set servo to 90°
   pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
   pinMode(MR_Ctrl, OUTPUT);
   pinMode(MR_PWM, OUTPUT);
}
}
void loop(){
void Car_back()
  distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
{
  if (distance >= 20 && distance <= 60) //range to go front
  {
    Car_front();
  }
  else if (distance > 10 && distance < 20)  //range to stop
  {
    Car_Stop();
  }
  else if (distance <= 10)  //range to go back
  {
    Car_back();
  }
  else  //other situations, stop
  {
    Car_Stop();
  }
}
/***********the function for motor running****************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
   digitalWrite(MR_Ctrl,HIGH);
   digitalWrite(MR_Ctrl,HIGH);
   analogWrite(MR_PWM,200);
   analogWrite(MR_PWM,200);
Line 2,262: Line 2,840:
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(MR_Ctrl,LOW);
   analogWrite(MR_PWM,200);
   analogWrite(MR_PWM,255);
   digitalWrite(ML_Ctrl,HIGH);
   digitalWrite(ML_Ctrl,HIGH);
   analogWrite(ML_PWM,200);
   analogWrite(ML_PWM,255);
}
}
void Car_right()
void Car_right()
{
{
   digitalWrite(MR_Ctrl,HIGH);
   digitalWrite(MR_Ctrl,HIGH);
   analogWrite(MR_PWM,200);
   analogWrite(MR_PWM,255);
   digitalWrite(ML_Ctrl,LOW);
   digitalWrite(ML_Ctrl,LOW);
   analogWrite(ML_PWM,200);
   analogWrite(ML_PWM,255);
}
}
void Car_Stop()
void Car_Stop()
Line 2,280: Line 2,858:
   analogWrite(ML_PWM,0);
   analogWrite(ML_PWM,0);
}
}
 
void Car_T_left()
/******************dot matrix********************/
// the function for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
{
   IIC_start(); // call the function that data transmission start
   digitalWrite(MR_Ctrl,LOW);
   IIC_send(0xc0); //Choose address
   analogWrite(MR_PWM,255);
    
   digitalWrite(ML_Ctrl,LOW);
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
   analogWrite(ML_PWM,180);
  {
    IIC_send(matrix_value[i]); //data to convey patterns
  }
 
   IIC_end();  //end to convey data pattern
 
  IIC_start();
  IIC_send(0x8A);  //select pulse width4/16, control display
  IIC_end();
}
}
 
void Car_T_right()
//The condition starting to transmit data
void IIC_start()
{
{
   digitalWrite(SCL_Pin,HIGH);
   digitalWrite(MR_Ctrl,LOW);
   delayMicroseconds(3);
   analogWrite(MR_PWM,180);
  digitalWrite(SDA_Pin,HIGH);
   digitalWrite(ML_Ctrl,LOW);
  delayMicroseconds(3);
   analogWrite(ML_PWM,255);
   digitalWrite(SDA_Pin,LOW);
   delayMicroseconds(3);
}
}
  //****************************************************************


// transmit data
</pre>
void IIC_send(unsigned char send_data)
 
{
(5)Test Result
  for(char i = 0;i < 8;i++) //Each byte has 8 bits
 
  {
Upload code successfully, DIP switch is dialed to right end and power on. After connecting Bluetooth, we could drive smart car to move by Bluetooth App.
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA     
 
delayMicroseconds(3);
[[File:0428-160.png|500px|frameless|thumb]]<br>
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
 
      {
=== Project 15: Multi-purpose Robot Car ===
        digitalWrite(SDA_Pin,HIGH);
      }
 
      else
[[File:0428-162.png|500px|frameless|thumb]]<br>
      {
 
        digitalWrite(SDA_Pin,LOW);
 
      }
(1)Description
      delayMicroseconds(3);
 
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
In previous projects, the tank car only performs single function, however, in this lesson, we integrate all of function to control smart car via Bluetooth control.
      delayMicroseconds(3);
Here is a simple flow chart of multi-purpose robot car as for your reference.
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
 
  }
[[File:0428-163.png|500px|frameless|thumb]]<br>
}
 
//The sign that data transmission ends
(2)Connection Diagram
void IIC_end()
 
{
[[File:04288-29.png|800px|frameless|thumb]]<br>
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/***************end dot matrix display******************/
//The function to control servo
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }}
//The function to control ultrasonic sensor function controlling ultrasonic
float checkdistance() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.20;  //58.20, that is , 2*29.1=58.2
  delay(10);
  return distance;
}
//****************************************************************


</pre>
Attention:Confirm that every component is connected.


(5)Test Result
Wire-up Guide:


Upload code successfully, DIP switch is dialed to right end, the servo rotates to 90°, “V” is shown on 8X16 LED panel and smart car moves as the obstacle moves.
[[File:0428-165.png|500px|frameless|thumb]]<br>


== Project 13: IR Remote Robot Tank ==
[[File:0428-166.png|500px|frameless|thumb]]<br>


[[File:0428-148.png|500px|frameless|thumb]]<br>
[[File:0428-167.png|500px|frameless|thumb]]<br>


'''(1)Description'''
[[File:0428-173.png|500px|frameless|thumb]]<br>
 
IR remote control is one of most ubiquitous control, applied in TV, electric fan and some household appliances. In this project, we will make an IR remote smart car. And we’ve known every key value on IR remote control. Thus, we could control smart car via and display the patterns on dot matrix via corresponding key value


The specific logic of infrared remote control robot is shown below:


[[File:0428-149.png|500px|frameless|thumb]]<br>
[[File:0428-150.png|500px|frameless|thumb]]<br>


[[File:0428-174.png|500px|frameless|thumb]]<br>


'''(2)Flow Chart'''
[[File:0428-175.png|500px|frameless|thumb]]<br>


[[File:0428-151.png|500px|frameless|thumb]]<br>
[[File:0428-176.png|500px|frameless|thumb]]<br>






'''(3)Connection Diagram'''


[[File:0428-152.png|500px|frameless|thumb]]<br>


Attention:GND,VCC, SDA, SCL of 8x16 LED panel are respectively linked with -(GND), +(VCC), SDA ,SCL. And “-”、“+” and S of IR receiver module are attached to G(GND), V(VCC) and A0 on sensor shield. On the condition of insufficient digital ports, the analog ports can be treat as digital ports. A0 equals to digital 14, A1 is like digital 15.
(3)Test Code
 
 
(4)Test Code


<pre>
<pre>
 
    /*
/*
  keyestudio Mini Tank Robot v2.0
  keyestudio Mini Tank Robot v2.0
  lesson 13
  lesson 15
  IR remote tank
  multiple functions
  http://www.keyestudio.com
  http://www.keyestudio.com
*/
*/
#include <IRremote.h>
IRrecv irrecv(A0);  //set IRrecv irrecv to A0
decode_results results;
long ir_rec;  //save the IR value received


//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
Line 2,426: Line 2,944:
#define SDA_Pin  A4  //Set data pin to A4
#define SDA_Pin  A4  //Set data pin to A4


#define ML_Ctrl 13  // define the direction control pin of left motor
#define ML_Ctrl 13  //define direction control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3   //define PWM control pin of right motor
#define MR_PWM 3   //define PWM control pin of right motor
 
#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance; //save the distance value detected by ultrasonic, follow function
int random2; //save the variable of random numberssave the variable of random numbers
//save the distance value detected by ultrasonic, obstacle avoidance function
int a; 
int a1;
int a2;
 
#define servoPin 9  //servo Pin
int pulsewidth;


#define servoPin 9 //pin of servo
#define light_L_Pin A1  //define the pin of left photo resistor sensor
int pulsewidth; // save the pulse width value of servo
#define light_R_Pin A2  //define the pin of right photo resistor sensor
int left_light;
int right_light;


char bluetooth_val; //save the value of Bluetooth reception
int flag;  //flag variable, it is used to entry and exist function
void setup(){
void setup(){
   Serial.begin(9600);
   Serial.begin(9600);
   irrecv.enableIRIn(); //Initialize the IR reception library
   pinMode(Trig, OUTPUT);
    
   pinMode(Echo, INPUT);
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_Ctrl, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
   pinMode(ML_PWM, OUTPUT);
Line 2,443: Line 2,977:
   pinMode(MR_PWM, OUTPUT);
   pinMode(MR_PWM, OUTPUT);
    
    
   pinMode(SCL_Pin,OUTPUT);
   pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
pinMode(SCL_Pin,OUTPUT);
   pinMode(SDA_Pin,OUTPUT);
   pinMode(SDA_Pin,OUTPUT);
   matrix_display(clear); //Clear the display
   matrix_display(clear);   //Clear the display
   matrix_display(start01);  //show start pattern
   matrix_display(start01);  //display start pattern
 
   pinMode(light_L_Pin, INPUT);
   pinMode(servoPin, OUTPUT);
   pinMode(light_R_Pin, INPUT);
   procedure(90); //servo rotates to 90°
}
}


void loop(){
void loop(){
   if (irrecv.decode(&results)) //receive the IR remote value
   if (Serial.available())
   {
   {
     ir_rec=results.value;
     bluetooth_val = Serial.read();
    String type="UNKNOWN";
     Serial.println(bluetooth_val);
    String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
    if(results.decode_type>=1&&results.decode_type<=13){
      type=typelist[results.decode_type];
    }
    Serial.print("IR TYPE:"+type+"  ");
     Serial.println(ir_rec,HEX);
    irrecv.resume();
   }
   }
    
   switch (bluetooth_val)  
  if (ir_rec == 0xFF629D) //forward instruction
   {
   {
     Car_front();
     case 'F':  //Forward instruction
    matrix_display(front);  //display forward sign
      Car_front();
  }
      matrix_display(front);  //display forward pattern
  if (ir_rec == 0xFFA857) //back instruction
      break;
  {
    case 'B': //Back instruction
    Car_back();
      Car_back();
    matrix_display(front);  //show back pattern
      matrix_display(back);  // display back pattern
  }
      break;
  if (ir_rec == 0xFF22DD)  //left-turning instruction
    case 'L':  //left-turning instruction
  {
      Car_left();
    Car_T_left();
      matrix_display(left);  //show left-turning pattern
    matrix_display(left);  //display left-turning sign
      break;
  }
    case 'R':  //right-turning instruction
  if (ir_rec == 0xFFC23D)  //right-turning instruction
      Car_right();
  {
      matrix_display(right);  //show right-turning pattern
    Car_T_right();
      break;
    matrix_display(right);  //display right-turning pattern
    case 'S':  //stop instruction
  }
      Car_Stop();
  if (ir_rec == 0xFF02FD)   //stop instruction
      matrix_display(STOP01)//display stop pattern
  {
      break;
    Car_Stop();
  case 'Y':
    matrix_display(STOP01);  //show stop pattern
      matrix_display(start01);  //show start pattern
   }
      follow();
  if (ir_rec == 0xFF30CF)  //the command to turn left
      break;
  case 'U':
      matrix_display(start01);  //show start pattern
      avoid();
      break;
  case 'X':
      matrix_display(start01);  //show start pattern
      light_track();
      break;
   }}
/*****************Obstacle Avoidance Function**************/
void avoid()  
{
   flag = 0;  //the design that enter obstacle avoidance function
  while (flag == 0)
   {
   {
     Car_left();
     random2 = random(1, 100);
     matrix_display(left);  //display left-turning pattern
     a = checkdistance();  //assign the front distance detected by ultrasonic sensor to variable a
  }
   
  if (ir_rec == 0xFF7A85) //the command to turn right
    if (a < 20) //when the front distance detected is less than 20cm
 
    {
  {
      Car_Stop();  //robot stops
    Car_right();
      delay(200); //delay in 200ms
    matrix_display(right);  //display right-turning pattern
     
  }
      procedure(160);  //Ultrasonic platform turns left
}
      for (int j = 1; j <= 10; j = j + (1)) { ///for statement, the data will be more accurate if ultrasonic sensor detect a few times.
/******************The function to control servo***********/
        a1 = checkdistance(); //assign the left distance detected by ultrasonic sensor to variable a1
void procedure(int myangle) {
      }
  for (int i = 0; i <= 50; i = i + (1)) {
      delay(200);
    pulsewidth = myangle * 11 + 500;
      procedure(20); //Ultrasonic platform turns right
    digitalWrite(servoPin,HIGH);
      for (int k = 1; k <= 10; k = k + (1)) {
    delayMicroseconds(pulsewidth);
        a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
    digitalWrite(servoPin,LOW);
      }
    delay((20 - pulsewidth / 1000));
      if (a1 < 50 || a2 < 50)  //robot will turn to the longer distance side when left or right distance is less than 50cm.if the left or right distance is less than 50cm, the robot will turn to the greater distance
  }
      {
}
        if (a1 > a2) //left distance is greater than right
 
        {
/******************Function for dot matrix***************/
          procedure(90); //Ultrasonic platform turns back to right ahead ultrasonic platform turns front
// this function is used for dot matrix display
          Car_left();  //robot turns left
void matrix_display(unsigned char matrix_value[])
          delay(500); //turn left 500ms
{
          Car_front(); //go forward
  IIC_start();
        }
  IIC_send(0xc0); //Choose address
        else
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); //go forward
        }
      }  
      else  //both distance on two side is greater than or equal to 50cm, turn randomly
      {
        if ((long) (random2) % (long) (2) == 0)  //when the random number is even
        {
          procedure(90);
          Car_left(); //robot turns left
          delay(500);
          Car_front(); //go forward
        }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); ///go forward
      } } }
  else  //If the front distance is greater than or equal to 20cm, robot car will go front
   {
   {
    IIC_send(matrix_value[i]); //data to convey patterns
      Car_front(); //go forward
   }
   }
  IIC_end();   //end to convey data pattern
   // receive the Bluetooth value to end the obstacle avoidance function
    
   if (Serial.available())
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
 
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bits for every character
   {
   {
       digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA     
    bluetooth_val = Serial.read();
delayMicroseconds(3);
    if (bluetooth_val == 'S')  //receive S
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
    {
       {
       flag = 1;  //when assign 1 to flag, end loop
        digitalWrite(SDA_Pin,HIGH);
    }}}}
       }
/*******************Follow****************/
       else
void follow() {
  flag = 0;
  while (flag == 0) {
    distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
    if (distance >= 20 && distance <= 60) //the range to go front
    {
      Car_front();
    }
    else if (distance > 10 && distance < 20)  //the range to stop
    {
       Car_Stop();
    }
    else if (distance <= 10)  // the range to go back
    {
      Car_back();
    }
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
       bluetooth_val = Serial.read();
       if (bluetooth_val == 'S')
       {
       {
         digitalWrite(SDA_Pin,LOW);
         flag = 1;  //end loop
      }
      }}}}
      delayMicroseconds(3);
//The function to control ultrasonic sensor the function controlling ultrasonic sensor
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
float checkdistance() {
      delayMicroseconds(3);
  digitalWrite(Trig, LOW);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  delayMicroseconds(2);
   }
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.00;  //58.20 means 2*29.1=58.2
  delay(10);
   return distance;
}
}
//The sign that data transmission ends
//The function to control servo the function controlling servo
void IIC_end()
void procedure(int myangle) {
{
   for (int i = 0; i <= 50; i = i + (1)) {
   digitalWrite(SCL_Pin,LOW);
    pulsewidth = myangle * 11 + 500;
  delayMicroseconds(3);
    digitalWrite(servoPin,HIGH);
  digitalWrite(SDA_Pin,LOW);
    delayMicroseconds(pulsewidth);
  delayMicroseconds(3);
    digitalWrite(servoPin,LOW);
  digitalWrite(SCL_Pin,HIGH);
    delay((20 - pulsewidth / 1000));
  delayMicroseconds(3);
  }}
  digitalWrite(SDA_Pin,HIGH);
 
  delayMicroseconds(3);
/****************Light Follow******************/
}
void light_track() {
/***************the function to run motor***************/
  flag = 0;
void Car_front()
  while (flag == 0) {
{
    left_light = analogRead(light_L_Pin);
  digitalWrite(MR_Ctrl,LOW);
    right_light = analogRead(light_R_Pin);
  analogWrite(MR_PWM,200);
    if (left_light > 650 && right_light > 650) //the value detected by photo resistor, go forward
  digitalWrite(ML_Ctrl,LOW);
    { 
  analogWrite(ML_PWM,200);
      Car_front();
}
    }
void Car_back()
    else if (left_light > 650 && right_light <= 650)  //the value detected by photo resistor, turn left
    {
      Car_left();
    }
    else if (left_light <= 650 && right_light > 650) //the value detected by photo resistor, turn right
    {
      Car_right();
    }
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      if (bluetooth_val == 'S') {
        flag = 1;
    }}}}
/***************Dot Matrix *****************/
// this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
{
   digitalWrite(MR_Ctrl,HIGH);
   IIC_start();
   analogWrite(MR_PWM,200);
   IIC_send(0xc0);  //Choose address
   digitalWrite(ML_Ctrl,HIGH);
 
   analogWrite(ML_PWM,200);
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
    IIC_send(matrix_value[i]); //convey the pattern data
  }
  IIC_end();   //end the transmission of pattern data
   IIC_start();
   IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
}
void Car_left()
//The condition starting to transmit data
void IIC_start()
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(SCL_Pin,HIGH);
   analogWrite(MR_PWM,255);
   delayMicroseconds(3);
   digitalWrite(ML_Ctrl,HIGH);
   digitalWrite(SDA_Pin,HIGH);
   analogWrite(ML_PWM,255);
   delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
}
void Car_right()
//convey data
void IIC_send(unsigned char send_data)
{
{
   digitalWrite(MR_Ctrl,HIGH);
   for(char i = 0;i < 8;i++)  //each byte has 8 bits
  analogWrite(MR_PWM,255);
  {
  digitalWrite(ML_Ctrl,LOW);
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA
  analogWrite(ML_PWM,255);
      delayMicroseconds(3);
}
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
void Car_Stop()
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(SCL_Pin,LOW);
   analogWrite(MR_PWM,0);
   delayMicroseconds(3);
   digitalWrite(ML_Ctrl,LOW);
  digitalWrite(SDA_Pin,LOW);
   analogWrite(ML_PWM,0);
  delayMicroseconds(3);
   digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
   digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
}
void Car_T_left()
/*************the function to run motor*************/
void Car_front()
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(MR_Ctrl,LOW);
   analogWrite(MR_PWM,255);
   analogWrite(MR_PWM,200);
   digitalWrite(ML_Ctrl,LOW);
   digitalWrite(ML_Ctrl,LOW);
   analogWrite(ML_PWM,180);
   analogWrite(ML_PWM,200);
}
}
void Car_T_right()
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
{
   digitalWrite(MR_Ctrl,LOW);
   digitalWrite(MR_Ctrl,LOW);
Line 2,625: Line 3,282:
}
}


</pre>
(5)Test Result
Upload code successfully and power on, the smart robot can be controlled by IR remote, at same time, the corresponding pattern is shown on 8X16 LED panel.
==Project 14: Bluetooth Control Robot ==
[[File:0428-153.png|500px|frameless|thumb]]<br>
(1)Description
We’ve learned the basic knowledge of Bluetooth, in this lesson, we will make a Bluetooth remote smart car. In the experiment, we default the HM-10 Bluetooth module as a Slave and the cellphone as a Host.
keyes BT car is an APP rolled out by keyestudio team. You could control the robot car by it readily.
There is a guide to how to download and install APP in the document as for your reference. The interface is shown below.
[[File:0428-154.png|500px|frameless|thumb]]<br>
(2)Test Code
<pre>
/*
keyestudio Mini Tank Robot v2.0
lesson 14.1
bluetooth test
http://www.keyestudio.com
*/
char ble_val; //character variables, used to save the value of Bluetooth reception
void setup() {
  Serial.begin(9600);
}
void loop() {
  if(Serial.available() > 0)  //judge if there is data in buffer area
  { ble_val = Serial.read();  //read the data from serial buffer
    Serial.println(ble_val);  //print out
  }}
</pre>
Pull off the Bluetooth module, upload test code, reconnect Bluetooth module, open serial monitor and set baud rate to 9600. Point at Bluetooth module and press keys on APP, the corresponding character is shown below.
[[File:Ks0446图片97.png|500px|frameless|thumb]]<br>
The detected character and corresponding function:
[[File:0428-156.png|800px|frameless|thumb]]<br>
In compliance with the above table, we make a flow chart to assist you to understand well. The flow chart is below:
[[File:0428-157.png|700px|frameless|thumb]]<br>
(3)Connection Diagram
[[File:0428-161.png|500px|frameless|thumb]]<br>
Wiring Attention:
[[File:0428-158.png|500px|frameless|thumb]]<br>
(4)Test Code
Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code
<pre>
/*
keyestudio Robot Car v2.0
lesson 14.2
bluetooth car
http://www.keyestudio.com
*/
//Array, used to store the data of pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
#define ML_Ctrl 13  //define direction control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3    //define PWM control pin of right motor
char bluetooth_val; //save the value of Bluetooth reception
void setup(){
  Serial.begin(9600);
 
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear);    //Clear the display
  matrix_display(start01);  //display start pattern
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}
void loop(){
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    Serial.println(bluetooth_val);
  }
  switch (bluetooth_val)
  {
    case 'F':  //forward command
        Car_front();
        matrix_display(front);  // show forward design
        break;
    case 'B':  //Back command
        Car_back();
        matrix_display(back);  //show back pattern
        break;
    case 'L':  // left-turning instruction
        Car_left();
        matrix_display(left);  //show “left-turning” sign
        break;
    case 'R':  //right-turning instruction
        Car_right();
        matrix_display(right);  //display right-turning sign      break;
    case 'S':  //stop command
        Car_Stop();
        matrix_display(STOP01);  //show stop picture
        break;
  }
}
/**************The function of dot matrix****************/
//this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();
  IIC_send(0xc0);  //Choose address
 
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
    IIC_send(matrix_value[i]); //data to convey patterns
  }
  IIC_end();  //end to convey data pattern
 
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//transmit data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // Detect bit by bit, so move the data right by one
  }
}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/*************the function to run motor**************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
</pre>
(5)Test Result
Upload code successfully, DIP switch is dialed to right end and power on. After connecting Bluetooth, we could drive smart car to move by Bluetooth App.
[[File:0428-160.png|500px|frameless|thumb]]<br>
== Project 15: Multi-purpose Robot Car ==
[[File:0428-162.png|500px|frameless|thumb]]<br>
(1)Description
In previous projects, the tank car only performs single function, however, in this lesson, we integrate all of function to control smart car via Bluetooth control.
Here is a simple flow chart of multi-purpose robot car as for your reference.
[[File:0428-163.png|500px|frameless|thumb]]<br>
(2)Connection Diagram
[[File:0428-164.png|800px|frameless|thumb]]<br>
Attention:Confirm that every component is connected.
Wire-up Guide:
[[File:0428-165.png|500px|frameless|thumb]]<br>
[[File:0428-166.png|500px|frameless|thumb]]<br>
[[File:0428-167.png|500px|frameless|thumb]]<br>
[[File:0428-173.png|500px|frameless|thumb]]<br>
[[File:0428-174.png|500px|frameless|thumb]]<br>
[[File:0428-175.png|500px|frameless|thumb]]<br>
[[File:0428-176.png|500px|frameless|thumb]]<br>
(3)Test Code
<pre>
  /*
keyestudio Mini Tank Robot v2.0
lesson 15
multiple functions
http://www.keyestudio.com
*/
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
#define ML_Ctrl 13  //define direction control pin of left motor
#define ML_PWM 11  //define PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3  //define PWM control pin of right motor
#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance; //save the distance value detected by ultrasonic, follow function
int random2; //save the variable of random numberssave the variable of random numbers
//save the distance value detected by ultrasonic, obstacle avoidance function
int a; 
int a1;
int a2;
#define servoPin 9  //servo Pin
int pulsewidth;
#define light_L_Pin A1  //define the pin of left photo resistor sensor
#define light_R_Pin A2  //define the pin of right photo resistor sensor
int left_light;
int right_light;
char bluetooth_val; //save the value of Bluetooth reception
int flag;  //flag variable, it is used to entry and exist function
void setup(){
  Serial.begin(9600);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
 
  pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear);    //Clear the display
  matrix_display(start01);  //display start pattern
  pinMode(light_L_Pin, INPUT);
  pinMode(light_R_Pin, INPUT);
}
void loop(){
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    Serial.println(bluetooth_val);
  }
  switch (bluetooth_val)
  {
    case 'F':  //Forward instruction
      Car_front();
      matrix_display(front);  //display forward pattern
      break;
    case 'B':  //Back instruction
      Car_back();
      matrix_display(back);  // display back pattern
      break;
    case 'L':  //left-turning instruction
      Car_left();
      matrix_display(left);  //show left-turning pattern
      break;
    case 'R':  //right-turning instruction
      Car_right();
      matrix_display(right);  //show right-turning pattern
      break;
    case 'S':  //stop instruction
      Car_Stop();
      matrix_display(STOP01);  //display stop pattern
      break;
  case 'Y':
      matrix_display(start01);  //show start pattern
      follow();
      break;
  case 'U':
      matrix_display(start01);  //show start pattern
      avoid();
      break;
  case 'X':
      matrix_display(start01);  //show start pattern
      light_track();
      break;
  }}
/*****************Obstacle Avoidance Function**************/
void avoid()
{
  flag = 0;  //the design that enter obstacle avoidance function
  while (flag == 0)
  {
    random2 = random(1, 100);
    a = checkdistance();  //assign the front distance detected by ultrasonic sensor to variable a
   
    if (a < 20) //when the front distance detected is less than 20cm
    {
      Car_Stop();  //robot stops
      delay(200); //delay in 200ms
     
      procedure(160);  //Ultrasonic platform turns left
      for (int j = 1; j <= 10; j = j + (1)) { ///for statement, the data will be more accurate if ultrasonic sensor detect a few times.
        a1 = checkdistance();  //assign the left distance detected by ultrasonic sensor to variable a1
      }
      delay(200);
      procedure(20); //Ultrasonic platform turns right
      for (int k = 1; k <= 10; k = k + (1)) {
        a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
      }
      if (a1 < 50 || a2 < 50)  //robot will turn to the longer distance side when left or right distance is less than 50cm.if the left or right distance is less than 50cm, the robot will turn to the greater distance
      {
        if (a1 > a2) //left distance is greater than right
        {
          procedure(90);  //Ultrasonic platform turns back to right ahead ultrasonic platform turns front
          Car_left();  //robot turns left
          delay(500);  //turn left 500ms
          Car_front(); //go forward
        }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front();  //go forward
        }
      }
      else  //both distance on two side is greater than or equal to 50cm, turn randomly
      {
        if ((long) (random2) % (long) (2) == 0)  //when the random number is even
        {
          procedure(90);
          Car_left(); //robot turns left
          delay(500);
          Car_front(); //go forward
        }
        else
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); ///go forward
      } } }
  else  //If the front distance is greater than or equal to 20cm, robot car will go front
  {
      Car_front(); //go forward
  }
  // receive the Bluetooth value to end the obstacle avoidance function
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    if (bluetooth_val == 'S')  //receive S
    {
      flag = 1;  //when assign 1 to flag, end loop
    }}}}
/*******************Follow****************/
void follow() {
  flag = 0;
  while (flag == 0) {
    distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
    if (distance >= 20 && distance <= 60) //the range to go front
    {
      Car_front();
    }
    else if (distance > 10 && distance < 20)  //the range to stop
    {
      Car_Stop();
    }
    else if (distance <= 10)  // the range to go back
    {
      Car_back();
    }
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      if (bluetooth_val == 'S')
      {
        flag = 1;  //end loop
      }}}}
//The function to control ultrasonic sensor the function controlling ultrasonic sensor
float checkdistance() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.00;  //58.20 means 2*29.1=58.2
  delay(10);
  return distance;
}
//The function to control servo the function controlling servo
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }}
/****************Light Follow******************/
void light_track() {
  flag = 0;
  while (flag == 0) {
    left_light = analogRead(light_L_Pin);
    right_light = analogRead(light_R_Pin);
    if (left_light > 650 && right_light > 650) //the value detected by photo resistor, go forward
    { 
      Car_front();
    }
    else if (left_light > 650 && right_light <= 650)  //the value detected by photo resistor, turn left
    {
      Car_left();
    }
    else if (left_light <= 650 && right_light > 650) //the value detected by photo resistor, turn right
    {
      Car_right();
    }
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      if (bluetooth_val == 'S') {
        flag = 1;
    }}}}
/***************Dot Matrix *****************/
// this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();
  IIC_send(0xc0);  //Choose address
 
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
    IIC_send(matrix_value[i]); //convey the pattern data
  }
  IIC_end();  //end the transmission of pattern data
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/*************the function to run motor*************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}


</pre>
</pre>
Line 3,297: Line 3,292:


Alright, the whole projects are finished. Please feel free to contact us if you confront some problems.
Alright, the whole projects are finished. Please feel free to contact us if you confront some problems.
There are some hot-selling items below. You could log in our website to browse them if you’re interested.  
There are some hot-selling items below. You could log in our website to browse them if you’re interested.
 


== 9. Resources ==   
== 9. Resources ==   
                                            
                                            
Wiki page: https://wiki.keyestudio.com/Main_Page<br>
Wiki page:https://wiki.keyestudio.com/Ks0428_keyestudio_Mini_Tank_Robot_V2


Official website: https://keyestudio.com/<br>
Official website: https://keyestudio.com/<br>
Assembly Video Link: http://video.keyestudio.com/ks0428/<br>
Assembly Video Link: http://video.keyestudio.com/ks0428/<br>


If you want to try with Mixly software, enter https://fs.keyestudio.com/KS0428 please to try.<br>
If you want to try with Mixly software, enter https://fs.keyestudio.com/KS0428.<br>

Latest revision as of 11:17, 17 June 2024

Thanks for your support on our products, we will continue to provide you better quality and service!

*About keyestudio

Keyestudio is a best-selling brand owned by KEYES Corporation, our product lines range from controller boards, shields and sensor modules to smart car and complete starter kits for Arduino, Raspberry Pi and BBC micro:bit, which designed for customers of any level to learn electronics and programming knowledge. All of our products comply with international quality standards and are greatly appreciated in a variety of different markets throughout the world.
You can obtain the details and the latest information through visiting the following web sites:http://www.keyestudio.com

*References and After-sales Service

1.Download Profile:https://fs.keyestudio.com/KS0428
2.Feel free to contact us please, if there is missing part or you encounter some troubles. Welcome to send email to us:service@keyestudio.com.
We will update projects and products continuously based on your sincere advice.

*Warning

1.This product contains tiny parts(screws, copper pillars), keep it out of reach of children under 7 years old please.
2. This product contains conductive parts (control board and electronic module). Please operate according to the requirements of this tutorial. Improper operation may cause overheating and damage parts. Do not touch and immediately disconnect the circuit power.


*Copyright

The keyestudio trademark and logo are the copyright of KEYES DIY ROBOT co.,LTD. All products under keyestudio brand can’t be copied, sold and resold without authorization by anyone or company. If you’re interested in our items, please contact to our sales representatives: fennie@keyestudio.com


thumb



Introduction

Nowadays, technological education such as VR, kids programming, and artificial intelligence, has become mainstream in educational industry. Thereby, people attach importance to STEAM education. Arduino is pretty notable in Maker education.

So what is Arduino? Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. Based on this, Keyestudio team has designed a mini tank robot. The tank robot has a processor which is programmable using the Arduino IDE, to mapped its pins to sensors and actuators by a shield that plug in the processor, it reads sensors and controls the actuators and decides how to operate.

15 learning projects, from simple to complex, will guide you how to make a smart tank robot on you own and introduce the detailed knowledge about sensors and modules.
Simultaneously, it is the best choice if you intend to obtain a DIY robot for learning programming, entertainment and competition requirement.


Features

1.Multi-purpose: Obstacle avoidance, follow, IR remote control, Bluetooth control, light following, ultrasonic follow and displayed face emoticons.
2.Simple assembly: No soldering circuit required, complete assembly easily.
3. High Tenacity:Aluminum alloy bracket, metal motors, high quality wheels and tracks.
4. High extension: expand other sensors and modules through motor driver shield and sensor shield
5. Multiple controls: IR remote control, App control(IOS and Android system)
6.Basic programming:C language code of Arduino IDE.

Specification

  • Working voltage: 5v
  • Input voltage: 7-12V
  • Maximum output current: 2A
  • Maximum power dissipation: 25W (T=75℃)
  • Motor speed: 5V 200 rpm/min
  • Motor drive mode: dual H bridge drive (L298P)
  • Ultrasonic induction angle: <15 degrees
  • Ultrasonic detection distance: 2cm-400cm
  • Infrared remote control distance: 10 meters (measured)
  • Bluetooth remote control distance: 50 meters (measured)

Product List

Make sure no any parts missing please when you get this robot kit. And if it is, please feel free to contact us.

Please tear off the protective film on the acrylic board before mounting the kit
thumb


thumb

thumb

thumb

thumb

thumb

thumb

thumb
QQ图片20210422152554.png|

Assembly Guide

Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code

Note: Peel the plastic film off the board first when installing the smart car. To be honest, we never intend to send wood to you.

After making an inventory all the parts in this kit, then we need to mount the tank robot. Let’s install smart car in compliance with the following instructions.

Step 1: Install Bottom Motor

Prepare the parts as follows:

  • M4 Nut * 2
  • Metal Motor *2
  • Metal Holder *2
  • Copper Coupler *2
  • Blue Supportive Parts *2
  • M4*12MM Inner Hex Screw * 2
  • M1.5 Hex Key Nickel Plated Allen Wrench *1
  • M3 Hex Key Nickel Plated Allen Wrench *1
  • M2.5 Hex Key Nickel Plated Allen Wrench *1
  • M3*8MM Inner Hex Screw * 4


thumb

thumb

Step 2: Install Driver Wheel

Prepare the parts as follows:

  • M4*12MM Inner Hex Screw * 2
  • M4*50MM Inner Hex Screw * 2
  • Tank Load-bearing Wheel * 2
  • Flange Bearing * 4
  • Copper Bush *2
  • Caterpillar Band *2
  • M4 Self-locking Nut * 2
  • M3 Hex Key Nickel Plated Allen Wrench *1


thumb

thumb

thumb


Step 3: Install the Battery Holder

Prepare the parts as follows:


thumb



  • Battery Holder *1
  • M3 Nut * 2
  • Blue Metal holder *2
  • M4 Nut *8
  • M3*10MM Flat Head Screw * 2
  • M4*40MM Inner Hex Screw *4
  • M2.5 Hex Key Nickel Plated Allen Wrench*1
  • M3 Hex Key Nickel Plated Allen Wrench *1
  • M3*25MM Inner Hex Screw *4

Finish the mount process. Go to fix the metal holder on the motor wheel with four M4*40MM inner hex screws and four M4 nuts.
thumb

thumb

thumb

Step 4: Mount Acrylic Board and Sensors

  • Acrylic Board * 2
  • L- type Black Bracket *1
  • Photocell Sensor *2
  • IR Receiver Module *1
  • 8X16 LED Panel *1
  • M2 Nut *4
  • M3 Nut *10
  • M3*6MM Inner Hex Screw * 8
  • M2.5 Hex Key Allen Wrench *1
  • M3*12MM Round Head Screw *7
  • M3*10MM Hexagon Copper Bush *8
  • M2*10MM Round Head Screw * 4


thumb

thumb

thumb

thumb

Step 5: Install the Servo Platform

Prepare the parts as follows:

  • Servo *1
  • Black Gimbal *1
  • Cable Tie *2
  • M2x8 Round Head Cross Tapping Screw *2
  • Ultrasonic Sensor *1
  • M2*4 Screw *1
  • M1.2*5 Screw *4


Note: for convenient debugging, keep the ultrasonic module straight ahead and the angle of servo motor 90°. Therefore, we need to set servo to 90° before installing the servo platform.


thumb


thumb

You can find M1.2*5 Screws inside the bag of Plastic Platform.


thumb


Step 6: Install Sensors and Boards

Prepare the parts as follows:

  • M3*6MM Round Head Screw *12
  • L298P Shield *1
  • V4.0 Board *1
  • V5 Sensor Shield *1
  • Screwdriver *1
  • Bluetooth Module *1


thumb

thumb

thumb thumb

thumb

Step 7: Hook-up Guide


thumb

thumb

thumb

thumb

thumb


Step 8: Wire up LED Panel


thumb

thumb

thumb

thumb


Step 9: install all parts of Acrylic plate
thumb

thumb

thumb

thumb

thumb

thumb

thumb

thumb


thumb

thumb

thumb

thumb

thumb

Step 10: Final Renderings


thumb

Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code.

Install Arduino IDE and Driver

Download software

When we get control board, we need to download Arduino IDE and driver firstly. You could download Arduino IDE from the official website: https://www.arduino.cc/, click the SOFTWARE on the browse bar, click “DOWNLOADS” to enter download page, as shown below:


thumb

There are various versions for Arduino, just download a suitable version for your system, we will take WINDOWS system as an example to show you how to download and install.


thumb

There are two versions for WINDOWS system, one is installed version, another one is download version, you just need to download file to computer directly and unzip it. These two versions can be used normally. Choose one and download on your computer.


thumb

You just need to click JUST DOWNLOAD, then click the downloaded file to install it. And when the ZIP file is downloaded, you can directly unzip and start it.

Keyestudio V4.0 Development Board

We need to know keyestudio V4.0 development board, as a core of this smart car.


thumb

keyestudio V4.0 development board is an Arduino uno-compatible board, which is based on ATmega328P MCU, and with a cp2102 Chip as a UART-to-USB converter.  


thumb

It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, 2 ICSP headers and a reset button.


thumb

It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it via an external DC power jack (DC 7-12V) or via female headers Vin/ GND(DC 7-12V) to get started.


thumb

Installing driver

Let’s install the driver of keyestudio PLUS control board. The USB-TTL chip on PLUS board adopts CP2102 serial chip. The driver program of this chip is included in Arduino 1.8 version and above, which is convenient. Plug on USB port of board, the computer can recognize the hardware and automatically install the driver of CP2102.

If install unsuccessfully, or you intend to install manually, open the device manager of computer. Right click Computer----- Properties----- Device Manager.


thumb

There is a yellow exclamation mark on the page, which implies installing the driver of CP2102 unsuccessfully. Then we double click the hardware and update the driver.


thumb

Click “OK” to enter the following page, click “browse my computer for updated driver software”, find out the installed or downloaded ARDUINO software. As shown below:


thumb

There is a DRIVERS folder in Arduino software installed package(thumb), open driver folder and you can see the driver of CP210X series chips.


We click “Browse”, then find out the driver folder, or you could enter “driver” to search in rectangular box, then click “next”, the driver will be installed successfully. (I place Arduino software folder on the desktop, you could follow my way)


thumb


Open device manager, we will find the yellow exclamation mark disappear. The driver of CP2102 is installed successfully.
thumb

thumb




Install other visions of driver

If your development board is Arduino UNO board, install the driver as follows:
Step 1: Plug in the development board, click Computer----- Properties----- Device Manager, you could see the unknown device is shown.


0428---100.png

Step 2: Update the driver


0428---101.png

Step 3: click“browse my computer for updated driver software”


0428---102.png
Step 4: find out the folder where the ARDUINO software is installed, click drivers folder and tap“Next”
0428---103.png

Step 5: the driver is installed successfully.


0428---104.png

The device manager shows the serial port of Arduino.
0428---105.png


Arduino IDE Setting

Click0486-12.png icon,open Arduino IDE.
0085=14.png

To avoid the errors when uploading the program to the board, you need to select the correct Arduino board that matches the board connected to your computer. Then come back to the Arduino software, you should click Tools→Board, select the board. (as shown below)
0085=15.png


Then select the correct COM port (you can see the corresponding COM port after the driver is successfully installed)


0085=16.png

0085=17.png

Before uploading the program to the board, let’s demonstrate the function of each symbol in the Arduino IDE toolbar.

A- Used to verify whether there is any compiling mistakes or not.
B- Used to upload the sketch to your Arduino board.
C- Used to create shortcut window of a new sketch.
D- Used to directly open an example sketch.
E- Used to save the sketch.
F- Used to send the serial data received from board to the serial monitor.

Start your first program

Open the file to select Example, choose BLINK from BASIC, as shown below:



048610png.png


048611png.png

Set board and COM port, the corresponding board and COM port are shown on the lower right of IDE.


048612png.png

Click 0486-20.png to start compiling the program, check errors.


048613png.png

Click 0486-23.pngto upload the program, upload successfully.


048614png.png

Upload the program successfully, the onboard LED lights on for 1s, lights off for 1s. Congratulation, you finish the first program.



How to Add a Library?

What are Libraries ?
Libraries are a collection of code that makes it easy for you to connect to a sensor,display, module, etc.
For example, the built-in LiquidCrystal library helps talk to LCD displays. There are hundreds of additional libraries available on the Internet for download.
The built-in libraries and some of these additional libraries are listed in the reference.

Here we will introduce the most simple way for you to add libraries .
Step 1:After downloading well the Arduino IDE, you can right-click the icon of Arduino IDE.
Find the option "Open file location" shown as below:


thumb

Step 2: Enter it to find out libraries folder which is the library file of Arduino.


thumb


Step 3:Next to find out the“libraries”of tank robot(seen in the link: https://fs.keyestudio.com/KS0428), you just need to replicate and paste into the libraries folder of Arduino IDE.


thumb

thumb

Then the libraries of tank robot are installed successfully, as shown below:
thumb

Projects

Project 1: LED Blink

(1)Description
thumb

For the starter and enthusiast, this is a fundamental program---LED Blink.
LED, the abbreviation of light emitting diodes, consist of Ga, As, P, N chemical compound and so on. The LED can flash diverse color by altering the delay time in the test code. When in control, power on GND and VCC, the LED will be on if S end is high level; nevertheless, it will go off.

(2)Specification


thumb

  • Control interface: digital port
  • Working voltage: DC 3.3-5V
  • Pin spacing: 2.54mm
  • LED display color: red

(3)Equipment


thumb


(4) V5 Sensor Shield
There will be troublesome when we combine Arduino development boards with numerous sensors. However, the V5 sensor shield, compatible with Arduino development board, address this problem perfectly. Just stack V5 board on it. This sensor shield can be inserted into 3pin sensor modules and breaks out some some communication pins, like serial, IIC, and SPI communication as well.

Pins Description
thumb


Connection Diagram:


thumb

Seen from the above diagram, LED is linked with D2

(5) Test Code

/*
 keyestudio Mini Tank Robot V2
 lesson 1.1
 Blink
 http://www.keyestudio.com
*/
void setup()
 { 
    pinMode(2, OUTPUT);// initialize digital pin 2 as an output.
}
void loop() // the loop function runs over and over again forever

{
   digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
   delay(1000); // wait for a second
   digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
   delay(1000); // wait for a second
}
//****************************************************************

(6) Test Result

(There will be contradict about serial communication between code and Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.) Upload the program on the development board, LED flicker with the interval of 1s.


thumb

(7)Code Explanation

pinMode(2,OUTPUT) - Set pin2 to OUTPUT

digitalWrite(2,HIGH) - When set pin2 to HIGH level(output 5V) or to LOW level(output 0V)

(8)Extension Practice We succeed to blink LED. Next, let’s observe what LED will change if we modify pins and delay time.


Connection Diagram


thumb

We’ve altered pins and connected LED to D10.

Test Code:


/*
 keyestudio Mini Tank Robot V2
 lesson 1.2
 delay
 http://www.keyestudio.com
*/
void setup() {  // initialize digital pin 10 as an output.
   pinMode(10, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
   digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
   delay(100); // wait for 0.1 second
   digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
   delay(100); // wait for 0.1 second
}
//****************************************************************

The LED flickers faster than before through the test result, therefore, pins and delay time affect flash frequency.

Project 2: Adjust LED Brightness

(1)Description In previous lesson, we control LED on and off and make it blink. In this project, we will control LED brightness through PWM to simulate breathing effect. Similarly, you can change the step length and delay time in the code so as to demonstrate different breathing effect. PWM is a means of controlling the analog output via digital means. Digital control is used to generate square waves with different duty cycles (a signal that constantly switches between high and low levels) to control the analog output.In general, the input voltage of port are 0V and 5V. What if the 3V is required? Or what if switch among 1V, 3V and 3.5V? We can’t change resistor constantly. For this situation, we need to control by PWM.


thumb

For the Arduino digital port voltage output, there are only LOW and HIGH, which correspond to the voltage output of 0V and 5V. You can define LOW as 0 and HIGH as 1, and let the Arduino output five hundred 0 or 1 signals within 1 second. If output five hundred 1, that is 5V; if all of which is 1, that is 0V. If output 010101010101 in this way then the output port is 2.5V, which is like showing movie. The movie we watch are not completely continuous. It actually outputs 25 pictures per second. In this case, the human can’t tell it, neither does PWM. If want different voltage, need to control the ratio of 0 and 1. The more 0,1 signals output per unit time, the more accurately control.


(2) Specification

  • Control interface: digital port
  • Working voltage: DC 3.3-5V
  • Pin spacing: 2.54mm
  • Display color: red


(3) Equipment
thumb

(4) Connection Diagram
thumb

(5) Test Code

/*
 keyestudio Mini Tank Robot V2
 lesson 2.1
 pwm
 http://www.keyestudio.com
*/
int ledPin = 10; // Define the LED pin at D10
int value;
void setup () 
{
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop () 
{
for (value = 0; value <255; value = value + 1)
{
analogWrite (ledPin, value); // LED lights gradually light up
delay (5); // delay 5MS
}
for (value = 255; value> 0; value = value-1)
{
analogWrite (ledPin, value); // LED gradually goes out
delay (5); // delay 5MS
}}//**********************************************************

(6)Test Result

Upload test code successfully, LED gradually becomes brighter then darker, like human breath, rather than light on and off immediately.

(7)Code Explanation

When we need to repeat some statements, we could use FOR statement.
FOR statement format is shown below:


thumb


FOR cyclic sequence: Round 1:1 → 2 → 3 → 4 Round 2:2 → 3 → 4 … Until number 2 is not established, “for”loop is over, After knowing this order, go back to code: for (int value = 0; value < 255; value=value+1){

       ...

} for (int value = 255; value >0; value=value-1){

      ...

}

The two“for”statements make value increase from 0 to 255, then reduce from 255 to 0, then increase to 255,....infinitely loop There is a new function in the following ----- analogWrite() We know that digital port only has two state of 0 and 1. So how to send an analog value to a digital value? Here,this function is needed. Let’s observe the Arduino board and find 6 pins marked“~”which can output PWM signals.

Function format as follows: analogWrite(pin,value) analogWrite() is used to write an analog value from 0~255 for PWM port, so the value is in the range of 0~255. Attention that you only write the digital pins with PWM function, such as pin 3, 5, 6, 9, 10, 11.
PWM is a technology to obtain analog quantity through digital method. Digital control forms a square wave, and the square wave signal only has two states of turning on and off (that is, high or low levels). By controlling the ratio of the duration of turning on and off, a voltage varying from 0 to 5V can be simulated. The time turning on(academically referred to as high level) is called pulse width, so PWM is also called pulse width modulation.
Through the following five square waves, let’s acknowledge more about PWM.


thumb

In the above figure, the green line represents a period, and value of analogWrite() corresponds to a percentage which is called Duty Cycle as well. Duty cycle implies that high-level duration is divided by low-level duration in a cycle. From top to bottom, the duty cycle of first square wave is 0% and its corresponding value is 0. The LED brightness is lowest, that is, turn off. The more time high level lasts, the brighter the LED.
Therefore, the last duty cycle is 100%, which correspond to 255, LED is brightest. 25% means darker.
PWM mostly is used for adjusting the LED brightness or rotation speed of motor.
It plays vital role in controlling smart robot car. I believe that you can’t wait to enter next project.


(8) Extension Practice:

Let’s modify the value of delay and remain the pin unchanged, then observe how LED changes.

/*
 keyestudio Mini Tank Robot V2
 lesson 2.2
 pwm-slow
 http://www.keyestudio.com
*/
int ledPin = 10; // Define the LED pin at D10
int value;
void setup () 
{
pinMode (ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop () 
{
for (value = 0; value <255; value = value + 1)
{
analogWrite (ledPin, value); // LED lights gradually light up
delay (30); // delay 30MS
}
for (value = 255; value> 0; value = value-1)
{
analogWrite (ledPin, value); // LED gradually goes out
delay (30); // delay 30MS
}}//**********************************************************

Upload code on the development board and LED gradually get brighter then darker.

Project 3: Photoresistor Sensor


thumb

(1)Description

The photoresistor is a special resistor made of semiconductor materials such as CdS or Selenide septum. The surface is also coated with moisture-proof resin, which has a photoconductive effect. It is sensitive to ambient light. Its resistance varies from different light intensities.

We use the characteristics of the photo-resistor to design the circuit and generate the photo-resistor module.
Connecting the signal pin of photocell module to Analog port, when the stronger the light intensity, the greater the voltage of analog port, the greater the analog value is.
On the contrary, the weaker the light intensity, the smaller the voltage of analog port, the smaller the analog value is. Based on that, we can use the photocell module to read the analog value, so get the ambient light intensity.

(2)Specification


thumb

  • Resistance:5K ohm-0.5Mohm
  • Interface Type: analog
  • Working Voltage: 3.3V-5V
  • Easy installation: with screw fixing holes
  • Pin spacing: 2.54mm

(3)Equipment


thumb

Connection Diagram:


thumb
The two photoresistor sensors are linked with A1 and A2, then finish the experiment via photoresistor connected to A1. Then finish the following experiment via photoresistor connected A1. Let’s read its analog value.


(4)Test Code


/*
 keyestudio Mini Tank Robot V2
 lesson 3.1
 photocell
 http://www.keyestudio.com
*/
int sensorPin = A1;    // select the input pin for the photocell
int sensorValue = 0;  // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);  // read the value from the sensor:
Serial.println(sensorValue);  //Serial port prints the resistance value
delay(500);
}
//******************************************************

(5)Test Result


thumb


Upload code on development board, open serial monitor, check if its value diminishes when covering photoresistor. However, the value increases when uncovered.

(6)Code Explanation analogRead(sensorPin):read the analog value of photoresistor via analog ports. Serial.begin(9600): Initialize the serial port, baud rate of serial communication is 9600 Serial.println : Serial port prints and word wrap.


(7)Extension Practice We’ve known how to read the value of photoresistor. Let’s combine photoresistor with LED and view the status of LED.


thumb
PWM restrains the brightness, so LED is linked with PWM pins, connect LED to pin 10, keep pin of photoresistor unchanged, then design the code:

/*keyestudio Mini Tank Robot V2
lesson 3.2
photocell-analog output
http://www.keyestudio.com
*/
int analogInPin = A1;  // Analog input pin that the photocell is attached to
int analogOutPin = 10; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
 }
void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);
  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
 Serial.println(sensorValue);  //serial port prints the value of photoresistor
delay(2);
}
//***************************************************************

Upload code, press it by hand and see the the LED brightness.


Project 4: Servo Control

(1)Description


thumb

Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtain the voltage difference output.

When the motor speed is constant, the potentiometer is driven to rotate through the cascade reduction gear, which leads that the voltage difference is 0, and the motor stops rotating. Generally, the angle range of servo rotation is 0° --180 °

The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle. 


thumb

The corresponding servo angles are shown below:
thumb


(2)Specification

  • Working voltage: DC 4.8V ~ 6V
  • Operating angle range: about 180 ° (at 500 → 2500 μsec)
  • Pulse width range: 500 → 2500 μsec
  • No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V) 0.1 ± 0.01 sec / 60 (DC 6V)
  • No-load current: 200 ± 20mA (DC 4.8V) 220 ± 20mA (DC 6V)
  • Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) 1.5 ± 0.1kg · cm (DC 6V)
  • Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V)
  • Standby current: 3 ± 1mA (DC 4.8V) 4 ± 1mA (DC 6V)


(3)Equipment


thumb

(4)Connection Diagram:


thumb

Wiring note: the brown line of servo is linked with Gnd(G), the red line is connected to 5v(V) and orange line is attached to digital 9.

The servo have to be connected to external power, due to its high demand for driving servo current. Generally, the current of development board is not enough. If without connected power, the development board could be burnt.


(5)Test Code1

/*
keyestudio Mini Tank Robot V2
lesson 4.1
Servo
http://www.keyestudio.com
*/
#define servoPin 9  //servo Pin
int pos; //angle variable of servo
int pulsewidth; // pulse width variable of servo
void setup() {
  pinMode(servoPin, OUTPUT);  //set servo pin to OUTPUT
  procedure(0); //set the angle of servo to 0°
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    procedure(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                   //control the rotation speed of servo
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    procedure(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                    
  }
}
// function to control servo
void procedure(int myangle) {
  pulsewidth = myangle * 11 + 500;  //calculate the value of pulse width
  digitalWrite(servoPin,HIGH);
  delayMicroseconds(pulsewidth);   //The duration of high level is pulse width
  digitalWrite(servoPin,LOW);
  delay((20 - pulsewidth / 1000));  // the cycle is 20ms, the low level last for the rest of time
}
//********************************************************************

Upload code successfully, servo swings back in the range of 0° to 180°

There is another guide for restraining servo---- servo library file, the following link of official website is for your reference. https://www.arduino.cc/en/Reference/Servo

Servo library file

(6)Test Code2

/*
 keyestudio Mini Tank Robot V2
 lesson 4.2
 servo
 http://www.keyestudio.com
*/
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
//****************************************************************

(7)Test Result

Upload code successfully and power on, servo rotates in the range of 0° to 180°. The result is same. We usually control it by library file.

(8)Code Explanation

Arduino comes with #include <Servo.h> (servo function and statement) The following are some common statements of the servo function: 1. attach(interface)——Set servo interface, port 9 and 10 are available

3.write(angle)——The statement to set rotation angle of servo, the angle range is from 0° to 180° 3. read()——The statement to read angle of servo, read the command value of “write()” 4. Note: The above written format is “servo variable name, specific statement()”, for instance: myservo.attach(9)

Project 5: Ultrasonic Sensor

(1)Description


thumb

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules.
The HC-SR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with arduino and ultrasonic sensor and how to use ultrasonic sensor with arduino.

(2)Specification


thumb


  • Power Supply :+5V DC
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS


(3)Equipment


thumb

(4)The principle of ultrasonic sensor

As the above picture shown, it is like two eyes. One is transmitting end, the other is receiving end.

The ultrasonic module will emit the ultrasonic waves after trigger signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal and echo signal. The t is the time that emitting signal meets obstacle and returns. and the propagation speed of sound in the air is about 343m/s, therefore, distance = speed * time, because the ultrasonic wave emits and comes back, which is 2 times of distance, so it needs to be divided by 2, the distance measured by ultrasonic wave = (speed * time)/2

1.Use method and timing chart of ultrasonic module: Setting the delay time of Trig pin of SR04 to 10μs at least, which can trigger it to detect distance. 2. After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return. This step will be completed automatically by the module. 3. If the signal returns, the Echo pin will output a high level, and the duration of the high level is the time from the transmission of the ultrasonic wave to the return.



thumb

Circuit diagram of ultrasonic sensor:


thumb


(5)Connection Diagram


thumb


thumb


(6)Test Code

/*
 keyestudio Mini Tank Robot V2
 lesson 5
 Ultrasonic sensor
 http://www.keyestudio.com
*/ 
int trigPin = 5;    // Trigger
int echoPin = 4;    // Echo
long duration, cm, inches;
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop() {
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
   // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);
   // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
}
//****************************************************************

(7)Test Result Upload test code on the development board, open serial monitor and set baud rate to 9600. The detected distance will be displayed, unit is cm and inch. Hinder the ultrasonic sensor by hand, the displayed distance value is smaller.



thumb


(8)Code Explanation int trigPin = 5; this pin is defined pin to transmit ultrasonic waves, generally output. int echoPin = 4; this is defined as the pin of reception, generally input cm = (duration/2) / 29.1; inches = (duration/2) / 74; by 0.0135 We can calculate the distance by using the following formula: distance = (traveltime/2) x speed of sound The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS We need to divide the traveltime by 2 because we have to take into account that the wave was sent, hit the object, and then returned back to the sensor. (9)Extension Practice: We have just measured the distance displayed by the ultrasonic. How about controlling the LED with the measured distance? Let's try it, connect an LED light module to the D10 pin.


thumb

/*
 keyestudio Mini Tank Robot V2
 lesson 5
 Ultrasonic LED
 http://www.keyestudio.com
*/ 
int trigPin = 5;    // Trigger
int echoPin = 4;    // Echo
long duration, cm, inches;
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 void loop() 
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);
  // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
if (cm>=2 && cm<=10)
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
}
//****************************************************************

Upload test code to development board and block ultrasonic sensor by hand, then check if LED is on

Project 6: IR Reception

(1)Description
There is no doubt that infrared remote control is ubiquitous in daily life. It is used to control various household appliances, such as TVs, stereos, video recorders and satellite signal receivers. Infrared remote control is composed of infrared transmitting and infrared receiving systems, that is, an infrared remote control and infrared receiving module and a single-chip microcomputer capable of decoding.​    
The 38K infrared carrier signal emitted by remote controller is encoded by the encoding chip in the remote controller. It is composed of a section of pilot code, user code, user inverse code, data code, and data inverse code. The time interval of the pulse is used to distinguish whether it is a 0 or 1 signal and the encoding is made up of these 0, 1 signals.
The user code of the same remote control is unchanged. The data code can distinguish the key.
When the remote control button is pressed, the remote control sends out an infrared carrier signal. When the IR receiver receives the signal, the program will decode the carrier signal and determines which key is pressed. The MCU decodes the received 01 signal, thereby judging what key is pressed by the remote control.
Infrared receiver we use is an infrared receiver module. Mainly composed of an infrared receiver head, it is a device that integrates reception, amplification, and demodulation. Its internal IC has completed demodulation, and can achieve from infrared reception to output and be compatible with TTL signals. Additionally, it is suitable for infrared remote control and infrared data transmission. The infrared receiving module made by the receiver has only three pins, signal line, VCC and GND. It is very convenient to communicate with arduino and other microcontrollers.


thumb


(2)Specification


thumb

  • Operating Voltage: 3.3-5V(DC)
  • Interface: 3PIN
  • Output Signal: Digital signal
  • Receiving Angle: 90 degrees
  • Frequency: 38khz
  • Receiving Distance: 10m


(3)Equipment


thumb

(4)Connection Diagram


thumb

Respectively link “-”、“+” and S of IR receiver module with G(GND), V(VCC)and A0 of keyestudio development board.
Attention: On the condition that digital ports are not available, analog ports can be regarded as digital ports. A0 equals to D14, A1 is equivalent to digital 15.


(5)Test Code

Firstly import library file of IR receiver module(refer to how to import Arduino library file) before designing code.

/*
keyestudio Mini Tank Robot V2
lesson 6
IRremote
http://www.keyestudio.com
*/ 
#include <IRremoteTank.h>     // IRremote library statement
int RECV_PIN = A0;        //define the pins of IR receiver as A0
IRrecv irrecv(RECV_PIN);   
decode_results results;   // decode results exist in the “result” of “decode results”
void setup()  
	{  
      Serial.begin(9600);  
      irrecv.enableIRIn(); //Enable receiver
	}  
 void loop() {  
	  if (irrecv.decode(&results))//decode successfully, receive a set of infrared signals
	  {  
	    Serial.println(results.value, HEX);//Wrap word in 16 HEX to output and receive code 
	    irrecv.resume(); // Receive the next value
	  }  
	  delay(100);  
	}  
//*******************************************************


(6)Test Result

Upload test code, open serial monitor and set baud rate to 9600, point remote control to IR receiver and the corresponding value will be shown, if pressing so long, the error codes will appear.


thumb

Below we have listed out each button value of keyestudio remote control. So you can keep it for reference.


thumb

(7)Code Explanation
irrecv.enableIRIn(): after enabling IR decoding, the IR signals will be received, then function“decode()”will check continuously if decode successfully.

irrecv.decode(&results): after decoding successfully, this function will come back to “true”, and keep result in “results”. After decoding a IR signals, run the resume()function and receive the next signal (8)Extension Practice We decoded the key value of IR remote control. How about restrain LED by the measured value? We could operate an experiment to affirm. Attach a LED to D10. When keep the pin of IR receiver unchanged, LED will be on when pressing the key of remote control; press again, LED will be off.


thumb


/* keyestudio Mini Tank Robot V2
lesson 6.2
IRremote
http://www.keyestudio.com
*/ 
#include <IRremoteTank.h>
int RECV_PIN = A0;//define the pin of IR receiver as A0
int LED_PIN=10;//define the pin of LED
int a=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Initialize the IR receiver 
  pinMode(LED_PIN,OUTPUT);//set LED_pin to OUTPUT
}
void loop() {
  if (irrecv.decode(&results)) {
if(results.value==0xFF02FD &a==0) // according to the above key value, press“OK”on remote control , LED will be controlled
{
    digitalWrite(LED_PIN,HIGH);//LED will be on
a=1;
}
else if(results.value==0xFF02FD &a==1) //press again
{
digitalWrite(LED_PIN,LOW);//LED will go off
a=0;
}
    irrecv.resume(); //receive the next value
  }
}
//*******************************************************

Upload code to development board, press“OK”key on remote control to make LED on and off.

Project 7: Bluetooth Remote Control

(1)Description


thumb

Bluetooth, a simple wireless communication module most popular since the last few decades and easy to use are being used in most of the battery-powered devices.
Over the years, there have been many upgrades of Bluetooth standard to keep fulfil the demand of customers and technology according to the need of time and situation. Over the few years, there are many things changed including data transmission rate, power consumption with wearable and IoT Devices and Security System.
Here we are going to learn about HM-10 BLE 4.0 with Arduino Board. The HM-10 is a readily available Bluetooth 4.0 module. This module is used for establishing wireless data communication. The module is designed by using the Texas Instruments CC2540 or CC2541 Bluetooth low energy (BLE) System on Chip (SoC).


(2)Specification

  • Bluetooth protocol: Bluetooth Specification V4.0 BLE
  • No byte limit in serial port Transceiving
  • In open environment, realize 100m ultra-distance communication with iphone4s
  • Working frequency: 2.4GHz ISM band
  • Modulation method: GFSK(Gaussian Frequency Shift Keying)
  • Transmission power: -23dbm, -6dbm, 0dbm, 6dbm, can be modified by AT command.
  • Sensitivity: ≤-84dBm at 0.1% BER
  • Transmission rate: Asynchronous: 6K bytes ; Synchronous: 6k Bytes
  • Security feature: Authentication and encryption
  • Supporting service: Central & Peripheral UUID FFE0, FFE1
  • Power consumption: Auto sleep mode, stand by current 400uA~800uA, 8.5mA during transmission.
  • Power supply: 5V DC
  • Working temperature: –5 to +65 Centigrade


(3)Equipment


thumb

(4)Connection Diagram

  • 1. STATE: state test pins, connected to internal LED, generally keep it unconnected.
  • 2. RXD: serial interface, receiving terminal.
  • 3. TXD: serial interface, transmitting terminal.
  • 4. GND: Ground.
  • 5. VCC: positive pole of the power source.
  • 6. EN/BRK: break connect, it means breaking the Bluetooth connection, generally, keep it unconnected.


thumb

(5)Test Code

/*
 keyestudio Mini Tank Robot v2.0
 lesson 7.1
 bluetooth 
http://www.keyestudio.com
*/

char ble_val; //character variable: save the value of Bluetooth reception

void setup() {
  Serial.begin(9600);
}
void loop() {
  if(Serial.available() > 0)  //Judge if there is data in serial buffer
  {
    ble_val = Serial.read();  //Read data from serial buffer
    Serial.println(ble_val);  //Print
  }}
//*******************************************


(There will be contradiction between serial communication of code and communication of Bluetooth when uploading code, therefore, don’t link with Bluetooth module before uploading code.)
After uploading code on development board, then insert Bluetooth module, wait for the command from cellphone.

(6)Download APP

Allow APP to access “location” in settings of your cellphone when connecting to Bluetooth module.

The code is reading the received signal, and we also need a stuff to send signal. In this project, we send signal to control robot car via cellphone.
Then we need to download the APP.

1.For iOS system

Enter APP STORE to search BLE Scanner 4.0, then download it.


thumb

2.For Android system

Enter Google Play to find out BLE Scanner, then download.
And allow APP to access“location”, you could enable “location”in settings of your cellphone.



thumb

  • 3.After installation, open App and enable “Location and Bluetooth” permission.
  • 4.We take iOS version as example. The operation method of Android version is almost same as it.
  • 5.Scan Bluetooth module to get Bluetooth BLE 4.0. Its name is HMSoft.

Then click“connect”to link with Bluetooth and use it.


thumb

6. After connecting to HMSoft, click it to get multiple options, such as device information, access permission, general and custom service. Choose “CUSTOM SERVICE”


thumb

7.Then pop up the following page


thumb

8. Click(Read,Notify,WriteWithoutResponse)to enter the following page


thumb

9.Click Write Value, appear the interface to enter HEX or Text.


thumb

10.Open the serial monitor on Arduino,enter a 0 or other character at Text interface.


thumb

Then click“Write”, open serial monitor to view if there is a “0” signal
thumb


(7)Code Explanation

Serial.available() : The current rest characters when return to buffer area. Generally, this function is used to judge if there is data in buffer. When Serial.available()>0, it means that serial receives the data and can be read
Serial.read():Read a data of a Byte in buffer of serial port, for instance, device sends data to Arduino via serial port, then we could read data by “Serial.read()” (8)Extension Practice
We could send a command via cellphone to turn on and off a LED.
D10 is connected to a LED, as shown below:


thumb

/*
 keyestudio Mini Tank Robot v2.0
 lesson 7.2
 Bluetooth 
 http://www.keyestudio.com
*/ 
int ledpin=11;
void setup()
{Serial.begin(9600);
 pinMode(ledpin,OUTPUT);
}
void loop()
{ int i;
  if (Serial.available())
  {i=Serial.read();
    Serial.println("DATA RECEIVED:");
    if(i=='1')
    { digitalWrite(ledpin,1);
      Serial.println("led on");
    }
    if(i=='0')
    { digitalWrite(ledpin,0);
      Serial.println("led off");
}}}//*******************************************


thumb thumb

Click “Write” on APP, when you enter 1, LED will be on, when you input 0, LED will be off. (Remember to remove the Bluetooth module after finishing experiment, otherwise, burning code will be affected)

Project 8: Motor Driving and Speed Control

(1) Description


thumb

There are many ways to drive the motor. Our tank robot uses the most commonly used L298P solution. L298P is an excellent high-power motor driver IC produced by STMicroelectronics. It can directly drive DC motors, two-phase and four-phase stepping motors. The driving current up to 2A, and output terminal of motor adopts eight high-speed Schottky diodes as protection.
We designed a shield based on the circuit of L298p.
The stacked design reduces the technical difficulty of using and driving the motor.

(2)Specification

Circuit Diagram for L298P Board


thumb

  • 1)Logic part input voltage: DC5V
  • 2)Driving part input voltage: DC 7-12V
  • 3)Logic part working current: <36mA
  • 4)Driving part working current: <2A
  • 5)Maximum power dissipation: 25W (T=75℃)
  • 6)Working temperature: -25℃~+130℃
  • 7)Control signal input level: high level 2.3V<Vin<5V, low level -0.3V<Vin<1.5V


thumb

(3)Drive Robot to Move

Through the above circuit diagram, the direction pin of A motor is D12, and speed pin is D3; D13 is the direction pin of B motor, D11 is speed pin.
We know how to control digital ports according to the following chart.
PWM decides 2 motors to turn so as to drive robot car. The PWM value is in the range of 0-255, the larger the number, the faster the motor rotates.


thumb

(4)Equipment


thumb

(5)Connection Diagram


thumb

Note: the 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4.

(6)Test Code

/*
 keyestudio Mini Tank Robot v2.0
 lesson 8.1
 motor driver
 http://www.keyestudio.com
*/ 

#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define the PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3   // define the PWM control pin of right motor

void setup()
{
  pinMode(ML_Ctrl, OUTPUT);//define direction control pin of left motor as output
  pinMode(ML_PWM, OUTPUT);//define PWM control pin of left motor as output
  pinMode(MR_Ctrl, OUTPUT);//define direction control pin of right motor as output.
  pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as output
}

void loop()
{ 
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200

  //front
  delay(2000);//delay in 2s
   digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200  
digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200

   //back
  delay(2000);//delay in 2s 
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of left motor to HIGH
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200

    //left
  delay(2000);//delay in 2s
   digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,200);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,200);//set the PWM control speed of right motor to 200

   //right
  delay(2000);//delay in 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);//set the PWM control speed of right motor to 0

    //stop
  delay(2000);//delay in 2s
}//*****************************************


(7)Test Result

Hook up by connection diagram, upload code and power on, smart car goes forward and back for 2s, turns left and right for 2s, stops for 2s and alternately.
(8) Code Explanation
digitalWrite(ML_Ctrl,LOW): the rotation direction of motor is decided by the high/low level and and the pins that decide rotation direction are general pins.
analogWrite(ML_PWM,200): the speed of motor is regulated by PWM, The speed of motor is regulated by PWM, and the pins that decide the speed of motor have to be PWM pins.


(9)Extension Practice

Hook up in same way


thumb

/*
 keyestudio Mini Tank Robot v2.0
 lesson 8.2
 motor driver pwm
 http://www.keyestudio.com
*/ 
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define the PWM control pin of left motor
#define MR_Ctrl 12  //define the control pin of right motor
#define MR_PWM 3   //define the PWM control pin of right motor
void setup()
{ pinMode(ML_Ctrl, OUTPUT);//define the direction control pin of left motor as OUTPUT
  pinMode(ML_PWM, OUTPUT);//define the PWM control pin of left motor as OUTPUT
  pinMode(MR_Ctrl, OUTPUT);//define the direction control pin of right motor as OUTPUT
  pinMode(MR_PWM, OUTPUT);//define the PWM control pin of right motor as OUTPUT

}
void loop()
{ digitalWrite(ML_Ctrl,LOW);//Set direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);// Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set the direction control pin of right motor to LOW
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
  //front
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,HIGH);//Set direction control pin of right motor to HIGH level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
   //back
  delay(2000);//define 2s
  digitalWrite(ML_Ctrl,HIGH);//Set direction control pin of left motor to HIGH level
  analogWrite(ML_PWM,100);//Set the PWM control speed of left motor to 100
  digitalWrite(MR_Ctrl,LOW);//Set direction control pin of right motor to LOW level
  analogWrite(MR_PWM,100);//Set the PWM control speed of right motor to 100
    //left
  delay(2000);//define 2s
   digitalWrite(ML_Ctrl,LOW);//set the direction control pin of left motor to LOW
  analogWrite(ML_PWM,100);//set the PWM control speed of left motor to 200
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of right motor to HIGH
  analogWrite(MR_PWM,100);//set the PWM control speed of right motor to 100
   //right
  delay(2000);//define 2s
  analogWrite(ML_PWM,0);//set the PWM control speed of left motor to 0
  analogWrite(MR_PWM,0);// set the PWM control speed of right motor to 0

    //stop
  delay(2000);//define 2s
}//******************************************************************

Upload code successfully, the motors rotate faster.

Project 9: LED Expression Panel

(1)Description


thumb

If we add an expression panel to the robot, it will be amazing. Keyestudio's 8*16 dot matrix can meet your requirements. You can create facial expressions, patterns or other interesting displays yourself. 8*16 LED light board comes with 128 LEDs. The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface, so as to control the 128 LEDs on the module, which produce the patterns you need on dot matrix. To facilitate wiring, we also provide a HX-2.54 4Pin wiring.


(2)Specification

  • Working voltage: DC 3.3-5V
  • Power loss: 400mW
  • Oscillation frequency: 450KHz
  • Drive current: 200mA
  • Working temperature: -40~80℃
  • Communication method: two-wire bus

(3)Equipment


thumb

(4)8*16 Dot Matrix Display

Circuit Graphic


thumb

The principle of 8*16 dot matrix:
How to control each led light of 8*16 dot matrix? We know that a byte has 8 bits, each bit is 0 or 1. When a bit is 0, turn off LED and when a bit is 0, turn on LED.
Thereby, one byte can control the LED in a row of dot matrix, so 16 bytes can control 16 columns of led lights, that is, 8*16 dot matrix.

Interface Description and Communication Protocol:
The data of the microprocessor (arduino) communicates with the AiP1640 through the two-wire bus interface.
The communication protocol diagram is shown below:

(SCLK) is SCL, (DIN) is SDA:


thumb

①The starting condition for data input: SCL is high level and SDA changes from high to low.
②For data command setting, there are methods as shown in the figure below.
In our sample program, select the way to add 1 to the address automatically, the binary value is 0100 0000 and the corresponding hexadecimal value is 0x40.


thumb

③For address command setting, the address can be selected as shown below. The first 00H is selected in our sample program, and the binary number 1100 0000 corresponds to the hexadecimal 0xc0


thumb

  • ④The requirement for data input is that SCL is high level when inputting data, the signal on SDA must remain unchanged. Only when the clock signal on SCL is low level, the signal on SDA can be altered. The data input is low-order first, high-order is behind
  • ⑤ The condition to end data transmission is that when SCL is low, SDA is low, and when SCL is high, the SDA level also becomes high.
  • ⑥ Display control, set different pulse width, the pulse width can be selected as shown below
  • In the example, we choose pulse width 4/16, and the hexadecimal corresponds to 1000 1010 is 0x8A



thumb

4. Introduction for Modulus Tool

The online version of dot matrix modulus tool: http://dotmatrixtool.com/#

①Open links to enter the following page.


thumb

②The dot matrix is 8*16 in this project, so set the height to 8, width to 16, as shown below.


thumb

③ Generate hexadecimal data from the pattern As shown below, press the left mouse button to select, the right button to cancel, draw the pattern you want, click Generate, and the hexadecimal data we need will be produced.


thumb


(5)Connection Diagram


thumb


Wiring note: The GND, VCC, SDA, and SCL of the 8x16 LED panel are respectively connected to -(GND), + (VCC), A4 and A5 of the keyestudio sensor expansion board for two-wire serial communication. (Note: This pin is connected to arduino IIC, but this module is not IIC communication, it can be linked with any two pins.)


(6)Test Code

The code that shows smile face

/*
 keyestudio Mini Tank Robot v2.0
 lesson 9.1
 Matrix  face
 http://www.keyestudio.com
*/ 
//the data of smiley from modulus tool
unsigned char smile[] = {0x00, 0x00, 0x1c, 0x02, 0x02, 0x02, 0x5c, 0x40, 0x40, 0x5c, 0x02, 0x02, 0x02, 0x1c, 0x00, 0x00};

#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4

void setup(){
  //Set pin to output
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  //clear the display
  //matrix_display(clear);
}
void loop(){
  matrix_display(smile);  // display smile face
}
// the function for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();  // use the function of the data transmission start condition
  IIC_send(0xc0);  //select address
  
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
     IIC_send(matrix_value[i]); //convey the pattern data
  }

  IIC_end();   //end the transmission of pattern data  
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16 s
  IIC_end();
}

//the condition to start conveying data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//Convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bit for every character
  {
      digitalWrite(SCL_Pin,LOW);  // pull down clock pin SCL_Pin to change the signal of SDA
      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up the clock pin SCL_Pin to stop transmission
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, shift the data to the right by one
  }
}

//The sign of ending data transmission
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
//******************************************************

(7)Test Result
Wire according to connection diagram. The DIP switch is dialed to right end and power on, the smile face appears on dot matrix.

thumb


(8)Extension Practice

We use the modulo tool (http://dotmatrixtool.com/#)to make the dot matrix alternately display start, go front and stop patterns then clear the patterns, the time interval is 2000 milliseconds.


thumb

Get the graphical code to be displayed via modulus tool

  • Start:
  • 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
  • Go front:
  • 0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00
  • Go back:
  • 0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00
  • Turn left:
  • 0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00
  • Turn left:
  • 0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00
  • Stop:
  • 0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00
  • Clear the displayed code:0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

The code that the multiple patterns shift:

/* keyestudio Mini Tank Robot v2.0
 lesson 9.2
 Matrix loop
 http://www.keyestudio.com
*/ 
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = 
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = 
{0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = 
{0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = 
{0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = 
{0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = 
{0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = 
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4
void setup(){
  //Set pins to output
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  //Clear the display
  matrix_display(clear);
}
void loop(){
  matrix_display(start01);  // Display start pattern
  delay(2000);
  matrix_display(front);    //Front pattern
  delay(2000);
  matrix_display(STOP01);   //Stop pattern
  delay(2000);
  matrix_display(clear);    //Clear the display Clear the screen
  delay(2000);
}
// This function is used to display of dot matrix
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();  //call the function that data transmission start  
  IIC_send(0xc0);  //Choose address
  
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
     IIC_send(matrix_value[i]); //data to convey patterns 
  }
  IIC_end();   //end the transmission of pattern dataEnd
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//Convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      
delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  //detect bit by bit, so shift the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);} 
//*****************************************************

Upload code on development board, 8*16 dot matrix display go front and back and stop patterns, alternately.


thumbthumbthumb

Project 10: Light Follow Robot

(1)Description

thumb

We’ve introduce how to use various sensors, modules.
In this lesson, we combine with hardware knowledge -- photoresistor module, motor driving, to build a light-following robot!
Just need to use 2 photoresistor modules to detect the light intensity at the both side of robot. Read the analog value to rotate the 2 motors, thus drive the tank robot run.


The specific logic of light following robot is shown as the table below:

thumb

thumb

We make a flow chart based on the above logic table, as shown below:

(2)Connection Diagram

thumb

Hook-up Attention: The 4Pin terminal block is marked with silkscreen 1234. The red line of right rear motor is connected to terminal 1, black line is linked with end 2. The red line of left front motor is attached to terminal 3, black line is linked with port 4.


thumb


(3)Test Code

  
/*
 keyestudio Mini Tank Robot v2.0
 lesson 10
 Light-following tank
 http://www.keyestudio.com
*/ 
#define light_L_Pin A1   //define the pin of left photo resistor
#define light_R_Pin A2   //define the pin of right photo resistor
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define the PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3   //define the PWM control pin of right motor
int left_light; 
int right_light;
void setup(){
  Serial.begin(9600);
  pinMode(light_L_Pin, INPUT);
  pinMode(light_R_Pin, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}
void loop(){
  left_light = analogRead(light_L_Pin);
  right_light = analogRead(light_R_Pin);
  Serial.print("left_light_value = ");
  Serial.println(left_light);
  Serial.print("right_light_value = ");
  Serial.println(right_light);
  if (left_light > 650 && right_light > 650) //the value detected photo resistor,go front
  {  
    Car_front();
  } 
  else if (left_light > 650 && right_light <= 650)  //the value detected photo resistor,turn left
  {
    Car_left();
  } 
  else if (left_light <= 650 && right_light > 650) //the value detected photo resistor,turn right
  {
    Car_right();
  } 
  else  //other situations, stop
  {
    Car_Stop();
  }
}
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
//****************************************************************

(4)Test Result

Upload code on keyestudio V4.0 development board, DIP switch is dialed to right end and power on, the smart robot follows light to move.

Project 11: Ultrasonic Avoid Tank

thumb

(1)Description

In this program, the ultrasonic sensor detects the distance of obstacle to send signals that control the robot car. Next, let’s show you how to make an obstacle avoidance car.

The specific logic of ultrasonic avoiding robot is as shown below:

thumb

(2)Flow chart

thumb
thumb

(3)Connection Diagram:

thumb

Note: “-”、“+” and “S” pins of servo are respectively attached to G(GND), V(VCC)and D9 of expansion board. The VCC, Trig, Echo and Gnd of ultrasonic sensor are linked with 5v(V), 5(S), Echo and Gnd(G) of expansion board.

(4)Test Code:


/*
 keyestudio Mini Tank Robot v2.0
 lesson 11
 ultrasonic_avoid_tank
 http://www.keyestudio.com
*/
int random2;
int a;
int a1;
int a2;
#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3   //define PWM control pin of right motor

#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance;
#define servoPin 9  //servo Pin
int pulsewidth;
/************the function to run motor**************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}

//The function to control servo
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }
}
//The function to control ultrasonic sensor
float checkdistance() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.00;  //58.20, that is, 2*29.1=58.2
  delay(10);
  return distance;
}
  //****************************************************************
void setup(){
  pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
  
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}
void loop(){
  random2 = random(1, 100);
  a = checkdistance();  //assign the front distance detected by ultrasonic sensor to variable a
  
  if (a < 20) //when the front distance detected is less than 20 
  {
      Car_Stop();  //robot stops
      delay(500); //delay in 500ms
      procedure(160);  //Ultrasonic platform turns left
      for (int j = 1; j <= 10; j = j + (1)) { //for statement, the data will be more accurate if ultrasonic sensor detect a few times.
        a1 = checkdistance();  //assign the left distance detected by ultrasonic sensor to variable a1
      }
      delay(300);
      procedure(20); //Ultrasonic platform turns right
      for (int k = 1; k <= 10; k = k + (1)) {
        a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
      }
      
      if (a1 < 50 || a2 < 50)  //robot will turn to the longer distance side when left or right distance is less than 50cm. 
      {
        if (a1 > a2) //left distance is greater than right side      
        {
          procedure(90);  //Ultrasonic platform turns back to right ahead         
Car_left();  //robot turns left
          delay(500);  //turn left for 500ms
          Car_front(); //go front
        } 
        else 
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front();  //go front
        }
      } 
      else  //If both side is greater than or equal to 50cm, turn left or right randomly
      {
        if ((long) (random2) % (long) (2) == 0)  //When the random number is even
        {
          procedure(90);
          Car_left(); //tank robot turns left
          delay(500);
          Car_front(); //go front
        } 
        else 
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); //go front
       }
     }
  } 
  else  //If the front distance is greater than or equal to 20cm, robot car will go front
  {
      Car_front(); //go front
  }
}
  //****************************************************************


(5)Test Result

Upload code successfully, DIP switch is dialed to right end and power on, tank robot goes forward. It can automatically avoid barrier.

Project 12: Ultrasonic Follow Tank

(1)Description

thumb

In project 11, we made an obstacle avoidance car. In fact, we only need to alter a test code to transform an obstacle avoidance car into following car. In this lesson, we will make an ultrasonic follow robot. The ultrasonic sensor detects the distance between smart car and the obstacle to drive tank car to move.

The specific logic of ultrasonic follow robot is as shown below:

thumb

(2)Flow chart

thumb

(3)Connection Diagram

thumb

Wire-up note:

thumb

(4)Test Code

 /*
 keyestudio Mini Tank Robot v2.0
 lesson 12
 ultrasonic follow tank
 http://www.keyestudio.com
*/ 
//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4

#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3   //define PWM control pin of right motor
#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance;
int pulsewidth;
#define servoPin 9  //servo Pin
void setup(){
  Serial.begin(9600);
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear); //Clear the display
  matrix_display(start01);  //display start pattern
  pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}
void loop(){
  distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
  if (distance >= 20 && distance <= 60) //range to go front
  {
    Car_front();
  }
  else if (distance > 10 && distance < 20)  //range to stop
  {
    Car_Stop();
  }
  else if (distance <= 10)  //range to go back
  {
    Car_back();
  }
  else  //other situations, stop
  {
    Car_Stop();
  }
}
/***********the function for motor running****************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}

/******************dot matrix********************/
// the function for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start(); // call the function that data transmission start
  IIC_send(0xc0);  //Choose address
  
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
     IIC_send(matrix_value[i]); //data to convey patterns
  }

  IIC_end();   //end to convey data pattern
  
  IIC_start();
  IIC_send(0x8A);  //select pulse width4/16, control display
  IIC_end();
}

//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}

// transmit data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      
delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }
}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/***************end dot matrix display******************/
//The function to control servo
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }}
//The function to control ultrasonic sensor function controlling ultrasonic
float checkdistance() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.20;  //58.20, that is , 2*29.1=58.2
  delay(10);
  return distance;
}
//****************************************************************

(5)Test Result

Upload code successfully, DIP switch is dialed to right end, the servo rotates to 90°, “V” is shown on 8X16 LED panel and smart car moves as the obstacle moves.

Project 13: IR Remote Robot Tank

thumb

(1)Description

IR remote control is one of most ubiquitous control, applied in TV, electric fan and some household appliances. In this project, we will make an IR remote smart car. And we’ve known every key value on IR remote control. Thus, we could control smart car via and display the patterns on dot matrix via corresponding key value

The specific logic of infrared remote control robot is shown below:

thumb
thumb


(2)Flow Chart

thumb


(3)Connection Diagram

thumb

Attention:GND,VCC, SDA, SCL of 8x16 LED panel are respectively linked with -(GND), +(VCC), SDA ,SCL. And “-”、“+” and S of IR receiver module are attached to G(GND), V(VCC) and A0 on sensor shield. On the condition of insufficient digital ports, the analog ports can be treat as digital ports. A0 equals to digital 14, A1 is like digital 15.


(4)Test Code


/*
 keyestudio Mini Tank Robot v2.0
 lesson 13
 IR remote tank
 http://www.keyestudio.com
*/

#include <IRremoteTank.h>
IRrecv irrecv(A0);  //set IRrecv irrecv to A0
decode_results results;
long ir_rec;  //save the IR value received

//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4

#define ML_Ctrl 13  //define the direction control pin of left motor
#define ML_PWM 11   //define PWM control pin of left motor
#define MR_Ctrl 12  //define the direction control pin of right motor
#define MR_PWM 3    //define PWM control pin of right motor

#define servoPin 9 //pin of servo
int pulsewidth; //save the pulse width value of servo

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();  //Initialize the IR reception library
  
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
  
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear); //Clear Screen
  matrix_display(start01);  //show start picture
  
  pinMode(servoPin, OUTPUT);
  procedure(90);  //Servo rotates to 90°
}

void loop(){
  if (irrecv.decode(&results)) //receive the IR remote value
  {
    ir_rec=results.value;
    String type="UNKNOWN";
    String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
    if(results.decode_type>=1&&results.decode_type<=13){
      type=typelist[results.decode_type];
    }
    Serial.print("IR TYPE:"+type+"  ");
    Serial.println(ir_rec,HEX);
    irrecv.resume();
  }
  
  if (ir_rec == 0xFF629D) //Go forward
  {
    Car_front();
    matrix_display(front);  //Display front image
  }
  if (ir_rec == 0xFFA857)  //Robot car goes back
  {
    Car_back();
    matrix_display(front);  //Go back
  }
  if (ir_rec == 0xFF22DD)   //Robot car turns left
  {
    Car_T_left();
    matrix_display(left);  //Display left-turning image
  }
  if (ir_rec == 0xFFC23D)   //Robot car turns right
  {
    Car_T_right();
    matrix_display(right);  //Display right-turning image
  }
  if (ir_rec == 0xFF02FD)   //Robot car stops
  { 
    Car_Stop();
    matrix_display(STOP01);  //show stop image
  }
  if (ir_rec == 0xFF30CF)   //robot car rotates anticlockwise
  {
    Car_left();
    matrix_display(left);  //show anticlockwise rotation picture
  }
  if (ir_rec == 0xFF7A85)  //robot car rotates clockwise
  {
    Car_right();
    matrix_display(right);  //show clockwise rotation picture
 }
}
/******************Control Servo*******************/
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }
}

/******************Dot Matrix****************/
// this function is used for dot matrix display 
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();
  IIC_send(0xc0);  //Choose address
   for(int i = 0;i < 16;i++) //The picture has 16 bits
  {
     IIC_send(matrix_value[i]); //data to convey patterns
  }
  IIC_end();   //end to convey data pattern
  
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}

//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//传输数据
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits 8bits for every character
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      
      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }
}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/***************the function to run motor***************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
 //****************************************************************


(5)Test Result


Upload code successfully and power on, the smart robot can be controlled by IR remote, at same time, the corresponding pattern is shown on 8X16 LED panel.

Project 14: Bluetooth Control Robot

thumb


(1)Description

We’ve learned the basic knowledge of Bluetooth, in this lesson, we will make a Bluetooth remote smart car. In the experiment, we default the HM-10 Bluetooth module as a Slave and the cellphone as a Host.
keyes BT car is an APP rolled out by keyestudio team. You could control the robot car by it readily.
There is a guide to how to download and install APP in the document as for your reference. The interface is shown below.

thumb


APP DOWNLOAD

https://play.google.com/store/apps/details?id=com.keyestudio.tankcar3


(2)Test Code


/*
 keyestudio Mini Tank Robot v2.0
 lesson 14.1
 bluetooth test
 http://www.keyestudio.com
*/
char ble_val; //character variables, used to save the value of Bluetooth reception
void setup() {
  Serial.begin(9600);
}
void loop() {
  if(Serial.available() > 0)  //judge if there is data in buffer area
  { ble_val = Serial.read();  //read the data from serial buffer
    Serial.println(ble_val);  //print out
  }}


Pull off the Bluetooth module, upload test code, reconnect Bluetooth module, open serial monitor and set baud rate to 9600. Point at Bluetooth module and press keys on APP, the corresponding character is shown below.


thumb


The detected character and corresponding function:

thumb


In compliance with the above table, we make a flow chart to assist you to understand well. The flow chart is below:

thumb

(3)Connection Diagram

thumb


Wiring Attention:

thumb

(4)Test Code

Note: Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code

/*
 keyestudio Robot Car v2.0
 lesson 14.2
 bluetooth car
 http://www.keyestudio.com
*/

//Array, used to store the data of pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4

#define ML_Ctrl 13  //define direction control pin of left motor
#define ML_PWM 11   //define PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3    //define PWM control pin of right motor

char bluetooth_val; //save the value of Bluetooth reception

void setup(){
  Serial.begin(9600);
  
  pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear);    //Clear the display
  matrix_display(start01);  //display start pattern

  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
}

void loop(){
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    Serial.println(bluetooth_val);
  }
  switch (bluetooth_val) 
  {
     case 'F':  //forward command
        Car_front();
        matrix_display(front);  // show forward design
        break;
     case 'B':  //Back command
        Car_back();
        matrix_display(back);  //show back pattern
        break;
     case 'L':  // left-turning instruction
        Car_left();
        matrix_display(left);  //show “left-turning” sign 
        break;
     case 'R':  //right-turning instruction
        Car_right();
        matrix_display(right);  //display right-turning sign
       break;
     case 'S':  //stop command
        Car_Stop();
        matrix_display(STOP01);  //show stop picture
        break;
  }
}

/**************The function of dot matrix****************/
//this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();
  IIC_send(0xc0);  //Choose address
  
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
     IIC_send(matrix_value[i]); //data to convey patterns
  }
  IIC_end();   //end to convey data pattern
  
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//transmit data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //Each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA      
      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // Detect bit by bit, so move the data right by one
  }
}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/*************the function to run motor**************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
  //****************************************************************

(5)Test Result

Upload code successfully, DIP switch is dialed to right end and power on. After connecting Bluetooth, we could drive smart car to move by Bluetooth App.

thumb

Project 15: Multi-purpose Robot Car

thumb


(1)Description

In previous projects, the tank car only performs single function, however, in this lesson, we integrate all of function to control smart car via Bluetooth control. Here is a simple flow chart of multi-purpose robot car as for your reference.

thumb

(2)Connection Diagram

thumb

Attention:Confirm that every component is connected.

Wire-up Guide:

thumb

thumb

thumb

thumb


thumb

thumb

thumb



(3)Test Code

    /*
 keyestudio Mini Tank Robot v2.0
 lesson 15
 multiple functions
 http://www.keyestudio.com
*/

//Array, used to store the data of the pattern, can be calculated by yourself or obtained from the modulus tool
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin  A5  //Set clock pin to A5
#define SDA_Pin  A4  //Set data pin to A4

#define ML_Ctrl 13  //define direction control pin of left motor
#define ML_PWM 11   //define PWM control pin of left motor
#define MR_Ctrl 12  //define direction control pin of right motor
#define MR_PWM 3   //define PWM control pin of right motor

#define Trig 5  //ultrasonic trig Pin
#define Echo 4  //ultrasonic echo Pin
int distance; //save the distance value detected by ultrasonic, follow function
int random2; //save the variable of random numberssave the variable of random numbers
//save the distance value detected by ultrasonic, obstacle avoidance function
int a;  
int a1;
int a2;

#define servoPin 9  //servo Pin
int pulsewidth;

#define light_L_Pin A1   //define the pin of left photo resistor sensor
#define light_R_Pin A2   //define the pin of right photo resistor sensor
int left_light;
int right_light;

char bluetooth_val; //save the value of Bluetooth reception
int flag;  //flag variable, it is used to entry and exist function
void setup(){
  Serial.begin(9600);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(ML_Ctrl, OUTPUT);
  pinMode(ML_PWM, OUTPUT);
  pinMode(MR_Ctrl, OUTPUT);
  pinMode(MR_PWM, OUTPUT);
  
  pinMode(servoPin, OUTPUT);
  procedure(90); //set servo to 90°
 pinMode(SCL_Pin,OUTPUT);
  pinMode(SDA_Pin,OUTPUT);
  matrix_display(clear);    //Clear the display
  matrix_display(start01);  //display start pattern
  pinMode(light_L_Pin, INPUT);
  pinMode(light_R_Pin, INPUT);
}

void loop(){
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    Serial.println(bluetooth_val);
  }
  switch (bluetooth_val) 
  {
    case 'F':  //Forward instruction 
      Car_front();
      matrix_display(front);  //display forward pattern
      break;
    case 'B':  //Back instruction
      Car_back();
      matrix_display(back);  // display back pattern
      break;
    case 'L':  //left-turning instruction
      Car_left();
      matrix_display(left);  //show left-turning pattern
      break;
    case 'R':  //right-turning instruction
      Car_right();
      matrix_display(right);  //show right-turning pattern
      break;
    case 'S':  //stop instruction
      Car_Stop();
      matrix_display(STOP01);  //display stop pattern
      break;
   case 'Y':
      matrix_display(start01);  //show start pattern
      follow();
      break;
   case 'U':
      matrix_display(start01);  //show start pattern
      avoid();
      break;
   case 'X':
      matrix_display(start01);  //show start pattern
      light_track();
      break;
  }}
/*****************Obstacle Avoidance Function**************/
void avoid() 
{
  flag = 0;  //the design that enter obstacle avoidance function
  while (flag == 0) 
  {
    random2 = random(1, 100);
    a = checkdistance();  //assign the front distance detected by ultrasonic sensor to variable a
    
    if (a < 20) //when the front distance detected is less than 20cm
    {
      Car_Stop();  //robot stops
      delay(200); //delay in 200ms
      
      procedure(160);  //Ultrasonic platform turns left
      for (int j = 1; j <= 10; j = j + (1)) { ///for statement, the data will be more accurate if ultrasonic sensor detect a few times.
        a1 = checkdistance();  //assign the left distance detected by ultrasonic sensor to variable a1
      }
      delay(200);
      procedure(20); //Ultrasonic platform turns right
      for (int k = 1; k <= 10; k = k + (1)) {
        a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2
      }
      if (a1 < 50 || a2 < 50)  //robot will turn to the longer distance side when left or right distance is less than 50cm.if the left or right distance is less than 50cm, the robot will turn to the greater distance
      {
        if (a1 > a2) //left distance is greater than right
        {
          procedure(90);  //Ultrasonic platform turns back to right ahead ultrasonic platform turns front
          Car_left();  //robot turns left
          delay(500);  //turn left 500ms
          Car_front(); //go forward
        } 
        else 
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front();  //go forward
        }
      } 
      else  //both distance on two side is greater than or equal to 50cm, turn randomly
      {
        if ((long) (random2) % (long) (2) == 0)  //when the random number is even
        {
          procedure(90);
          Car_left(); //robot turns left
          delay(500);
          Car_front(); //go forward
        } 
        else 
        {
          procedure(90);
          Car_right(); //robot turns right
          delay(500);
          Car_front(); ///go forward
       } } } 
  else  //If the front distance is greater than or equal to 20cm, robot car will go front
  {
      Car_front(); //go forward
  }
  // receive the Bluetooth value to end the obstacle avoidance function
  if (Serial.available())
  {
    bluetooth_val = Serial.read();
    if (bluetooth_val == 'S')  //receive S
    {
      flag = 1;  //when assign 1 to flag, end loop
    }}}}
/*******************Follow****************/
void follow() {
  flag = 0;
  while (flag == 0) {
    distance = checkdistance();  //assign the distance detected by ultrasonic sensor to distance
    if (distance >= 20 && distance <= 60) //the range to go front
    {
      Car_front();
    }
    else if (distance > 10 && distance < 20)  //the range to stop
    {
      Car_Stop();
    }
    else if (distance <= 10)  // the range to go back
    {
      Car_back();
    }
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      if (bluetooth_val == 'S') 
      {
        flag = 1;  //end loop
      }}}}
//The function to control ultrasonic sensor the function controlling ultrasonic sensor
float checkdistance() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  float distance = pulseIn(Echo, HIGH) / 58.00;  //58.20 means 2*29.1=58.2
  delay(10);
  return distance;
}
//The function to control servo the function controlling servo
void procedure(int myangle) {
  for (int i = 0; i <= 50; i = i + (1)) {
    pulsewidth = myangle * 11 + 500;
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin,LOW);
    delay((20 - pulsewidth / 1000));
  }}

/****************Light Follow******************/
void light_track() {
  flag = 0;
  while (flag == 0) {
    left_light = analogRead(light_L_Pin);
    right_light = analogRead(light_R_Pin);
    if (left_light > 650 && right_light > 650) //the value detected by photo resistor, go forward
    {  
      Car_front();
    } 
    else if (left_light > 650 && right_light <= 650)  //the value detected by photo resistor, turn left
    {
      Car_left();
    } 
    else if (left_light <= 650 && right_light > 650) //the value detected by photo resistor, turn right
    {
      Car_right();
    } 
    else  //other situations, stop
    {
      Car_Stop();
    }
    if (Serial.available())
    {
      bluetooth_val = Serial.read();
      if (bluetooth_val == 'S') {
        flag = 1;
     }}}}
/***************Dot Matrix *****************/
// this function is used for dot matrix display 
void matrix_display(unsigned char matrix_value[])
{
  IIC_start();
  IIC_send(0xc0);  //Choose address
  
  for(int i = 0;i < 16;i++) //pattern data has 16 bits
  {
     IIC_send(matrix_value[i]); //convey the pattern data
  }
  IIC_end();   //end the transmission of pattern data
  IIC_start();
  IIC_send(0x8A);  //display control, set pulse width to 4/16
  IIC_end();
}
//The condition starting to transmit data
void IIC_start()
{
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
}
//convey data
void IIC_send(unsigned char send_data)
{
  for(char i = 0;i < 8;i++)  //each byte has 8 bits
  {
      digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA
      delayMicroseconds(3);
      if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
      {
        digitalWrite(SDA_Pin,HIGH);
      }
      else
      {
        digitalWrite(SDA_Pin,LOW);
      }
      delayMicroseconds(3);
      digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
      delayMicroseconds(3);
      send_data = send_data >> 1;  // detect bit by bit, so move the data right by one
  }}
//The sign that data transmission ends
void IIC_end()
{
  digitalWrite(SCL_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,LOW);
  delayMicroseconds(3);
  digitalWrite(SCL_Pin,HIGH);
  delayMicroseconds(3);
  digitalWrite(SDA_Pin,HIGH);
  delayMicroseconds(3);
}
/*************the function to run motor*************/
void Car_front()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,200);
}
void Car_back()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,200);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,200);
}
void Car_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,HIGH);
  analogWrite(ML_PWM,255);
}
void Car_right()
{
  digitalWrite(MR_Ctrl,HIGH);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}
void Car_Stop()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,0);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,0);
}
void Car_T_left()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,255);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,180);
}
void Car_T_right()
{
  digitalWrite(MR_Ctrl,LOW);
  analogWrite(MR_PWM,180);
  digitalWrite(ML_Ctrl,LOW);
  analogWrite(ML_PWM,255);
}


(4)Test Result

Note:Remove the Bluetooth module before uploading test code, otherwise, you will fail to upload test code. Reconnect Bluetooth module after uploading test code

Upload test code successfully, insert Bluetooth module, power on, and connect to Bluetooth. The tank robot can show distinct function by App.

Alright, the whole projects are finished. Please feel free to contact us if you confront some problems. There are some hot-selling items below. You could log in our website to browse them if you’re interested.

9. Resources

Wiki page:https://wiki.keyestudio.com/Ks0428_keyestudio_Mini_Tank_Robot_V2

Official website: https://keyestudio.com/
Assembly Video Link: http://video.keyestudio.com/ks0428/

If you want to try with Mixly software, enter https://fs.keyestudio.com/KS0428.