Arduino Tank Car Kit Lesson 4:Control the Car Through IR Remote

Byamber

Arduino Tank Car Kit Lesson 4:Control the Car Through IR Remote

{:en}


Authorized Amazon Seller:

Robot Tank Car Chassis

Buy from US Buy from CA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES

Robot Tank Car Electronic Parts Kit

Buy from US Buy from CA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES

I. Objective:

In this tutorial, we will use KOOKYE Smart tank car to make a simple remote controlled smart car. Once the car installation is completed, we will use a Infrared Remote to control the car movements including go forward, go back, left turn and right turn.

II. How it work

There is a IR receiver and remote control. The arduino board would translate the programs to the predefined behaviors once it receive the IR signals from remote control.

III. Software Installation:

Step 1: Install latest Arduino IDE (If you have Arduino IDE version after 1.1.16, please skip this step)
Download Arduino IDE from https://www.arduino.cc/en/Main/Software?setlang=en , then install the software.

Step 2: Install IRremote library into Arduino IDE (If you have already installed IRremote library, please skip this step)
Download IRremote library from  http://www.kookye.com/download/Arduino_Tank_Car_Kit/IRremote.zip, then import the library into Arduino IDE(Open Arduino IDE-> click Sketch->Include Library->Add .Zip Library)

Step 3:Download Lesson 4sample code from http://www.kookye.com/download/Arduino_Tank_Car_Kit/tank_robot_lesson4.zip , unzip the download zip file tank_robot_lesson4.zip, you will see a folder called tank_robot_lesson4.

Step 4: Connect UNO R3 board to PC with USB cable, Open Arduino IDE -> click file -> click Open -> choose code “tank_robot_lesson4.ino” in tank_robot_lesson4 folder, load the code into arduino.

open

Step 5:Choose corresponding board and port for your project,upload the sketch to the board.

port

IV. Understanding the Code:

Part 1: Define the button that you will be using on IR remote control. (If you use the lesson 1example code, the arduino IDE Serial Monitor will output its counterpart IR code.)

#define IR_ADVANCE       0x00FF18E7       //code from IR controller "▲" button
#define IR_BACK          0x00FF4AB5       //code from IR controller "▼" button
#define IR_RIGHT         0x00FF5AA5       //code from IR controller ">" button
#define IR_LEFT          0x00FF10EF       //code from IR controller "<" button
#define IR_SERVO         0x00FF38C7       //code from IR controller "OK" button
#define IR_OPENLED       0x00FFB04F       //code from IR controller "#" button
#define IR_CLOSELED      0x00FF6897       //code from IR controller "*" button
#define IR_BEEP          0x00FF9867       //code from IR controller "0" button

Part 2:The function on IR Remote Control

Button Function
go forward
go back
> turn right
< turn left
OK servo rotate
# turn on LED
* turn off LED
0 buzzer beep

Part 3: Define movement name for each button.

enum DN
{ 
  GO_ADVANCE, //go ahead
  GO_LEFT, //left turn
  GO_RIGHT,//right turn
  GO_BACK,//go back
  MOVE_SERVO,//move servo
  OPEN_LED,//open led
  CLOSE_LED,//close led
  BEEP,//control buzzer
  DEF
}Drive_Num=DEF;

Part 4:Decode the IR signal and translate button code into movment name. void do_IR_Tick()

void do_IR_Tick()

{
  if(IR.decode(&IRresults))
  {
    if(IRresults.value==IR_ADVANCE)
    {
      Drive_Num=GO_ADVANCE;
    }
    else if(IRresults.value==IR_RIGHT)
    {
       Drive_Num=GO_RIGHT;
    }
    else if(IRresults.value==IR_LEFT)
    {
       Drive_Num=GO_LEFT;
    }
    else if(IRresults.value==IR_BACK)
    {
        Drive_Num=GO_BACK;
    }
    else if(IRresults.value==IR_SERVO)
    {
        Drive_Num=MOVE_SERVO;
    }
    else if(IRresults.value==IR_OPENLED)
    {
      Drive_Num=OPEN_LED;
    }
    else if(IRresults.value==IR_CLOSELED)
    {
      Drive_Num=CLOSE_LED;
    }
    else if(IRresults.value==IR_BEEP)
    {
      Drive_Num=BEEP;
    }
    IRresults.value = 0;
    IR.resume();
  }
}

