7. Follow Your HandΒΆ

Think of this car as your pet here, and when you will wave to him, it comes running to you.

Wiring

Connect the ultrasonic module and the 2 IR obstacle avoidance modules at the same time.

Wire the ultrasonic to the R3 board as follows.

Ultrasonic Module

R3 Board

Vcc

5V

Trig

3

Echo

4

Gnd

GND

The wiring of the 2 IR obstacle avoidance modules to the R3 board is as follows.

Left IR Module

R3 Board

OUT

8

GND

GND

VCC

5V

Right IR Module

R3 Board

OUT

7

GND

GND

VCC

5V

../_images/car_avoid_ultrasonic.jpg

Code

Note

  • Open the 7.follow_your_hand.ino file under the path of 3in1-kit\car_project\7.follow_your_hand.

  • Or copy this code into Arduino IDE.

  • Or upload the code through the Arduino Web Editor.

Place the car on the ground after the code has been uploaded successfully. Place your hand close to 5*10cm in front of the car, and it will follow your hand forward. If you put your hand close to the IR Obstacle module on both sides, it will also turn to the corresponding direction.

How it works?

This project is a combination of the previous two projects 6. Play with Ultrasonic Module and 5. Play with Obstacle Avoidance Module, but the implemented effect is different. The previous 2 projects are detecting an obstacle backwards, but here it is detecting that your hand will follow the forward or turn direction. The workflow of this project is as follows.

  • Read the distance detected by the ultrasonic module and the value of both infrared modules.

  • If the distance is 5~10cm, let the car move with your hand.

  • If the left IR module detects your hand, turn left.

  • If the right IR module detects your hand, turn right.

  • If neither the infrared module nor the ultrasonic module detects your hand, let the car stop.

void loop() {

    float distance = readSensorData();

    int left = digitalRead(leftIR);   // 0: Obstructed  1: Empty
    int right = digitalRead(rightIR);
    int speed = 150;

    if (distance>5 && distance<10){
        moveForward(speed);
    }
    if(!left&&right){
        turnLeft(speed);
    }else if(left&&!right){
        turnRight(speed);
    }else{
        stopMove();
    }
}