Category :Arduino Projects

Byamber

Robot Tank Car Starter Kit Lesson 4:Tracking Robot Tank Car

Robot Tank Car Starter Kit Lesson 4: Control the motor

Objective:

In this lesson, we will use 2 black/white tracking sensors to the framework built in Lesson 1. If you have not completed installation in Lesson 1, please review Lesson 1

The software in this lesson will read data from these 2 black/white tracking sensors and automatically guide the smart car to move along the black track line in the white ground.

How It Work:

There are two probe on tracking sensor module,one is sender diode(IR LED) and the other one is receiver diode(Photo Diode).

If the tracking sensor is not on black track,the infrared ray from IR LED can be reflected and received by photo diode. The sensor will output low level.

If the tracking sensor is on black track,the infrared ray from IR LED can not be reflected and not received by photo diode. The sensor will output high level.

There are 2 tracking sensor on left and right. When the left tracking sensor on black line, the tank car will turn left. When the right tracking sensor on black line, the tank car will turn right. When the left and right tracking sensor on both sideways of black line, the tank car will go straight. When both tracking sensor are on black line,the tank car will stop.

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:Download Lesson 4 sample code from http://osoyoo.com/driver/tank_robot_lesson4.zip , unzip the download zip file tank_robot_lesson4.zip, you will see a folder called tank_robot_lesson4.

Step 3: 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_lesson2 folder, load the code into arduino.

open

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

port

Understanding the Code:

Step 1: Define the pinout of left and right tracking sensor.

#define LFSensor_1 A0 //line follow sensor1
#define LFSensor_2 A1 //line follow sensor2

Step 2:Read the signal from tracking sensor and store the signal value.

void read_sensor_values()
{
  sensor[0]=digitalRead(LFSensor_1);
  sensor[1]=digitalRead(LFSensor_2);
}

Step 3:Control the tank car movement. The structure of auto_tarcking() can get the left and right sensor signal via call the structure of read_sensor_values(),then judge the signals via “if “control structure.

void auto_tarcking(){
  read_sensor_values();
  if((sensor[0]==LOW)&&(sensor[1]==HIGH)){ //The right sensor is on the black line.The left sensor is on the white line
    set_motorspeed(M_SPEED1,M_SPEED1);
    turn_right(250);
  }
  else if((sensor[0]==HIGH)&&(sensor[1]==LOW)){//The right sensor is on the white line.The left sensor is on the black line
    set_motorspeed(M_SPEED1,M_SPEED1);
    turn_left(250);
  }
  else if((sensor[0]==LOW)&&(sensor[1]==LOW)){//The left an right sensor are on the white line.
    set_motorspeed(M_SPEED2,M_SPEED2);
    go_ahead();
  }
  else if((sensor[0]==HIGH)&&(sensor[1]==HIGH)){//The left an right sensor are on the blac

Step 4: Testing. Press the OK button to make the tank car go along with black line.Press “o” to make the tank car stop.

注意:若无法循迹,请看考第一课调节循迹模块灵敏度,并检查接线。

Byamber

Robot Tank Car Starter Kit Lesson 3:Control the Tank Car Through IR Remote

Robot Tank Car Starter Kit Lesson 3: Control the motor

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.

How it work

There is a IR receiver and remote control. The arduino board would translate to kinds of behavior once it receive the IR signals from remote control.

Hardware Installation:

Step 1: Install ESP8266 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 ESP8266 wifi Board as the following picture.

IR Receiver-ESP8266
Step 3: Turn the switch of esp8266 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 step)

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://osoyoo.com/wp-content/uploads/samplecode/IRremote.zip, then import the library into Arduino IDE(Open Arduino IDE-> click Sketch->Include Library->Add .Zip Library)

Step 3:Download Lesson 3 sample code from http://osoyoo.com/driver/tank_robot_lesson3.zip , unzip the download zip file tank_robot_lesson2.zip, you will see a folder called tank_robot_lesson2.

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

open

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

Understanding the Code:

Step 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

Step 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

Step 3: Define each button function in enumeration variable for readability.

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;

Step 4:Decode the IR signal. The decode result from IR signal will be stored in variable of  IRresults.value and will be compared with predefined button IR code. If they are the same, its counterpart function value will be assign to enumeration variable of Drive_Num.

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();
  }
}

