YahBoom BatCar Owner's manual

0
2019
Yahboom Arduino Batmobile
Arduino IDE Programming Tutorials

2
1- Go ahead
Advance
The purpose of the experiment:
The purpose of the experiment in this lesson is to open the power switch of the
BatCar after the program is uploaded, and the BatCar starts to move forward after 2
seconds of stationary.
List of components required for the experiment:
BatCar*1
USB data cable*1
Experimental code analysis:
int Left_motor_back = 9;
int Left_motor_go = 5;
int Right_motor_go = 6;
int Right_motor_back = 10;
int Right_motor_en = 8;
int Left_motor_en = 7;
void setup()
{
//Initialize motor drive for output mode
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
}
void run(int time) // go ahead
{
digitalWrite(Left_motor_en,HIGH); // set left motor enable
digitalWrite(Right_motor_en,HIGH); // set right motor enable
digitalWrite(Right_motor_go,HIGH); // right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);//PWM--Pulse Width Modulation(0~255). It can be
adjusted to control speed.
digitalWrite(Left_motor_go,HIGH); // set left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);//PWM--Pulse Width Modulation(0~255).It can be
adjusted to control speed.
delay(time * 100); //Running time can be adjusted
}

3
void loop()
{
delay(2000); //delay 2s
run(10); //Run
}
Experimental steps:
1. We need to open the code of this experiment: advance.ino, click“√”under the menu
bar to compile the code, and wait for the word "Done compiling " in the lower right
corner, as shown in the figure below.
2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】---
selecting the port that the serial number displayed by the device manager just now, as
shown in the figure below.

4
3.Click [Tools]---[Board]---Select Arduino Uno as shown below.
3. After the selection is completed, you need to click “→”under the menu bar to upload
the code to the Arduino UNO board. When the word “Done uploading” appears in the
lower left corner, the code has been successfully uploaded to the Arduino UNO board,
as shown in the figure below.

5
4. After the program is successfully uploaded, as shown in Figure 1, press the power
switch, you can see that the BatCar starts moving forward after 2 seconds .
Figure 1
Figure 2

6
2- Button start
The purpose of the experiment:
After uploading the Button start program, turn on the power switch and press the start
button K1. After a short whistle, the BatCar starts to move forward, backward, left turn,
right turn, rotate left, rotate right.
List of components required for the experiment:
BatCar *1
USB cable *1
Experimental code analysis:
int Left_motor_back = 9;
int Left_motor_go = 5;
int Right_motor_go = 6;
int Right_motor_back = 10;
int Right_motor_en = 8;
int Left_motor_en = 7;
/*Set Button port*/
int key=4;
/*Set BUZZER port*/
int beep=3;
void setup()
{
//Initialize motor drive for output mode
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
pinMode(key,INPUT);// Set button as input
pinMode(beep,OUTPUT); // Set buzzer as output
digitalWrite(key,HIGH);//Initialize button
digitalWrite(beep,HIGH);// set buzzer mute
}
void run(int time) // run
{
digitalWrite(Right_motor_go,HIGH); // right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);//PWM--Pulse Width Modulation(0~255). It can be
adjusted to control speed.
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // set left motor go ahead

7
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);//PWM--Pulse Width Modulation(0~255). It can be
adjusted to control speed.
analogWrite(Left_motor_back,0);
delay(time * 100); //Running time can be adjusted
}
void brake(int time) //stop
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
delay(time * 100);
}
void left(int time) //turn left
{
digitalWrite(Right_motor_go,HIGH);// right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);// PWM--Pulse Width Modulation(0~255) control
speed
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW); // left motor stop
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void spin_left(int time) //Left rotation
{
digitalWrite(Right_motor_go,HIGH);// right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);// PWM--Pulse Width Modulation(0~255) control
speed
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW); // left motor back off
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);// PWM--Pulse Width Modulation(0~255) control
speed
delay(time * 100);
}
void right(int time) //turn right
{
digitalWrite(Right_motor_go,LOW); // right motor stop
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);// left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);// PWM--Pulse Width Modulation(0~255) control
speed
analogWrite(Left_motor_back,0);

