Guide Arduino pour Robot 4WD Keyestudio
Guide Arduino pour Robot 4WD Keyestudio
dernier
»
Tutoriel Arduino
Modifier sur GitHub
Tutoriel Arduino
Démarrer avec Arduino
Microcontrôleur ATmega328P-PU
Tension de fonctionnement 5V
Tension d'entrée
DC7-12V
(recommandée)
LED_INTEGREE D13
Projets
1. Description
Pour les débutants et les passionnés, LED Blink est un programme fondamental.
LED, l'abréviation de diodes électroluminescentes, se compose de composés
chimiques Ga, As, P, N, etc.
2. Spécification
Interface de contrôle : port numérique
Tension de fonctionnement : DC 3,3-5 V
Espacement des broches : 2,54 mm
Couleur de l'affichage LED : rouge
3. Composants
Carte de développement Keyestudio Blindage moteur Keyestudio 8833 Module LED
4.0 *1 *1 rouge*1
4. Schéma de câblage
Comme on peut le voir sur la figure ci-dessus, le blindage moteur Keyestudio 8833
est empilé sur la carte de développement Keyestudio 4.0.
5. Code de test
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 1.1
Blink
[Link]
*/
void setup()
{
pinMode(9, OUTPUT);// initialize digital pin 9 as an output.
}
void loop() // the loop function runs over and over again forever
{
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(9, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
//****************************************************************************
6. Résultat du test
Après avoir téléchargé avec succès le code sur la carte V4.0, connectez les câbles
conformément au schéma de câblage et utilisez un câble USB pour connecter
l'ordinateur afin d'alimenter la carte. Après la mise sous tension, vous verrez que la
LED connectée au D9 s'allumera et s'éteindra.
7. Explication du code
pinMode(9,OUTPUT) - Cette fonction peut indiquer que la broche est INPUT ou
OUTPUT
8. Pratique de l'extension
Nous avons réussi à faire clignoter la LED. Observons maintenant ce qui va arriver à
la LED si nous modifions le temps de retard.
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 1.2
delay
[Link]
*/
void setup()
{
// initialize digital pin 11 as an output.
pinMode(9, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for 0.1 second
digitalWrite(9, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 0.1 second
}
//*****************************************************************
The test result shows that the LED flashes faster. Therefore, the delaying time
enables to affect the flash frequency of the LED.
1. Description
In previous lesson, we control LED on and off and make it blink.
In this project, we will control LED’s brightness through PWM simulating 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 voltages of ports are 0V and 5V.
What if the 3V is required? Or a switch among 1V, 3V and 3.5V? We cannot change
resistors constantly. For this reason, we resort to PWM.
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 1s.
If all of the output five hundred are 1, that is 5V; if all of which are 0, 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 see it, neither does PWM. If we
want different voltage, we need to control the ratio of 0 and 1. The more 0,1 signals
output per unit time, the more accurate the control.
PWM is a technology that uses digital methods to obtain analog quantities. Digital
control enables to form a square wave, the square wave signal only has on and off
two states (high and low). A voltage ranging from 0 to 5V can be simulated by
controlling the ratio of on to off duration. The time spent on (technically called high
level) is called pulse width, so PWM is also called pulse width modulation.
The green vertical bars represent one period of the square wave. The value written
in each analogWrite(value) corresponds to a percentage, which is also called Duty
Cycle. This percentage refers to the ratio of time occupied by the high level in a
cycle, that is, duty cycle = high level time/cycle time.
In the figure, from top to bottom, the duty cycle of the first square wave is 0%, and
the corresponding value is 0, and the LED brightness is the lowest, that is, off
state. The longer the high level lasts, the brighter it will be. Therefore, the value of
the last duty cycle of 100% is 255, and the LED is the brightest. 50% is the brightest
half, and 25% is darker.
PWM is more used to adjust the brightness of LED lights or the rotation speed of the
motors, and the wheels speed driven by the motors can be easily controlled. When
playing with some Arduino robots, the benefits of PWM can be better reflected.
2. Components
Keyestudio 4.0 Development Board Keyestudio 8833 Motor Red LED
*1 Shield *1 Module*1
3. Wiring Diagram
Keep the wiring-up unchanged.
4. Test Code
//*****************************************************************
/*
keyestudio 4wd BT Car
lesson 2.1
pwm
[Link]
*/
int ledPin = 9; // Define the LED pin at D9
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
}
}
//*****************************************************************
5. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, and use a USB cable to connect the computer to
power the board. After powering on, you will see that the LED gradually changes
from bright to dark, like human’s breath, rather than turning on and off immediately.
6. Code Explanation
If we need to repeat a certain statement, we could use for statement.
Round 1:1 → 2 → 3 → 4
Round 2:2 → 3 → 4
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.
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 can 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 learn more about the PWM.
In the above figure, the green line represents a period, and the value of analogWrite()
corresponds to a percentage which is called Duty Cycle as well. Duty cycle implies
the ratio of time occupied by the high level in the 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, light off. The more time the high level lasts, the
brighter the LED. Therefore, the last duty cycle is 100%, which corresponds to 255,
and LED is the brightest. And 50% is the brightest half, 25% means darker.
PWM is more used for adjusting the LED’s brightness or the rotation speed of
motors.
It plays a vital role in controlling smart robot cars. I believe that you cannot wait to
learn the next project.
7. Extension Practice
Let’s modify the value of delay and remain the pin unchanged, then observe how the
LED changes.
//***********************************************************
/*
keyestudio 4wd BT Car
lesson 2.2
pwm
[Link]
*/
int ledPin = 9; // Define the LED pin at D9
void setup () {
pinMode(ledPin, OUTPUT); // initialize ledpin as an output.
}
void loop () {
for (int value = 0; value <255; value = value + 1) {
analogWrite (ledPin, value); // LED lights gradually light up
delay (30); // delay 30MS
}
for (int value = 255; value> 0; value = value-1) {
analogWrite (ledPin, value); // LED gradually goes out
delay (30); // delay 30MS
}
}
//***********************************************************
Upload the code to the development board, then the LED will blink more slowly.
1. Description
The tracking sensor is actually an infrared sensor. The component used here is the
TCRT5000 infrared tube. Its working principle is to use different reflectivity of infrared
light to colors, then convert the strength of the reflected signal into a current signal.
During the process of detection, black is active at HIGH level while white is active at
LOW level. The detection height is 0-3 cm.
Keyestudio 3-channel line tracking module has integrated 3 sets of TCRT5000
infrared tubes on a board, which is more convenient for wiring and control.
By rotating the adjustable potentiometer on the sensor, it can adjust the detection
sensitivity of the sensor.
2. Specification
Operating Voltage: 3.3-5V (DC)
Interface: 5PIN
Output Signal: Digital signal
Detection Height: 0-3 cm
Note: Before testing, rotate the potentiometer on the sensor to adjust the detection
sensitivity. The sensitivity is best when adjusting the LED to a threshold between ON
and OFF.
3. Components
Keyestudio 4.0 Keyestudio 8833 Motor Red LED Keyestudio Line
Development Board *1 Shield *1 Module*1 Tracking Sensor*1
4. Wiring Diagram
G, V, S1, S2 and S3 of the line tracking sensor are connected to G(GND), V(VCC),
D11, D7 and D8 of the sensor expansion board.
5. Test Code
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 3.1
Line Track sensor
[Link]
*/
int L_pin = 11; //pins of left line tracking sensor
int M_pin = 7; //pins of middle line tracking sensor
int R_pin = 8; //pins of right line tracking sensor
int val_L,val_R,val_M;// define the variable value of three sensors
void setup()
{
[Link](9600); // initialize serial communication at 9600 bits per second
pinMode(L_pin,INPUT); // make the L_pin as an input
pinMode(M_pin,INPUT); // make the M_pin as an input
pinMode(R_pin,INPUT); // make the R_pin as an input
}
void loop()
{
val_L = digitalRead(L_pin);//read the L_pin:
val_R = digitalRead(R_pin);//read the R_pin:
val_M = digitalRead(M_pin);//read the M_pin:
[Link]("left:");
[Link](val_L);
[Link](" middle:");
[Link](val_M);
[Link](" right:");
[Link](val_R);
delay(500);// delay in between reads for stability
}
//****************************************************************************
6. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, and use a USB cable to connect the computer to
power the board.
After powering on, open the serial monitor and you will view status of three line
tracking sensors. When no signals are received, the value is 1. If we cover the
sensor with a white paper, the value will be 0.
7. Code Explanation
[Link](9600) - Initialize serial port, set baud rate to 9600
digitalRead -Read the state of pin, which are generally HIGH and LOW level
8. Extension Practice
After knowing its working principle, you can connect an LED to D9 so as to control
LED by it.
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 3.2
Line Track Sensor LED
[Link]
*/
int L_pin = 11; //pins of left line tracking sensor
int M_pin = 7; //pins of middle line tracking sensor
int R_pin = 8; //pins of right line tracking sensor
int val_L,val_R,val_M;// define the variables of three sensors
void setup()
{
[Link](9600); // initialize serial communication at 9600 bits per second
pinMode(L_pin,INPUT); // make the L_pin as an input
pinMode(M_pin,INPUT); // make the M_pin as an input
pinMode(R_pin,INPUT); // make the R_pin as an input
pinMode(9, OUTPUT);
}
void loop()
{
val_L = digitalRead(L_pin);//read the L_pin:
val_R = digitalRead(R_pin);//read the R_pin:
val_M = digitalRead(M_pin);//read the M_pin:
[Link]("left:");
[Link](val_L);
[Link](" middle:");
[Link](val_M);
[Link](" right:");
[Link](val_R);
delay(500);// delay in between reads for stability
if ((val_L == LOW) || (val_M == LOW) || (val_R == LOW))//if left line tracking
sensor detects signals
{
[Link]("HIGH");
digitalWrite(9, HIGH);//LED is off
}
else//if left line tracking sensor doesn’t detect signals
{
[Link]("LOW");
digitalWrite(9, LOW);//LED is off
}
}
//****************************************************************************
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, and use a USB cable to connect the computer to
power the board.
After powering on, make a paper close to the sensor, then we can find the LED light
up when covering the line tracking sensor.
Project 4 Servo Control
1. Description
In general, servo has three lines in brown, red and orange. The brown wire is
grounded, the red one is a positive pole line and the orange one is a signal line.
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 motors, the same signal may have different rotation angles.
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)
4. Wiring Diagram
Wiring note: The servo is connected to G(GND)、V(VCC)and A3, the brown line of
servo is linked with Gnd(G), the red one is connected to 5v(V) and the orange one is
attached to A3.
The servo is obliged to connect to the external power due to its high demand for
driving servo current. Generally, the current of development board is not big enough.
If without connecting the external power, the development board could be burnt.
5. Test Code
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 4.1
Servo
[Link]
*/
#define servoPin A3 //servo Pin
int pos; //the angle variable of servo
int pulsewidth; //pulse width variable of servo
void setup() {
pinMode(servoPin, OUTPUT); //set the pins of servo to output
procedure(0); //set the angle of servo to 0 degree
}
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);
}
}
6. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, and power on the external power. After powering on,
turn the dip switch to the “ON” end, then servo will swing in the range of 0° to 180°.
7. Extension Practice
What’s more, we empower to control the servo via library file. Please refer to the
link: [Link]
//***************************************************************************
/*
keyestudio 4wd BT Car
lesson 4.2
Servo
[Link]
*/
#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() {
[Link](A3); // attaches the servo on pin A3 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
[Link](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
[Link](pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15ms for the servo to reach the
position
}
}
//***************************************************************************
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, and power on the external power. After powering on,
turn the dip switch to the “ON” end, then servo will swing in the range of 0° to 180°
too. We usually control it by library file.
8. Code Explanation
Arduino comes with #include <Servo.h> (servo function and statement)
2). write(angle)——Used to set rotation angle of servo, and the set angle range is
from 0° to 180°
3). read()——used to read angle of servo, namely, reading the command value
of“write()”
Note: The above written format is“servo variable name, specific statement()”, for
instance: [Link](9).
1. Description
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like
what 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 an 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 an ultrasonic sensor and how to use the ultrasonic sensor
with Arduino.
2. Specification
Working Voltage :+5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Distance Range : 2cm – 300 cm
Precision : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse width: 10uS
3. Components
Keyestudio 4.0 Keyestudio 8833 Motor Red LED HC-SR04 Ultrasonic
Development Board *1 Shield *1 Module*1 Sensor*1
4. Working Principle
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 triggering a 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 the object from the time difference
between the trigger signal and the 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, and distance = speed * time. However, the
ultrasonic wave emits and comes back, which is 2 times of distance. Therefore, it
needs to be divided by 2, the distance measured by ultrasonic wave = (speed *
time)/2.
1. Use the GPIO pin to give a high level signal of at least 10μs to the Trig pin of
SR04, 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.