Step 5: Execute the function Structure. In do_Drive_Tick() structure, judge the variable value of Drive_Num and execute the corresponding function structure.

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();
      }
    }
}

Step 6: The Control Structure of LED,Buzzer and Servo: turn on and turn off the LED,make the buzzer beep,make the servo motor rotate 180 degree and rotate back to 0 degree,then back to 90 degree.

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);
}

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

Byamber

Robot Tank Car Starter Kit Lesson 2: Control the motor

Robot Tank Car Starter Kit Lesson 2: Control the motor

 

Objective:

In this tutorial, we will use KOOKYE Smart DIY kit to make a simple program controlled smart car. Once the car installation is completed, we will use a UNO R3 board to control the car movements including go forward, go back, left turn and right turn.

 

How It Work:

Three Parts of Car Motor: DC motor,gear reducer and encoder.
There are 6 pin wires from encoder motor,two of them (red and black wires) come from the DC motor.
The pinout K1 or K2 and K3 or K4 on L298N board can be used for driving the left motor and right motor respectively.
The pinout ENA and ENB can be used to adjust the motor speed via input PWM signal.
The pinout N1,N2,N3 and N4 can be used to control the forward and backward direction of motor.

Motor(Left) Motor (Right)
ENA IN1 IN2 DC motor status ENB IN3 IN4 DC motor status
0 x x stop 0 x x stop
1 0 1 rotate clockwise 1 0 1 rotate clockwise
1 1 0 rotate counterclockwise 1 1 0 rotate counterclockwise
1 1 1 brake 1 1 1 brake
1 0 0 brake 1 0 0 brake

 

Hardware Installation:

Step 1: Install ESP8266 Expansion Board on UNO R3 board.
lesson2-HW-1
Step 2: Move the wire connected to digit ports(D5,D6,D8,D9,D10,D12) in UNO R3 board to its counterpart digit pin in ESP8266 wifi Board.

L298N-ESP8266
Step 3: Turn the switch of esp8266 to “1” and “2” position, as the following photo shows.

lesson2-HW-2
(If you have finished the above steps on lesson one, please skip these step)

 

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:Download Lesson Two sample code from http://osoyoo.com/driver/tank_robot_lesson2.zip , unzip the download zip file tank_robot_lesson2.zip, you will see a folder called tank_robot_lesson2.

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

open

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

port

 

Understanding the Code:

Step 1: Define the pinout as the table

#define IN1  8    //K1、K2 motor direction
#define IN2  9    //K1、K2 motor direction
#define IN3  10    //K3、K4 motor direction
#define IN4  12   //K3、K4 motor direction
#define ENA  5    //needs to be a PWM pin to be able to control motor speed ENA
#define ENB  6   //needs to be a PWM pin to be able to control motor speed ENB

Step 2: Understand the structure,value and function.

void go_ahead() //motor rotate clockwise -->robot go ahead
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4,HIGH);
}
void go_back()  //motor rotate counterclockwise -->robot go back
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4,LOW); 
}
void go_stop()   //motor brake  -->robot stop
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4,LOW); 
}
void turn_left()  //left motor rotate counterclockwise and right motor rotate clockwise -->robot turn left
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}
void turn_right() //left motor rotate clockwise and right motor rotate counterclockwise -->robot turn right
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}
/*set motor speed */
void set_motorspeed(int lspeed,int rspeed)  //change motor speed
{
  analogWrite(ENA,lspeed);//lspeed:0-255
  analogWrite(ENB,rspeed);//rspeed:0-255  
}

Step 3:Data initialization. We need to set the correct work mode to each pinouts(ENA,ENB,N1,N2,N3,N4) on L298N and call the above function in step 2.

 void setup() 
{
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT); 
  pinMode(ENA, OUTPUT); 
  pinMode(ENB, OUTPUT);  
  set_motorspeed(255,255);//maximum speed
  go_ahead(),delay(5000),go_stop();  //robot forward 5s
  go_back(),delay(5000),go_stop();  //robot go back 5s
  turn_left(),delay(5000),go_stop();//robot turn left 5s
  turn_right(),delay(5000),go_stop();//robot turn right 5s
  go_stop();//stop
}

Step 4: testing the code, the tank car will go forward for 5s,go back for 5s ,turn left for 5s and turn right for 5s in sequence.You also can change the example code to make the car movements as your need.