8
delay(time * 100);
}
void spin_right(int time) //Right rotation
{
digitalWrite(Right_motor_go,LOW); // right motor back off
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);// PWM--Pulse Width Modulation(0~255) control
speed
digitalWrite(Left_motor_go,HIGH);// left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);// PWM--Pulse Width Modulation(0~255) control
speed
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void back(int time) //back off
{
digitalWrite(Right_motor_go,LOW); //right motor back off
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);// PWM--Pulse Width Modulation(0~255) control
speed
digitalWrite(Left_motor_go,LOW); //left motor back off
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,150);// PWM--Pulse Width Modulation(0~255) control
speed
delay(time * 100);
}
void keysacn()
{
int val;
val=digitalRead(key);// Reads the button ,the level value assigns to val
while(digitalRead(key))// When the button is not pressed
{
val=digitalRead(key);
}
while(!digitalRead(key))// When the button is pressed
{
delay(10); //delay 10ms
val=digitalRead(key);// Reads the button ,the level value assigns to val
if(val==LOW) //Double check the button is pressed
{
digitalWrite(beep,LOW);//The buzzer sounds
delay(100);//delay 100ms
while(!digitalRead(key)) //Determine if the button is released or not
digitalWrite(beep,HIGH);//mute
}
else
digitalWrite(beep,HIGH);//mute
}

9
}
void loop()
{
delay(2000); //delay 2s
keysacn();
back(10); //back off for 1s
brake(5); //stop for 0.5s
run(10);//go ahead for 1s
brake(5); //stop for 0.5s
left(10);//turn left for 1s
right(10);//turn right for 1s
spin_left(20);//Left rotation for 2s
spin_right(20);//Right rotation for 2s
brake(5); //stop for 0.5s
}
Experimental steps:
1. We need to open the code of this experiment: Button_start.ino, click“√”under the
menu bar to compile the code, and wait for the word "Done compiling " in the lower
right corner, as shown in the figure below.
2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】---
selecting the port that the serial number displayed by the device manager just now, as
shown in the figure below.

10
3. After the selection is completed, you need to click “→”under the menu bar to upload
the code to the Arduino UNO board. When the word “Done uploading” appears in the
lower left corner, the code has been successfully uploaded to the Arduino UNO board,
as shown in the figure below.

11
4. Unplug the USB cable, put the BatCar in an open place, turn on the power switch, and
the BatCar is still at rest until the start button K1 is pressed. After a short whistle,
the BatCar starts to move forward and backward. Turn left, turn right, etc.

12
3- Line Walking
The purpose of the experiment:
After uploading the program, unplug the USB data cable, put the BatCar on the patrol
tarck.Adjust the potentionmeterSW3 and SW4 so that the photoelectric sensor can
recognize the black line.Press the start button K1, the BatCar starts running along the
black line.
List of components required for the experiment:
BatCar*1
USB data cable*1
Patrol tarck*1
Experimental code analysis:
int Left_motor_back = 9;
int Left_motor_go = 5;
int Right_motor_go = 6;
int Right_motor_back = 10;
int Right_motor_en = 8;
int Left_motor_en = 7;
/*Set Button port*/
int key=4;
/*Set BUZZER port*/
int beep=3;
/*Line Walking*/
const int SensorRight = A3; // Set Right Line Walking Infrared sensor port
const int SensorLeft = A2; // Set Left Line Walking Infrared sensor port
int SL; // State of Left Line Walking Infrared sensor
int SR; // State of Right Line Walking Infrared sensor
void setup()
{
//Initialize motor drive for output mode
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
pinMode(key,INPUT);// Set button as input
pinMode(beep,OUTPUT);// Set buzzer as output
pinMode(SensorRight, INPUT); // Set Right Line Walking Infrared sensor as input
pinMode(SensorLeft, INPUT); // Set left Line Walking Infrared sensor as input

13
digitalWrite(key,HIGH);//Initialize button
digitalWrite(beep,HIGH);// set buzzer mute
}
//=======================Motor=========================
void run()
{
digitalWrite(Right_motor_go,HIGH);// right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,100);//PWM--Pulse Width Modulation(0~255). It can be
adjusted to control speed.
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);// set left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,100);//PWM--Pulse Width Modulation(0~255). It can be
adjusted to control speed.
analogWrite(Left_motor_back,0);
}
void brake() //stop
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
}
void left()//turn left
{
digitalWrite(Right_motor_go,HIGH); // right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,100);
analogWrite(Right_motor_back,0);// PWM--Pulse Width Modulation(0~255) control
speed
digitalWrite(Left_motor_go,LOW); // left motor stop
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control
speed
}
void spin_left(int time) //Left rotation
{
digitalWrite(Right_motor_go,HIGH); // right motor go ahead
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,100); // PWM--Pulse Width Modulation(0~255) control
speed
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW); // left motor back off
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,100); // PWM--Pulse Width Modulation(0~255) control
speed
delay(time * 100);
}
void right() //turn right