Part 5: Execute movement function as per movement name from Part 4.

void do_Drive_Tick()
{
    switch (Drive_Num) 
    {
      case GO_ADVANCE:
            go_ahead(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance
      case GO_LEFT:
            turn_left(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_LEFT code is detected, then turn left
      case GO_RIGHT:
            turn_right(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_RIGHT code is detected, then turn right
      case GO_BACK:
            go_back(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_BACK code is detected, then backward
      case MOVE_SERVO:
            move_servo();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//move servo
      case OPEN_LED:
            open_led(1),open_led(2);JogTime = 0;break;//open led
      case CLOSE_LED:
            close_led(1),close_led(2);JogTime = 0;break;//close led
      case BEEP:
            control_beep();JogTime = 0;break;//control beep
      default:break;
    }
    Drive_Num=DEF;
   //keep current moving mode for  200 millis seconds
    if(millis()-JogTime>=200)
    {
      JogTime=millis();
      if(JogFlag == true) 
      {
        stopFlag = false;
        if(JogTimeCnt <= 0) 
        {
          JogFlag = false; stopFlag = true;
        }
        JogTimeCnt--;
      }
      if(stopFlag == true) 
      {
        JogTimeCnt=0;
        go_stop();
      }
    }
}

V. Hardware Installation:

Step 1: Install expansion board on UNO R3 board.
lesson2-HW-1
Step 2: Move the wire connected to pinout(GND,VCC,S) in IR recevier to the counterpart pin in expansion board as the following picture.

IR_Receiver-ESP8266
Step 3: Turn the switch of expansion board to "1" and "2" position, as the following photo shows.

lesson2-HW-2

Step 4: Put two 12865 batteries in battery box and turn the swith of box to "ON". (If you have finished the above steps on lesson one, please skip these steps)
Step 5:

Press IR controller keys to control the car movements as per predefine code.

Note: Please check the wire connections if the tank car can not work as expected.


Youtube link--https://youtu.be/xc3YXAJL8mY

{:}{:zh}概述

在第二课中介绍了如何控制电机,这一课中我们会简介如何用红外遥控器控制坦克小车运动。

工作原理

坦克小车上有一个红外接收器,当遥控器按下按键,红外接收器会接收红外信号,arduino会把这个红外信号解码,每个按键对应一个不同的红外信号,当arduino接收到不同的红外信号后执行不同的操作,实现不同的功能。例如,arduino接收到遥控器上“1”对应的红外信号后坦克小车前进;接收到“2”对应的红外信号小车后退。

软件

下载http://www.kookye.com/download/car/tank_robot_lesson3.zip并解压文件,用arduino IDE打开tank_robot_lesson3.ino文件,下面对部分代码做简要说明。

#define IR_ADVANCE       0x00FF18E7       //code from IR controller "▲" button
#define IR_BACK          0x00FF4AB5       //code from IR controller "▼" button
#define IR_RIGHT         0x00FF5AA5       //code from IR controller ">" button
#define IR_LEFT          0x00FF10EF       //code from IR controller "<" button
#define IR_SERVO         0x00FF38C7       //code from IR controller "OK" button
#define IR_OPENLED       0x00FFB04F       //code from IR controller "#" button
#define IR_CLOSELED      0x00FF6897       //code from IR controller "*" button
#define IR_BEEP          0x00FF9867       //code from IR controller "0" button

上面的代码,把要用到按键的红外编码在软件中做如下定义,每个按键的红外编码可以烧录第一课中的代码后,对着红外接收器按遥控器按键,打开arduino IDE Serial Monitor就会打印出对应按键的红外编码。

在本课中我们用到的按键和按键功能如下表所示

按键名 对应功能
前进
后退
> 右转
< 左转
OK 旋转舵机
# 打开LED
* 关闭LED
0 蜂鸣器响

 

enum DN
{ 
  GO_ADVANCE, //go ahead
  GO_LEFT, //left turn
  GO_RIGHT,//right turn
  GO_BACK,//go back
  MOVE_SERVO,//move servo
  OPEN_LED,//open led
  CLOSE_LED,//close led
  BEEP,//control buzzer
  DEF
}Drive_Num=DEF;

为了增加代码的可读性,把每个按键的功能定义在枚举变量中。

void do_IR_Tick()
{
  if(IR.decode(&IRresults))
  {
    if(IRresults.value==IR_ADVANCE)
    {
      Drive_Num=GO_ADVANCE;
    }
    else if(IRresults.value==IR_RIGHT)
    {
       Drive_Num=GO_RIGHT;
    }
    else if(IRresults.value==IR_LEFT)
    {
       Drive_Num=GO_LEFT;
    }
    else if(IRresults.value==IR_BACK)
    {
        Drive_Num=GO_BACK;
    }
    else if(IRresults.value==IR_SERVO)
    {
        Drive_Num=MOVE_SERVO;
    }
    else if(IRresults.value==IR_OPENLED)
    {
      Drive_Num=OPEN_LED;
    }
    else if(IRresults.value==IR_CLOSELED)
    {
      Drive_Num=CLOSE_LED;
    }
    else if(IRresults.value==IR_BEEP)
    {
      Drive_Num=BEEP;
    }
    IRresults.value = 0;
    IR.resume();
  }
}

上面的函数用于解码红外信号,通过调用IR.decode(&IRresults)将红外信号解码结果存储在IRresults.value变量中,比较IRresults.value变量与预定义按键红外编码,如果相等,就把对应的功能代码赋值给枚举变量Drive_Num。

void do_Drive_Tick()
{
    switch (Drive_Num) 
    {
      case GO_ADVANCE:
            go_ahead(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance
      case GO_LEFT:
            turn_left(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_LEFT code is detected, then turn left
      case GO_RIGHT:
            turn_right(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_RIGHT code is detected, then turn right
      case GO_BACK:
            go_back(10);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_BACK code is detected, then backward
      case MOVE_SERVO:
            move_servo();JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//move servo
      case OPEN_LED:
            open_led(1),open_led(2);JogTime = 0;break;//open led
      case CLOSE_LED:
            close_led(1),close_led(2);JogTime = 0;break;//close led
      case BEEP:
            control_beep();JogTime = 0;break;//control beep
      default:break;
    }
    Drive_Num=DEF;
   //keep current moving mode for  200 millis seconds
    if(millis()-JogTime>=200)
    {
      JogTime=millis();
      if(JogFlag == true) 
      {
        stopFlag = false;
        if(JogTimeCnt <= 0) 
        {
          JogFlag = false; stopFlag = true;
        }
        JogTimeCnt--;
      }
      if(stopFlag == true) 
      {
        JogTimeCnt=0;
        go_stop();
      }
    }
}

在do_Drive_Tick()函数中,判断Drive_Num变量的值并执行对应的功能函数。

void open_led(int led_num)
{
  if (led_num == 1)  digitalWrite(LED1,LOW);
  else digitalWrite(LED2,LOW);
}
void close_led(int led_num)
{
   if (led_num == 1)  digitalWrite(LED1,HIGH);
   else digitalWrite(LED2,HIGH);
}
/*******control buzzer*******/
void control_beep()
{
  digitalWrite(BUZZER,LOW),delay(100);
  digitalWrite(BUZZER,HIGH),delay(100);
}
/***move servo***/
void move_servo()
{
  int i;
   for(i = 0;i<180;i++){ head.write(i); delay(5); } for(i = 180;i>=0;i--){
     head.write(i);
     delay(5);
  }
  head.write(90);
}

上面的4个函数分别用于控制LED的打开、关闭、控制蜂鸣器响以及让舵机从0度转到180度,再从180度转到0度,最后回到90度。

测试

step 1 用usb线把坦克下车与pc连接起来,选择正确的板子型号和端口号,将程序烧录到arduino中

step 2 打开电池盒电源

step 3 将遥控器对着红外接收器按按键,如果所按按键是程序预先定义好的,小车将执行相应的功能函数;如果所按按键是程序未定义的,小车不做任何操作。

 {:}

About the Author

amber administrator

You must be logged in to post a comment