Circuit diagram of ultrasonic sensor:
5. Wiring Diagram
VCC, Trig, Echo and Gnd of the ultrasonic sensor are connected to 5V(V), D12, D13
and Gnd(G)
6. Test Code
//***************************************************************************
/*
keyestudio 4wd BT Car
lesson 5.1
Ultrasonic Sensor
[Link]
*/
int trigPin = 12; // Trigger
int echoPin = 13; // Echo
long duration, cm, inches;
void setup() {
//Serial Port begin
[Link] (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
[Link](inches);
[Link]("in, ");
[Link](cm);
[Link]("cm");
[Link]();
delay(250);
}
//***************************************************************************
7. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, open the serial monitor and set baud rate to
9600.
The detected distance will be displayed, and the unit is cm and inch. Hinder the
ultrasonic sensor by hand, the displayed distance value gets smaller.
8. Code Explanation
int trigPin- this pin is defined to transmit ultrasonic waves,generally output.
cm = (duration/2) / 29.1-
We need to divide the traveltime by 2 for 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 and connect an LED light
module to the D9 pin.
//*****************************************************************
/*
keyestudio 4wd BT Car
lesson 5.2
Ultrasonic LED
[Link]
*/
int trigPin = 12; // Trigger
int echoPin = 13; // Echo
long duration, cm, inches;
void setup() {
[Link] (9600); //Serial Port begin
pinMode(trigPin, OUTPUT); //Define inputs and outputs
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
[Link](inches);
[Link]("in, ");
[Link](cm);
[Link]("cm");
[Link]();
delay(250);
if (cm>=2 && cm<=10)
{
[Link]("HIGH");
digitalWrite(9, HIGH);
}
else
{
[Link]("LOW");
digitalWrite(9, LOW);
}
}
//*****************************************************************
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, block the ultrasonic sensor by hand(the distance
is between 2-10cm), then check if the 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 0 or 1 signal and the encoding is made up of
these 0, 1 signals.
The user code of the same remote control is constant while 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.
2. Specification
Operating Voltage: 3.3-5V(DC)
Output Signal: Digital signal
Receiving Angle: 90 degrees
Frequency: 38khz
Receiving Distance: 10m
The picture shows the real product and circuit diagram of the infrared receiver.
3. Components
Keyestudio 4.0 Development Board Keyestudio 8833 Motor Red LED
*1 Shield *1 Module*1
Since the 8833 board integrates with the IR receiver, it doesn’t need wiring up. Pins
of IR receiver module are G(GND), V(VCC)and D3.
4. Test Code
//
***********************************************************************************
**
/*
keyestudio 4wd BT Car
lesson 6.1
IR remote
[Link]
*/
#include <IRremote.h> //IRremote library statement
int RECV_PIN = 3; //define the pins of IR receiver as D3
IRrecv irrecv(RECV_PIN);
decode_results results; // decode results exist in the“result” of “decode
results”
void setup()
{
[Link](9600);
[Link](); // Enable receiver
}
void loop() {
if ([Link](&results))//decode successfully, receive a set of infrared
signals
{
[Link]([Link], HEX);//Wrap word in 16 HEX to output and receive
code
[Link](); // Receive the next value
}
delay(100);
}
//
***********************************************************************************
**
5. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, open the serial monitor and set baud rate to
9600.
Take out the remote control, and send signal to the infrared receiver sensor. You can
see the key value of the corresponding key, if the key time is too long, FFFFFFFF is
prone to garbled characters.
6. Code Explanation
[Link](): After enabling IR decoding, the IR signals will be received,
7. Extension Practice
We have decoded the key value of the IR remote control. How about controlling LED
by the measured value? We could design an experiment.
Attach an LED to D9, then press the keys of remote control to make LED light on and
off.
//
***********************************************************************************
**
/*
keyestudio 4wd BT Car
lesson 6.2
IR remote LED
[Link]
*/
#include <IRremote.h>
int RECV_PIN = 3;//define the pin of IR receiver as D3
int LED_PIN = 9;//define the pin of LED as pin 9
int a=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{[Link](9600);
[Link](); //Initialize the IR receiver
pinMode(LED_PIN,OUTPUT);//set pin 9 of LED to OUTPUT
}
void loop() {
if ([Link](&results))
{
if([Link]==0xFF02FD && (a==0)) //according to the above key value,
press“OK”on remote control , LED will be controlled
{
[Link]("HIGH");
digitalWrite(LED_PIN,HIGH);//LED will be on
a=1;
}
else if([Link]==0xFF02FD && (a==1)) //press again
{
[Link]("LOW");
digitalWrite(LED_PIN,LOW);//LED will go off
a=0;
}
[Link](); // receive the next value
}
}
//
***********************************************************************************
**
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, press the “OK” key on remote control can make
the LED on and off.
Project 7 Bluetooth Remote Control
1. Description
There is a DX-BT24 5.1 Bluetooth module in this kit. This bluetooth module comes
with 256Kb space and complies with V5.1BLE bluetooth specification, which supports
AT commands. Users can change parameters such as the baud rate and device
name of the serial port as required.
2. Specification
Bluetooth protocol: Bluetooth Specification V5.1 BLE
Working distance: In an open environment, it can achieve 40m ultra-long distance
communication
Operating frequency: 2.4GHz ISM band
Communication interface: UART
Bluetooth certification: Accord with FCC CE ROHS REACH certification standard
Serial port parameters: 9600, 8 data bits, 1 stop bit, invalid bit, no flow control
Power: 5V DC
Operating temperature: –10℃ to +65℃
3. Application
The DX-BT24 module also supports the BT5.1 BLE protocol, which can be directly
connected to iOS devices with BLE Bluetooth function, and supports resident running
of background programs. It is mainly used in the field of short-distance data wireless
transmission. It enables to avoid cumbersome cable connections and can directly
replace serial cables.
※ Bluetooth printer;
※ Shared bicycles;
Ports
①STATE:Status pin
②RX:Receiving pin
③TX:sending pin
④GND:GND
⑤VCC:Power
Uno BT2
4
TX RX
RX TX
VCC 5V
GN GND
D
4. Components
Keyestudio 4.0 Development Keyestudio 8833 Motor Red LED Module*1
Board *1 Shield *1
5. Wiring Diagram
RXD, TXD, GND and VCC of the BT module are connected to TX, RX, G and 5V.
Note: the direction of the BT module when inserting it onto the 8833 board. And don’t
insert it before uploading the code.
6. Test Code
Note: Before uploading the test code, you need to remove the Bluetooth module,
otherwise the code will fail to be [Link] the Bluetooth module after
uploading the code successfully.
//***********************************************************************
/*
keyestudio 4wd BT Car
lesson 7.1
Bluetooth
[Link]
*/
char ble_val; //character variable, used to store the value received by Bluetooth
void setup() {
[Link](9600);
}
void loop() {
if([Link]() > 0) //make sure if there is data in serial buffer
{
ble_val = [Link](); //Read data from serial buffer
[Link](ble_val); //Print
}
}
//***********************************************************************
7. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, insert the BT module and the LED will flash,
then we need to download the BT app.
4. Click “Connect” button in the upper left corner to automatically search for
Bluetooth. When BT24 is found, click “Connect” to connect Bluetooth, and then
Android System
6. After connecting Bluetooth module, open serial monitor to set baud rate to 9600.
Pressing the button of the Bluetooth APP, and the corresponding characters will
be displayed, as shown below:
Key Function
Pair DX-BT24 5.1
Bluetooth module
Disconnect Bluetooth
Press: F Press the button, the car goes front; release to stop
Release: S
Press: L Press the button, the car turns left; release to stop
Release: S
Press: R Press the button, the car turns right; release to stop
Release: S
Press: B Press the button, the car goes back; release to stop
Release: S
Click to send“X”, click Start line tracking function; click again to exit
again to send“S”
Click to send“Y”, click Start ultrasonic avoiding function; click again to exit
again to send“S”
Click to send“U”, click Start ultrasonic follow function; click again to exit
again to send“S”
Click to send“G”, click Start restricting function; click again to exit
again to send“S”
9. Code Explanation
[Link]() : Return the number of characters currently remaining in the serial
port buffer. Generally, this function is used to judge whether there is data in the buffer
of the serial port. When [Link]()>0, it means that the serial port has received
data and can be read;
[Link]() : Refers to taking out and reading a Byte of data from the serial port
buffer. For example, if a device sends data to Arduino through the serial port, we can
use [Link]() to read the sent data.
//****************************************************************************
/*
keyestudio smart turtle robot
lesson 7.2
Bluetooth LED
[Link]
*/
int ledpin=9;
char ble_val;// An integer variable used to store the value received by Bluetooth
void setup()
{
[Link](9600);
pinMode(ledpin,OUTPUT);
}
void loop()
{
if ([Link]() > 0) //Check whether there is data in the serial port
cache
{
ble_val = [Link](); //Read data from the serial port cache
[Link]("DATA RECEIVED:");
[Link](ble_val);
if (ble_val == 'F') {
digitalWrite(ledpin, HIGH);
[Link]("led on");
}
if (ble_val == 'B') {
digitalWrite(ledpin, LOW);
[Link]("led off");
}
}
}
//****************************************************************************
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then connect the computer via a USB cable to
power the board. After powering on, click and to control the LED turn on
and turn off.
1. Description
There are many ways to drive motors. Our car uses the most commonly used
DRV8833 motor driver chip, which provides a two-channel bridge electric drive
solution for toys, printers and other integrated motor applications.
When we stack the Shield on the 4.0 development board and power on the BAT,
then set the DIP switch to the ON end, the external power supply will power on the
two boards at the same time. To facilitate wiring connections, the Shield comes with
an anti-reverse port (PH2.0-2P-3P-4P-5P). You can connect the motors, power
supply, and sensor modules directly to the Shield.
The Bluetooth interface of the Shield is fully compatible with the DX-BT24 5.1
Bluetooth module. When connecting the Bluetooth module, you solely need to plug it
into the corresponding interface. At the same time, 2.54 row pins are used to draw
out some unused digital and analog ports on the Shield, making it accessible for you
to add other sensors and carry out extension experiments.
The expansion board can be connected to four DC motors. When the jumper cap is
connected by default, the motors of ports A and A1 and B and B1 are connected in
parallel and have the same motion law. 8 jumper caps can be used to control the
rotation direction of the 4 motor interfaces.
For example, when the 2 jumper caps in front of B1 of the M1 motor change from
transverse connection to longitudinal connection, the rotation direction of M1 motor
will be opposite to the original rotation direction.
2. Specification
Input voltage for logic:DC 5V
Input voltage for driving:DC 6-9 V
Working current for logic:<36mA
Working current for driving:<2A
Maximum power dissipation:25W(T=75℃)
Input level for control signal: high level is 2.3V<Vin<5V ,low level is -
0.3V<Vin<1.5V
Working temperature:-25+130℃
3. Keyestudio 8833 motor Shield
Working Principle
We use the same side parallel connection mode for the four motors, which can be
regarded as two groups of motors. As shown in the wiring diagram, B and B1 are a
group, and A and A1 are a group.
The motors in the same group should rotate in the same direction. If they are
different, please adjust the corresponding jumper caps next to the terminal to change
the direction.
As shown below, if the directions of A and A1 are different, adjust the direction of
jumper caps until the motor movement direction of the same group is consistent.
From the above diagram, it is known that the direction pin of A motor is D4, the
speed pin is D6; D2 is the direction pin of B motor; and D6 is speed pin.
PWM drives the robot car. The PWM value is in the range of 0-255. When we set the
direction to HIGH, the smaller the PWM number, the faster the rotation of the motor.
5. Wiring Diagram
6. Test Code
//****************************************************************************
/*
keyestudio 4wd BT Car
lesson 8.1
Motor driver shield
[Link]
*/
#define ML_Ctrl 2 //define the direction control pins of group B motor
#define ML_PWM 5 //define the PWM control pins of group B motor
#define MR_Ctrl 4 //define the direction control pins of group A motor
#define MR_PWM 6 //define the PWM control pins of group A motor
void setup()
{
pinMode(ML_Ctrl, OUTPUT);//set direction control pins of group B motor to output
pinMode(ML_PWM, OUTPUT);//set PWM control pins of group B motor to output
pinMode(MR_Ctrl, OUTPUT);//set direction control pins of group A motor to output
pinMode(MR_PWM, OUTPUT);//set PWM control pins of group A motor to output
}
void loop()
{
//front
digitalWrite(ML_Ctrl,HIGH);//set the direction control pins of group B motor to
HIGH
analogWrite(ML_PWM,55);//set the PWM control speed of group B motor to 55
digitalWrite(MR_Ctrl,HIGH);//set the direction control pins of group A motor to
HIGH
analogWrite(MR_PWM,55);// set the PWM control speed of group A motor to 55
delay(2000);//delay in 2000ms
//back
digitalWrite(ML_Ctrl,LOW);//set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,200);// set the PWM control speed of group B motor to 200
digitalWrite(MR_Ctrl,LOW);//set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,200);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//left
digitalWrite(ML_Ctrl,LOW);//set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,200);//set the PWM control speed of group B motor to 200
digitalWrite(MR_Ctrl,HIGH);//set the direction control pins of group A motor to
HIGH level
analogWrite(MR_PWM,55);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//right
digitalWrite(ML_Ctrl,HIGH);//set the direction control pins of group B motor to
HIGH level
analogWrite(ML_PWM,55);//set the PWM control speed of group B motor to 55
digitalWrite(MR_Ctrl,LOW);// set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,200);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//stop
digitalWrite(ML_Ctrl, LOW);// set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,0);//set the PWM control speed of group B motor to 0
digitalWrite(MR_Ctrl, LOW);// set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,0);//set the PWM control speed of group A motor to 0
delay(2000);// delay in 2000ms
}
//****************************************************************************
7. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then power on the external power and turn the DIP
switch to ON, the car will go forward for 2s, back for 2s, turn left for 2s and right for 2s
and stop for 2s
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 digital pins.
//************************************************************************
/*
keyestudio 4wd BT Car
lesson 8.2
Motor driver
[Link]
*/
#define ML_Ctrl 2 //define the direction control pins of group B motor
#define ML_PWM 5 //define the PWM control pins of group B motor
#define MR_Ctrl 4 //define the direction control pins of group A motor
#define MR_PWM 6 //define the PWM control pins of group A motor
void setup()
{
pinMode(ML_Ctrl, OUTPUT);//set direction control pins of group B motor to output
pinMode(ML_PWM, OUTPUT);//set PWM control pins of group B motor to output
pinMode(MR_Ctrl, OUTPUT);//set direction control pins of group A motor to output
pinMode(MR_PWM, OUTPUT);//set PWM control pins of group A motor to output
}
void loop()
{
//front
digitalWrite(ML_Ctrl,HIGH);//set the direction control pins of group B motor to
HIGH
analogWrite(ML_PWM,105);//set the PWM control speed of group B motor to 55
digitalWrite(MR_Ctrl,HIGH);//set the direction control pins of group A motor to
HIGH
analogWrite(MR_PWM,105);// set the PWM control speed of group A motor to 55
delay(2000);//delay in 2000ms
//back
digitalWrite(ML_Ctrl,LOW);//set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,150);// set the PWM control speed of group B motor to 200
digitalWrite(MR_Ctrl,LOW);//set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,150);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//left
digitalWrite(ML_Ctrl,LOW);//set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,150);//set the PWM control speed of group B motor to 200
digitalWrite(MR_Ctrl,HIGH);//set the direction control pins of group A motor to
HIGH level
analogWrite(MR_PWM,105);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//right
digitalWrite(ML_Ctrl,HIGH);//set the direction control pins of group B motor to
HIGH level
analogWrite(ML_PWM,105);//set the PWM control speed of group B motor to 55
digitalWrite(MR_Ctrl,LOW);// set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,150);//set the PWM control speed of group A motor to 200
delay(2000);//delay in 2000ms
//stop
digitalWrite(ML_Ctrl, LOW);// set the direction control pins of group B motor to
LOW level
analogWrite(ML_PWM,0);//set the PWM control speed of group B motor to 0
digitalWrite(MR_Ctrl, LOW);// set the direction control pins of group A motor to
LOW level
analogWrite(MR_PWM,0);//set the PWM control speed of group A motor to 0
delay(2000);// delay in 2000ms
}
//************************************************************************
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then power on the external power and turn the DIP
switch to ON, then you find the speed of the motor is much slower.
1. Description
How fun it is if a expression board is added to the robot. And the Keyestudio 8*16
LED board can do the trick. With the help of it, you could design facial expressions,
images, patterns and other displays by yourselves.
The 8*16 LED board comes with 128 LEDs. The data of the microprocessor(Arduino)
communicates with the AiP1640 through a two-wire bus interface. Therefore, it can
control the on and off of 128 LEDs on the module, so as to make the dot matrix on
the module to display the pattern you need. A HX-2.54 4Pin cable is provided for
your convenience of wiring.
2. Specification
Working voltage: DC 3.3-5V
Power loss: 400mW
Oscillation frequency: 450KHz
Drive current: 200mA
Working temperature: -40~80℃
Communication mode: I2C
3. Circuit Diagram
4. Working Principle
How to control each LED of the 8*16 dot matrix? It is known that each byte has 8 bits
and each bit is 0 or 1. when it is 0, LED is off while when it is 1 LED is on. One byte
can control one column of the LED,and naturally 16 bytes can control 16 columns of
LEDs, that’s the 8*16 dot matrix.
①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.
③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.
④The requirement for data input is that when SCL is at high level when inputting
data, the signal on SDA must remain unchanged. Only when the clock signal on SCL
is at low level, can the signal on SDA be changed. The input of data is the low bit
first, and the high bit later.
⑤The condition for the end of data transmission is that when SCL is at low level,
SDA at low level and SCL at high level, the level of SDA becomes high.
⑥Display control, set different pulse width, pulse width can be selected as shown in
the figure below.
In the example, the pulse width is 4/16, and the hexadecimal corresponding to 1000
1010 is 0x8A.
The dot matrix tool uses the online version, and the link is : [Link]
②The dot matrix is 8*16, so adjust the height to 8 and width to 16, as shown in the
figure below.
As shown in the figure below, press the left mouse button to select, right click to
cancel; draw the pattern you want, click Generate, and the hexadecimal data we
need will be generated.
6. Components
Keyestudio 4.0 Development Keyestudio 8833 Motor Keyestudio 8x16 LED
Board *1 Shield *1 Panel*1
7. Wiring Diagram
The GND, VCC, SDA, and SCL of the 8x16 LED light board are respectively
connected to the keyestudio sensor expansion board-(GND), + (VCC), A4, A5 for
two-wire serial communication.
(Note: Though it is connected with the IIC pin of Arduino, this module is not for IIC
communication. And the IO port here is to simulate I2C communication and can be
connected with any two pins ).
9. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, then turn the DIP switch to ON, a smile-shaped
pattern will be displayed on the LED board.
0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x
00
0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x
00
0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x
00
0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x
00
0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,
0x00
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x
00
//************************************************************************
/*
keyestudio 4wd BT Car
lesson 9.2
Matrix face
[Link]
*/
//Data from the smile pattern obtained from the touch 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 the 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
//matrix_display(clear);
}
void loop() {
matrix_display(start01); //Show start pattern
delay(2000);
matrix_display(front); //Show forward pattern
delay(2000);
matrix_display(STOP01); //Show stop pattern
delay(2000);
matrix_display(clear); //Clear Screen
delay(2000);
}
//this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[])
{
IIC_start(); //the function that calls the data transfer start condition
IIC_send(0xc0); //select address
After uploading test code, the facial expression board shows these patterns orderly
and repeats this sequence.
1. Description
In this project, we look to combine the knowledge of a line tracking sensor and motor
driver modules to make a restricting smart car. In the experiment, we aim to use the
line tracking sensor to detect whether there is a black line around the smart car, and
then control the rotation of the two motors according to the detection results in a way
that lock the smart car in a circle drawn in black line.
2. Flow Chart
The specific logic of the restricting 4WD smart car is shown in the table.
3. Wiring Diagram
G, V, S1, S2 and S3 of the line tracking sensor are connected to G(GND), V(VCC),
D11, D7 and D8 of the sensor expansion board.
4. Test Code
//*************************************************************************
/*
keyestudio 4wd BT Car
lesson 10
Restricting Smart Car
[Link]
*/
//Data from the smile pattern obtained from the touch tool
unsigned char start01[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x80,
0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
#define SDA_Pin A4 //Set data pin to A4
#define SCL_Pin A5 //Set the clock pin to A5
void setup() {
[Link](9600);//start serial monitor and set baud rate to 9600
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
pinMode(sensor_L,INPUT);//set the pins of left line tracking sensor to INPUT
pinMode(sensor_M,INPUT);//set the pins of middle line tracking sensor to INPUT
pinMode(sensor_R,INPUT);//set the pins of right line tracking sensor to INPUT
//Set pin to output
pinMode(SCL_Pin, OUTPUT);
pinMode(SDA_Pin, OUTPUT);
matrix_display(start01);//Show start pattern
}
void loop()
{
tracking(); //run main program
}
void tracking()
{
L_val = digitalRead(sensor_L);//read the value of left line tracking sensor
M_val = digitalRead(sensor_M);//read the value of middle line tracking sensor
R_val = digitalRead(sensor_R);//read the value of right line tracking sensor
if ( L_val == 0 && M_val == 0 && R_val == 0 ) { //when no black lines are
detected,turtle car forward
Car_front();
}
else { //Otherwise, if any of the patrol sensors detect a black line, back up and
turn left
Car_back();
delay(700);
Car_left();
delay(700);
}
}
void Car_front()
{
digitalWrite(left_ctrl, HIGH);
analogWrite(left_pwm, 155);
digitalWrite(right_ctrl, HIGH);
analogWrite(right_pwm, 155);
}
void Car_back()
{
digitalWrite(left_ctrl, LOW);
analogWrite(left_pwm, 100);
digitalWrite(right_ctrl, LOW);
analogWrite(right_pwm, 100);
}
void Car_left()
{
digitalWrite(left_ctrl, LOW);
analogWrite(left_pwm, 100);
digitalWrite(right_ctrl, HIGH);
analogWrite(right_pwm, 155);
}
void Car_right()
{
digitalWrite(left_ctrl, HIGH);
analogWrite(left_pwm, 155);
digitalWrite(right_ctrl, LOW);
analogWrite(right_pwm, 100);
}
void Car_Stop()
{
digitalWrite(left_ctrl, LOW);
analogWrite(left_pwm, 0);
digitalWrite(right_ctrl, LOW);
analogWrite(right_pwm, 0);
}
5. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, power on the external power then turn the DIP
switch to ON. Put the smart car in the black circle, then it will move solely in the
circle.
1. Description
Based on the working principle of the line tracking sensor, we empower to make a
line tracking smart car.
In this project, we detect whether there is a black line at the bottom of the smart car
through a line tracking sensor, and then control the rotation of the two groups of
motors according to the detection results in a way that controls the smart car to walk
along the black line.
2. Flow Chart
3. Wiring Diagram
G, V, S1, S2 and S3 of the line tracking sensor are connected to G(GND), V(VCC),
D11, D7 and D8 of the sensor expansion board.
4. Test Code
//*************************************************************************
/*
keyestudio 4wd BT Car
lesson 11
Tracking Car
[Link]
*/
//Data from the smile pattern obtained from the touch tool
unsigned char start01[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x80,
0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
#define SDA_Pin A4 //Set data pin to A4
#define SCL_Pin A5 //Set the clock pin to A5
void setup() {
[Link](9600);//start serial monitor and set baud rate to 9600
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
pinMode(sensor_L,INPUT);//set the pins of left line tracking sensor to INPUT
pinMode(sensor_M,INPUT);//set the pins of middle line tracking sensor to INPUT
pinMode(sensor_R,INPUT);//set the pins of right line tracking sensor to INPUT
//Set pin to output
pinMode(SCL_Pin, OUTPUT);
pinMode(SDA_Pin, OUTPUT);
matrix_display(start01);//Show start pattern
}
void loop()
{
tracking(); //run main program
}
void tracking()
{
L_val = digitalRead(sensor_L);//read the value of left line tracking sensor
M_val = digitalRead(sensor_M);//read the value of middle line tracking sensor
R_val = digitalRead(sensor_R);//read the value of right line tracking sensor
if(M_val == 1){//if the state of middle one is 1, which means detecting black
line
if (L_val == 1 && R_val == 0) { //If a black line is detected on the left, but
not on the right, turn left
left();
}
else if (L_val == 0 && R_val == 1) { //Otherwise, if a black line is detected
on the right and not on the left, turn right
right();
}
else { //Otherwise,forward
front();
}
}
else { //No black lines detected in the middle
if (L_val == 1 && R_val == 0) { //If a black line is detected on the left, but
not on the right, turn left
left();
}
else if (L_val == 0 && R_val == 1) { //Otherwise, if a black line is detected
on the right and not on the left, turn right
right();
}
else { //Otherwise,stop
Stop();
}
}
}
void front()//define the status of going forward
{
digitalWrite(left_ctrl,HIGH);
analogWrite(left_pwm,155);
digitalWrite(right_ctrl,HIGH);
analogWrite(right_pwm,155);
}
void back()//define the state of going back
{
digitalWrite(left_ctrl,LOW);
analogWrite(left_pwm,100);
digitalWrite(right_ctrl,LOW);
analogWrite(right_pwm,100);
}
void left()//define the left-turning state
{
digitalWrite(left_ctrl, LOW);
analogWrite(left_pwm, 100);
digitalWrite(right_ctrl, HIGH);
analogWrite(right_pwm, 155);
}
void right()//define the right-turning state
{
digitalWrite(left_ctrl, HIGH);
analogWrite(left_pwm, 155);
digitalWrite(right_ctrl, LOW);
analogWrite(right_pwm, 100);
}
void Stop()//define the state of stop
{
digitalWrite(left_ctrl, LOW);
analogWrite(left_pwm,0);
digitalWrite(right_ctrl, LOW);
analogWrite(right_pwm,0);
}
5. Test Resul
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, power on the external power then turn the DIP
switch to ON. Then the smart car will walk along the lines.
1. Description
In this project, we will look to detect the distance between the 4WD smart car and the
obstacles ahead through an ultrasonic sensor to drive two motors in a way that make
the car move and make the 8*8 LED board show a smile facial pattern.
2. Flow Chart
1). GND, VCC, SDA and SCL of the 8*8 LED board are connected to G(GND),
V(VCC), A4 and A5 of the expansion board.
2). VCC, Trig, Echo and Gnd of the ultrasonic sensor are connected to 5V(V),
D12(S), D13(S) and Gnd(G)
3). The servo is connected to G, V and A3. The brown wire is interfaced with Gnd(G),
the red wire is interfaced with 5V(V) and the orange wire is interfaced with A3.
4. Test Code
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 12
Flowing Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
void setup() {
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
pinMode(TRIG_PIN, OUTPUT); //Set the trig pin to output
pinMode(ECHO_PIN, INPUT); //Set the echo pin to input
pinMode(SCL_Pin,OUTPUT);//Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
servopulse(servopin,90);//Set the initial steering gear angle to 90°
delay(500); //waits 500ms
matrix_display(smile); //display smiling expression pattern
}
void loop() {
distance = [Link]();//the distance detected by ultrasonic sensor
if(distance <= 10)//if distance is less than 10
{
back();//go back
}
else if((distance > 10)&&(distance< 20 ))//if 10<distance<20
{
Stop();//stop
}
else if((distance >= 20)&&(distance <= 50))//if 20≤distance<50
{
front();//follow
}
else//otherwise
{
Stop();//stop
}
}
1. Description
In this project, we aim to make an ultrasonic obstacle avoidance smart car. We will
use the ultrasonic to detect the distance from the obstacle, which can be used to
control the servo to rotate so as to make the car move. Meanwhile, the 8X16 LED
board will display the corresponding status pattern.
2. Flow Chart
The specific logic of ultrasonic obstacle avoidance smart car is shown below:
3. Wiring Diagram
1). GND, VCC, SDA and SCL of the 8*8 LED board module are connected to
G(GND), V(VCC), A4 and A5 of the expansion board.
2). VCC, Trig, Echo and Gnd of the ultrasonic sensor are connected to 5V(V),
D12(S), D13(S) and Gnd(G)
3). The servo is connected to G, V and A3. The brown wire is interfaced with Gnd(G),
the red wire is interfaced with 5V(V) and the orange wire is interfaced with A3.
4). The power is connected to the BAT port
4. Test Code
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 13
Avoiding Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
//Array, used to store the data of pattern, can be calculated by yourself or
obtained from the modulus tool
unsigned char front[] =
{0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,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};
void setup() {
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
pinMode(TRIG_PIN, OUTPUT); //Set the trig pin to output
pinMode(ECHO_PIN, INPUT); //Set the echo pin to input
servopulse(servopin,90);//the angle of servo is 90 degree
delay(300);
pinMode(SCL_Pin,OUTPUT);// Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
matrix_display(clear);
}
void loop()
{
avoid();//run the main program
}
void avoid()
{
distance=[Link](); //obtain the value detected by ultrasonic sensor
if((distance < 20)&&(distance != 0))//if the distance is greater than 0 and less
than 10
{
car_Stop();//stop
matrix_display(clear);
matrix_display(STOP01);//show stop pattern
delay(1000);
servopulse(servopin,160);//servo rotates to 160°
delay(500);
a1=[Link]();//measure the distance
delay(100);
servopulse(servopin,20);//rotate to 20 degree
delay(500);
a2=[Link]();//measure the distance
delay(100);
servopulse(servopin,90); //Return to the 90 degree position
delay(500);
if(a1 > a2)//compare the distance, if left distance is more than right distance
{
car_left();//turn left
matrix_display(clear);
matrix_display(left); //display left-turning pattern
servopulse(servopin,90);//servo rotates to 90 degree
delay(700); //turn left 700ms
matrix_display(clear);
matrix_display(front); //show forward pattern
}
else//if the right distance is greater than the left
{
car_right();//turn right
matrix_display(clear);
matrix_display(right); //display right-turning pattern
servopulse(servopin,90);//servo rotates to 90 degree
delay(700);
matrix_display(clear);
matrix_display(front); //show forward pattern
}
}
else//otherwise
{
car_front();//go forward
matrix_display(clear);
matrix_display(front); // show forward pattern
}
}
5. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, power on the external power then turn the DIP
switch to ON.
The smart car moves forward and automatically avoids obstacles. When there is no
road ahead, the servo will drive the ultrasonic sensor to scan the left, middle and
right distances, and the car will turn to the open side. Meanwhile, the 8X16 LED
board will display the corresponding status pattern.
1. Description
In this project, we will make an IR remote control smart car and press the button on
the IR remote control to drive the car to move.
2. Flow Chart
leftward icon
1). GND, VCC, SDA and SCL of the 8*8 LED board module are connected to
G(GND), V(VCC), A4 and A5 of the expansion board.
2). As the IR receiver is integrated on the 8833 motor Shield, there is no need for
additional wiring. The pins of the IR receiver on the 8833 board are G (GND), V
(VCC) and D3 respectively.
3). The servo is connected to G, V and A3. The brown wire is interfaced with Gnd(G),
the red wire is interfaced with 5V(V) and the orange wire is interfaced with A3.
4. Test Code
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 14
IR remote Control Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
//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};
#include <Arduino.h>
#include <IRremote.h>//function library of IR remote control
int RECV_PIN = 3;//set the pin of IR receiver to D3
IRrecv irrecv(RECV_PIN);
long irr_val;
decode_results results;
#include <Servo.h>
Servo servo_A3;//set the pin of servo to A3
void setup() {
[Link](9600);//
// In case the interrupt driver crashes on setup, give a clue
// to the user what's going on.
[Link]("Enabling IRin");
[Link](); // Start the receiver
[Link]("Enabled IRin");
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
servo_A3.attach(A3);
servo_A3.write(90);//the angle of servo is 90 degree
delay(300);
pinMode(SCL_Pin,OUTPUT);// Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
matrix_display(clear);
matrix_display(start01); //display start01 expression pattern
}
void loop()
{
if ([Link](&results))
{
irr_val = [Link];
[Link](irr_val, HEX);//serial prints the read IR remote signals
switch(irr_val)
{
case 0xFF629D : car_front(); //Receive 0xFF629D,the car goes forward
matrix_display(clear);
matrix_display(front);
break;
5. Test Result
After successfully uploading the code to the V4.0 board, connect the wirings
according to the wiring diagram, power on the external power then turn the DIP
switch to ON. Then we enable to use the IR remote control drive the car to move to
and the 8X16 LED board will display the corresponding status pattern.
Project 15 Bluetooth Control Smart Car
1. Description
We’ve learned the basic knowledge of Bluetooth. And in this lesson, we will make a
Bluetooth control smart car. In this project, we aim to regard the mobile phone as the
transmitter (host), and the smart car connected to the BT24 Bluetooth module (slave)
as the receiver and use the mobile APP to control the smart car via the Bluetooth.
Disconnect Bluetooth
Version : S
Version : S
Version : S
Version : S
Sortie : « S » (minimum:0)
4. Schéma de câblage
1). GND, VCC, SDA et SCL de la carte LED 8*8 sont connectés à G(GND),
V(VCC), A4 et A5 de la carte d'extension.
2). Les RXD, TXD, GND et VCC du module Bluetooth sont respectivement connectés
à TX, RX, G et 5V sur le shield moteur 8833, tandis que les broches STATE et BRK
du module Bluetooth n'ont pas besoin d'être connectées.
3). Le servo est connecté à G, V et A3. Le fil marron est connecté à Gnd (G), le fil
rouge est connecté à 5 V (V) et le fil orange est connecté à A3.
5. Code de test
Remarque : avant de télécharger le code de test, vous devez retirer le module
Bluetooth, sinon le téléchargement du code échouera. Connectez le module
Bluetooth après avoir téléchargé le code avec succès.
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 15
Bluetooth Control Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
//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};
char BLE_val;
void setup() {
[Link](9600);//
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
servopulse(servopin,90);//the angle of servo is 90 degree
delay(300);
pinMode(SCL_Pin,OUTPUT);// Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
matrix_display(clear);
matrix_display(start01); //display start01 expression pattern
}
void loop() {
if([Link]()>0) {
BLE_val = [Link]();
[Link](BLE_val);
}
switch(BLE_val)
{
case 'F' : car_front(); //Receive 'F',the car goes forward
matrix_display(clear);
matrix_display(front);
break;
1. Description
Dans ce projet, nous utiliserons un Bluetooth pour ajuster la vitesse de la voiture
intelligente. Nous pouvons définir une vitesse variable et la modifier pour modifier la
vitesse de la voiture intelligente.
2. Organigramme
3. Schéma de câblage
1). GND, VCC, SDA et SCL de la carte LED 8*8 sont connectés à G(GND),
V(VCC), A4 et A5 de la carte d'extension.
2). Les RXD, TXD, GND et VCC du module Bluetooth sont respectivement connectés
à TX, RX, G et 5V sur le shield moteur 8833, tandis que les broches STATE et BRK
du module Bluetooth n'ont pas besoin d'être connectées.
3). Le servo est connecté à G, V et A3. Le fil marron est connecté à Gnd (G), le fil
rouge est connecté à 5 V (V) et le fil orange est connecté à A3.
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 16
Bluetooth Speed Control Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
//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};
unsigned char speed_a[] =
{0x00,0x40,0x20,0x10,0x08,0x04,0x02,0xff,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00};
unsigned char speed_d[] =
{0x00,0x02,0x04,0x08,0x10,0x20,0x40,0xff,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00};
char BLE_val;
void setup() {
[Link](9600);//
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
servopulse(servopin,90);//the angle of servo is 90 degree
delay(300);
pinMode(SCL_Pin,OUTPUT);// Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
matrix_display(clear);
matrix_display(start01); //display start01 expression pattern
}
void loop() {
if([Link]()>0) {
BLE_val = [Link]();
[Link](BLE_val);
}
switch(BLE_val)
{
case 'F' : car_front();
matrix_display(clear);
matrix_display(front);
break;
5. Résultat du test
Après avoir téléchargé avec succès le code sur la carte V4.0, connectez les câbles
conformément au schéma de câblage, mettez l'alimentation externe sous tension,
puis placez le commutateur DIP sur ON. En associant l'application au Bluetooth, la
voiture intelligente peut être contrôlée pour se déplacer par l'application.
1. Description
Dans les projets précédents, la voiture n'exécutait qu'une seule fonction. Cependant,
dans cette leçon, nous allons intégrer toutes ses fonctions via Bluetooth.
2. Organigramme
3. Schéma de câblage
1). GND, VCC, SDA et SCL de la carte LED 8*8 sont connectés à G(GND),
V(VCC), A4 et A5 de la carte d'extension.
2). Les RXD, TXD, GND et VCC du module Bluetooth sont respectivement connectés
à TX, RX, G et 5V sur le shield moteur 8833, tandis que les broches STATE et BRK
du module Bluetooth n'ont pas besoin d'être connectées.
3). Le servo est connecté à G, V et A3. Le fil marron est connecté à Gnd (G), le fil
rouge est connecté à 5 V (V) et le fil orange est connecté à A3.
5). VCC, Trig, Echo et Gnd du capteur à ultrasons sont connectés à 5V(V), D12(S),
D13(S) et Gnd(G).
4. Code de test
Remarque : avant de télécharger le code de test, vous devez retirer le module
Bluetooth, sinon le téléchargement du code échouera. Connectez le module
Bluetooth après avoir téléchargé le code avec succès.
//*******************************************************************************
/*
keyestudio 4wd BT Car
lesson 17
Bluetooth Multifunctional Car
[Link]
*/
#define SCL_Pin A5 //Set the clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
//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};
unsigned char speed_a[] =
{0x00,0x40,0x20,0x10,0x08,0x04,0x02,0xff,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00};
unsigned char speed_d[] =
{0x00,0x02,0x04,0x08,0x10,0x20,0x40,0xff,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00};
int L_pin = 11; //define the left tracking sensor pin as D11
int M_pin = 7; //define the middle tracking sensor pin as D7
int R_pin = 8; //define the right tracking sensor pin as D8
int L_val, M_val, R_val;
char BLE_val;
void setup() {
[Link](9600);//Set baud rate to 9600
pinMode(left_ctrl,OUTPUT);//set direction control pins of group B motor to OUTPUT
pinMode(left_pwm,OUTPUT);//set PWM control pins of group B motor to OUTPUT
pinMode(right_ctrl,OUTPUT);//set direction control pins of group A motor to
OUTPUT
pinMode(right_pwm,OUTPUT);//set PWM control pins of group A motor to OUTPUT
servopulse(servopin,90);//the angle of servo is 90 degree
delay(300);
pinMode(L_pin, INPUT); //Tracking sensor pins are configured for input mode
pinMode(M_pin, INPUT);
pinMode(R_pin, INPUT);
pinMode(trigPin, OUTPUT); //define TRIG as the output mode
pinMode(echoPin, INPUT); //define ECHO as the input mode
pinMode(SCL_Pin,OUTPUT);// Set the clock pin to output
pinMode(SDA_Pin,OUTPUT);//Set the data pin to output
matrix_display(clear);
matrix_display(start01); //display start01 expression pattern
}
void loop() {
if([Link]()>0) {
BLE_val = [Link]();
[Link](BLE_val);
}
switch(BLE_val)
{
case 'F' : car_front();
matrix_display(clear);
matrix_display(front);
break;
int get_distance() {
int distance = 0;
digitalWrite(trigPin, LOW); // send pulse through Trig/Pin, trigger HC-SR04
ranging, so that send out ultrasonic signal interface low level 2μs
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // make ultrasonic signal interface high level
10μs, here is at least 10μs
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // keep the ultrasonic signal interface low level
distance = pulseIn(echoPin, HIGH) / 58; // read the pulse time and convert the
pulse time to the distance (unit: cm)
[Link](distance); //output distance value
return distance;
}
void follow() {
servopulse(servopin,90);
delay(200);
int follow_flag = 1;
while (follow_flag) {
distance = get_distance(); //call the ranging function
if (distance < 8 ) {//If the distance is less than 8
car_back();//the car goes back
matrix_display(clear);
matrix_display(back);
}
else if (distance >= 8 && distance < 13) { //If the distance is greater than or
equal to 8, it's less than 13
car_Stop();//stop
matrix_display(clear);
matrix_display(STOP01);
}
else if (distance >= 13 && distance <= 35 ) { //If the distance is greater than
or equal to 13, it's less than 35
car_front();//the car goes forward
matrix_display(clear);
matrix_display(front);
}
else {//If none of the above
car_Stop();//stop
matrix_display(clear);
matrix_display(STOP01);
}
BLE_val = [Link]();
if (BLE_val == 'S') { //When S is received, the car stops
follow_flag = 0;
car_Stop();
}
}
}
void avoid() {
int avoid_flag = 1;
while (avoid_flag) {
distance = get_distance(); //Call the ranging function
if (distance > 0 && distance < 20) { //If the distance is less than 20 and
greater than 0
car_Stop();//stops
matrix_display(clear);
matrix_display(STOP01); //the dot matrix displays a stop pattern
delay(1000);
servopulse(servopin,160); //bring the steering gear over 180 degrees
delay(500);
distance_l = get_distance(); //gets the left distance
delay(100);
servopulse(servopin,20); //turn the steering gear to 0 degrees
delay(500);
distance_r = get_distance(); //get the right distance
delay(100);
if (distance_l > distance_r) { //compare the distance, if the left is bigger
than the right
car_left(); //the car turns left
matrix_display(clear);
matrix_display(left); //the dot matrix shows a left pattern
servopulse(servopin,90);//the steering gear returns to 90 degrees
delay(700);
matrix_display(clear);
matrix_display(front); //the dot matrix displays a forward pattern
}
else { //Otherwise if the right is bigger than the left
car_right();//the car turns right
matrix_display(clear);
matrix_display(right); //the dot matrix shows a left pattern
servopulse(servopin,90);//the steering gear returns to 90 degrees
delay(700);
matrix_display(clear);
matrix_display(front); //the dot matrix displays a forward pattern
}
}
else { //When the front distance is less than or equal to 10cm
car_front();//the car goes forward
matrix_display(clear);
matrix_display(front); //the dot matrix displays a forward pattern
}
BLE_val = [Link]();
if (BLE_val == 'S') {//When S is received, the car stops
avoid_flag = 0;
car_Stop();
}
}
}
void confinement() {
int confinement_flag = 1;
while (confinement_flag) {
L_val = digitalRead(L_pin); //read the value of the left sensor
M_val = digitalRead(M_pin); //read the value of the middle sensor
R_val = digitalRead(R_pin); //read the value of the right sensor
if ( L_val == 0 && M_val == 0 && R_val == 0 ) { //the car goes forward when no
black line is detected
car_front();
}
else { //Otherwise, if any of the tracking sensors detect a black line, the
goes back and then turns left
car_back();
delay(500);
car_left();
delay(800);
}
BLE_val = [Link]();
if (BLE_val == 'S') { //When S is received, the car stops
confinement_flag = 0;
car_Stop();
}
}
}
void tracking() {
int track_flag = 1;
while (track_flag) {
L_val = digitalRead(L_pin); //read the value of the left sensor
M_val = digitalRead(M_pin); //read the value of the middle sensor
R_val = digitalRead(R_pin); //read the value of the right sensor
if (M_val == 1) { //Black line detected in the middle
if (L_val == 1 && R_val == 0) { //If a black line is detected on the left,
but not on the right, turn left
car_left();
}
else if (L_val == 0 && R_val == 1) { //Otherwise, if a black line is detected
on the right and not on the left, turn right
car_right();
}
else { //Otherwise, the car goes forward
car_front();
}
}
else { //no black lines detected in the middle
if (L_val == 1 && R_val == 0) { //If a black line is detected on the left,
but not on the right, turn left
car_right();
}
else if (L_val == 0 && R_val == 1) { //Otherwise, if a black line is detected
on the right and not on the left, turn right
car_right();;
}
else { //Otherwise, stop
car_Stop();
}
}
BLE_val = [Link]();
if (BLE_val == 'S') { //When S is received, the car stops
track_flag = 0;
car_Stop();
}
}
}
5. Résultat du test
Après avoir téléchargé avec succès le code sur la carte V4.0, connectez les câbles
selon le schéma de câblage, mettez l'alimentation externe sous tension, puis mettez
le commutateur DIP sur ON.
PrécédentSuivant