14
{
digitalWrite(Right_motor_go,LOW); // right motor stop
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);// left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,100);
analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control
speed
}
void spin_right(int time) //Right rotation
{
digitalWrite(Right_motor_go,LOW); // right motor back off
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);// PWM--Pulse Width Modulation(0~255) control
speed
digitalWrite(Left_motor_go,HIGH);// left motor go ahead
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);// PWM--Pulse Width Modulation(0~255) control
speed
delay(time * 100);
}
void back(int time) //back off
{
digitalWrite(Right_motor_go,LOW); //right motor back off
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);// PWM--Pulse Width Modulation(0~255) control
speed
digitalWrite(Left_motor_go,LOW); //left motor back off
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,150);// PWM--Pulse Width Modulation(0~255) control
speed
delay(time * 100);
}
//==========================================================
void keysacn()
{
int val;
val=digitalRead(key);// Reads the button ,the level value assigns to val
while(digitalRead(key))// When the button is not pressed
{
val=digitalRead(key);
}
while(!digitalRead(key))// When the button is pressed
{
delay(10); //delay 10ms
val=digitalRead(key);// Reads the button ,the level value assigns to val

15
if(val==LOW) //Double check the button is pressed
{
digitalWrite(beep,LOW);//The buzzer sounds
delay(50);//delay 50ms
while(!digitalRead(key)) //Determine if the button is released or not
digitalWrite(beep,HIGH);//mute
}
else
digitalWrite(beep,HIGH);//mute
}
}
/*main loop*/
void loop()
{
keysacn();//Press the button to start
while(1)
{
/**************************************************************************************
Infrared signal back means white undersurface ,returns low level and led lights up.
Infrared signal gone means black undersurface ,returns high level and led lights off.
**************************************************************************************/
SR = digitalRead(SensorRight);//Right Line Walking Infrared sensor against white
undersurface,then LED[L2] light illuminates and while against black
undersurface,LED[L2] goes off
SL = digitalRead(SensorLeft);//Left Line Walking Infrared sensor against white
undersurface,then LED[L3] light illuminates and while against black
undersurface,LED[L3] goes off
if (SL ==LOW&&SR== LOW)// Black lines were not detected at the same time
run(); // go ahead
else if (SL == LOW & SR == HIGH)// Left sensor against white undersurface and right
against black undersurface , the car left off track and need to adjust to the right.
right();
else if (SR == LOW & SL == HIGH) // Rihgt sensor against white undersurface and left
against black undersurface , the car right off track and need to adjust to the left.
left();
else // Black lines were detected at the same time , the car stop.
brake();
}
}
Experimental steps:
1. We need to open the code of this experiment: Line_Walking.ino, click“√”under the
menu bar to compile the code, and wait for the word "Done compiling " in the lower
right corner, as shown in the figure below.

16
2. In the menu bar of Arduino IDE, we need to select 【Tools】---【Port】---
selecting the port that the serial number displayed by the device manager just now, as
shown in the figure below.

17
3. After the selection is completed, you need to click “→”under the menu bar to upload
the code to the Arduino UNO board. When the word “Done uploading” appears in the
lower left corner, the code has been successfully uploaded to the Arduino UNO board,
as shown in the figure below.

18
4. After the program is successfully downloaded, adjust potentiometers SW3 and SW4
to the correct angle.
5.Put the BatCar on the patrol tarck and press the start button K1. With a short whistle,
the BatCar starts running along the black line.

19
4- Infrared obstacle avoidance
The purpose of the experiment:
Debug potentiometers SW1 and SW2 according to the debug diagram at the end of the
article.Turn on the power switch of the BatCar, press the start button K1, and when you
hear the short whistle, the BatCar avoids the obstacle.
List of components required for the experiment:
BatCar*1
USB data cable*1
Experimental code analysis:
int Left_motor_back = 9;
int Left_motor_go = 5;
int Right_motor_go = 6;
int Right_motor_back = 10;
int Right_motor_en = 8;
int Left_motor_en = 7;
/*Set Button port*/
int key=4;
/*Set BUZZER port*/
int beep=3;
/*Line Walking*/
const int SensorRight = A3; // Set Right Line Walking Infrared sensor port
const int SensorLeft = A2; // Set Left Line Walking Infrared sensor port
int SL; // State of Left Line Walking Infrared sensor
int SR; // State of Right Line Walking Infrared sensor
/*Infrared obstacle avoidance*/
const int SensorRight_2 = A4; // Right Infrared sensor
const int SensorLeft_2 = A5; // Left Infrared sensor
int SL_2; // State of Left Infrared sensor
int SR_2; // State of Right Infrared sensor
void setup()
{
//Initialize motor drive for output mode
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
pinMode(key,INPUT);// Set button as input
pinMode(beep,OUTPUT);// Set buzzer as output
pinMode(SensorRight, INPUT); // Set Right Line Walking Infrared sensor as input
Table of contents
Other YahBoom Robotics manuals