littlebot
Published on 2025-04-16 / 0 Visits
0

【源码】基于 Arduino 的线路探测小车系统

项目简介

本项目是一个基于 Arduino 的线路探测小车系统,通过线路探测传感器实现小车沿着特定线路行驶的功能,为 DIY 爱好者提供了一个具有实际应用价值的开发案例。

项目的主要特性和功能

  • 线路探测功能:利用线路探测传感器感知线路,让小车能够沿着预设线路自动行驶。
  • 电机驱动控制:借助 L298 电机驱动模块,精准控制小车的四个直流减速电机,实现前进、转弯等动作。
  • 可扩展性强:可以根据需求添加更多的传感器或功能模块,进一步丰富小车的功能。

安装使用步骤

前提条件

假设你已经下载了本项目的源码文件,并且具备 Arduino 开发环境。若首次使用 Arduino 软件,需完成 此处 指南中的所有步骤;若对项目中的硬件不熟悉,可在 Makerlab 的 Wiki 页面 了解各硬件的使用方法。

硬件准备

| 序号 | 设备名称 | 数量 | 链接 | | ---- | ---- | ---- | ---- | | 1 | Mạch Uno SMD CH340 (Arduino Uno Compatible) | 1 个 | Link | | 2 | Khung Xe Robot Chasiss 4WD Car R1 | 1 个 | Link | | 3 | Động Cơ DC Giảm Tốc V1 Dual Shaft Plastic Geared TT Motor + Bánh Xe | 4 个 | Link | | 4 | Hộp Pin 2 X 18650 Battery Holder Có Công Tắc | 1 个 | Link | | 5 | Breadboard Mini 170 Lỗ 45x34x9mm | 1 个 | Link | | 6 | Cảm Biến Dò Line Đơn TCRT5000 Line Follower Sensor | 3 个 | Link | | 7 | Mạch Điều Khiển Động Cơ L298 DC Motor Driver | 1 个 | Link | | 8 | Đầu Chuyển Domino Sang Giắc DC Đực 2.1mm | 1 个 | Link | | 9 | Dây Điện Đỏ Đen | 1 米 | Link | | 10 | Dây bus đực - cái | 1 包 | Link | | 11 | Dây bus đực - đực | 1 包 | Link |

硬件连接

| UNO | A1 | A2 | A3 | 5V | GND | | ---- | ---- | ---- | ---- | ---- | ---- | | TCRT 5000 (左) | | | DO | 5V | GND | | TCRT 5000 (中) | | DO | | 5V | GND | | TCRT 5000 (右) | DO | | | 5V | GND |

| UNO | D4 | D5 | D6 | D7 | DC+ | DC - | | ---- | ---- | ---- | ---- | ---- | ---- | ---- | | L298 | IN1 | IN2 | IN3 | IN4 | VCC | GND | | 电源 | | | | | + | - |

| L298 | OUT1 | OUT2 | OUT3 | OUT4 | | ---- | ---- | ---- | ---- | ---- | | 电机 | DC1 | DC1 | DC2 | DC2 |

代码上传

将项目中的 Arduino 代码复制到 Arduino IDE 中,选择正确的开发板和端口,然后上传代码到 Arduino 开发板。代码如下: ```cpp

include

include "R1_VAR.h"

float eye_Left_Value; float eye_Right_Value; float eye_Mid_Value; float threshold_left; float last_lost_dir; float left_dir; float speedForward; float threshold_right; float right_dir; float threshold_mid; float speedForwardlow; float stopspeed;

void setup(){ threshold_left = 0.5; threshold_right = 0.5; threshold_mid = 0.5; left_dir = 0; right_dir = 1; last_lost_dir = left_dir; speedForward = 35; speedForwardlow = speedForward/2; stopspeed = 0; } void loop(){ eye_Left_Value = digitalRead(A3); eye_Right_Value = digitalRead(A1); eye_Mid_Value = digitalRead(A2); // if((eye_Mid_Value > threshold_mid)){

      demoMotor.car_fw(speedForward,speedForward);

    }else{
      if((eye_Left_Value > threshold_left)  &&  (eye_Right_Value < threshold_right) &&  (eye_Mid_Value > threshold_mid)){


        demoMotor.car_fw(speedForwardlow,speedForward);
        }else{
      if((eye_Left_Value < threshold_left)  &&  (eye_Right_Value > threshold_right) &&  (eye_Mid_Value > threshold_mid)){


        demoMotor.car_fw(speedForward,speedForwardlow);
        }else{
      if((eye_Left_Value > threshold_left)  &&  (eye_Right_Value < threshold_right) &&  (eye_Mid_Value < threshold_mid)){
        last_lost_dir = left_dir;

        demoMotor.car_fw(stopspeed,speedForward);
      }else{
        if((eye_Left_Value < threshold_left)  &&  (eye_Right_Value > threshold_right) &&  (eye_Mid_Value < threshold_mid)){
          last_lost_dir = right_dir;

          demoMotor.car_fw(speedForward,stopspeed);

        }else{
          if(last_lost_dir == left_dir){

            demoMotor.car_rotateL(speedForward);

          }else{

            demoMotor.car_rotateR(speedForward);
          }

        }

      }

    }

}

} } ```

测试运行

完成硬件连接和代码上传后,给小车通电,小车即可开始沿着线路行驶。

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】