Skip to content

Latest commit

 

History

History
155 lines (112 loc) · 4.36 KB

car_following.rst

File metadata and controls

155 lines (112 loc) · 4.36 KB

Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
  • Learn & Share: Exchange tips and tutorials to enhance your skills.
  • Exclusive Previews: Get early access to new product announcements and sneak peeks.
  • Special Discounts: Enjoy exclusive discounts on our newest products.
  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [] and join today!

7. Follow Your Hand

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

Required Components

In this project, we need the following components.

It's definitely convenient to buy a whole kit, here's the link:

Name ITEMS IN THIS KIT LINK
3 in 1 Starter Kit 380+

You can also buy them separately from the links below.

COMPONENT INTRODUCTION PURCHASE LINK
cpn_uno
cpn_l9110 -
cpn_tt_motor -
cpn_ultrasonic
cpn_avoid

Wiring

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

Wire the ultrasonic to the R4 board as follows.

Ultrasonic Module R4 Board
Vcc 5V
Trig 3
Echo 4
Gnd GND

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

Left IR Module R4 Board
OUT 8
GND GND
VCC 5V
Right IR Module R4 Board
OUT 7
GND GND
VCC 5V

image

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 car_ultrasonic and car_ir_obstacle, 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();
    }
}