电子发烧友App

硬声App

0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示
电子发烧友网>电子资料下载>电子资料>使用Arduino的自控机器人汽车

使用Arduino的自控机器人汽车

2022-11-16 | zip | 0.16 MB | 次下载 | 2积分

资料介绍

描述

一、简介

使用 Arduino 的自控机器人汽车。

这辆机器人汽车使用超声波传感器来检测前方的障碍物,每当它检测到障碍物时,它的超声波传感器就会在左右两个方向上移动,以计算出自由移动的最佳距离。

它的超声波传感器范围高达 150 厘米。

2. 示范

3. 制作这款机器人汽车的步骤

在 Arduino IDE 中导入 Servo.h 和 NewPing.h 库。

// Library
#include         // Include Servo Library
#include       // Include Newping Library 

初始化引脚

// L298N Control Pins
const int LeftMotorForward = 4;
const int LeftMotorBackward = 5;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;
const int LEDext = 1;
const int Buzzer = 0;

#define TRIGGER_PIN  A1  // Arduino pin to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     A2  // Arduino pin to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.

创建对象和变量

Servo servo_motor;  // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
boolean goesForward = false;
int distance = 50;

编写 Arduino 代码的设置部分

 void setup()
{
// Set L298N Control Pins as Output
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(LEDext, OUTPUT);        //set led as output
pinMode(Buzzer, OUTPUT);        //set buzzer as output
servo_motor.attach(9);   // Attachs the servo on pin 9 to servo object.
servo_motor.write(115);   // Set at 115 degrees.
delay(2000);              // Wait for 2s.
distance = readPing();    // Get Ping Distance.
delay(100);               // Wait for 100ms.
}

编写 Arduino 代码的无效循环部分

void loop() 
{  
  int distanceRight = 0;  //Initialize right side distance                                              
  int distanceLeft = 0;   //Initialize left side distance
  delay(50);

  if (distance <= 30)   //If distance of obstacle less than 30 cm from robot 
  {
    Stop();   //call stop function to stop the robot
    digitalWrite(LEDext, HIGH);   //Turn led ON
    digitalWrite(Buzzer, HIGH);   //Turn Buzzer ON
    delay(300);   //wait for 300ms
    moveBackward();   //call moveBackward function to move robot in backward direction
    
    delay(400);   //wait for 400ms
    Stop();   //call stop function to stop the robot
    
    delay(300);   //wait for 300ms
    distanceRight = lookRight();    //call lookRight function to save distance in distanceRight variable
    
    delay(300);   //wait for 300ms
    distanceLeft = lookLeft();    //call lookLeft function to save distance in distanceLeft variable
    
    delay(300);   //wait for 300ms

    if (distanceRight >= distanceLeft)    //If distance of right greater or equall to distance of left
    {
      turnRight();    //call function to turn right robot
      delay(300);   //wait for 300ms
      Stop();   //call stop function to stop robot
    }
    else //else
    {
      turnLeft();   //call function to turn left robot
      delay(300);   //wait for 300ms
      Stop();   //call stop function to stop robot
    }
  
  }
  else    //else
  {
    moveForward();    //call moveForward function to move robot in forward direction 
  }

    distance = readPing();    //call readPing function to calculate Distance
}

制作计算右侧距离的函数

int lookRight()     // lookRight Function for Servo Motor
{  
  servo_motor.write(0);   //make servo position at 0 degree
  delay(500);   //wait for 500ms
  int distance = readPing();    //read distance
  delay(100);   //wait for 100ms
  servo_motor.write(90);    //make servo position 90 degree
  return distance;    //return distance whenever lookRight function is called
}

制作计算左侧距离的函数

int lookLeft()      // lookLeft Function for Servo Motor 
{
  servo_motor.write(180);   //make servo position at 0 degree
  delay(500);   //wait for 500ms
  int distance = readPing();    //read distance
  delay(100);   //wait for 100ms
  servo_motor.write(90);    //make servo position 90 degree
  return distance;    //return distance whenever lookLeft function is called
} 

使功能与超声波传感器保持距离

int readPing()      // readPing Function for Ultrasonic Sensor.
{
  delay(100);                 // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  int cm = sonar.ping_cm();   //Send ping, get ping distance in centimeters (cm).
  if (cm==0)
  {
    cm=250;
  }
  return cm;    //return distance whenever readPing function is called
} 

制作停止机器人的功能

void Stop()       // Stop Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
} 

使机器人向前移动的功能

void moveForward()    // Move Forward Function for Motor Driver.
{
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(RightMotorBackward, LOW);
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(LEDext, LOW);
    digitalWrite(Buzzer, LOW);
}

使机器人向后移动的功能

void moveBackward()   // Move Backward Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
} 

使机器人向右移动的功能

void turnRight()      // Turn Right Function for Motor Driver.
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
} 

使机器人在左侧方向移动的功能

void turnLeft()       // Turn Left Function for Motor Driver.
{
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
} 

将代码上传到您的 Arduino 板。

4. 按照原理图连接所有组件。


下载该资料的人也在下载 下载该资料的人还在阅读
更多 >

评论

查看更多

下载排行

本周

  1. 1山景DSP芯片AP8248A2数据手册
  2. 1.06 MB  |  532次下载  |  免费
  3. 2RK3399完整板原理图(支持平板,盒子VR)
  4. 3.28 MB  |  339次下载  |  免费
  5. 3TC358743XBG评估板参考手册
  6. 1.36 MB  |  330次下载  |  免费
  7. 4DFM软件使用教程
  8. 0.84 MB  |  295次下载  |  免费
  9. 5元宇宙深度解析—未来的未来-风口还是泡沫
  10. 6.40 MB  |  227次下载  |  免费
  11. 6迪文DGUS开发指南
  12. 31.67 MB  |  194次下载  |  免费
  13. 7元宇宙底层硬件系列报告
  14. 13.42 MB  |  182次下载  |  免费
  15. 8FP5207XR-G1中文应用手册
  16. 1.09 MB  |  178次下载  |  免费

本月

  1. 1OrCAD10.5下载OrCAD10.5中文版软件
  2. 0.00 MB  |  234315次下载  |  免费
  3. 2555集成电路应用800例(新编版)
  4. 0.00 MB  |  33566次下载  |  免费
  5. 3接口电路图大全
  6. 未知  |  30323次下载  |  免费
  7. 4开关电源设计实例指南
  8. 未知  |  21549次下载  |  免费
  9. 5电气工程师手册免费下载(新编第二版pdf电子书)
  10. 0.00 MB  |  15349次下载  |  免费
  11. 6数字电路基础pdf(下载)
  12. 未知  |  13750次下载  |  免费
  13. 7电子制作实例集锦 下载
  14. 未知  |  8113次下载  |  免费
  15. 8《LED驱动电路设计》 温德尔著
  16. 0.00 MB  |  6656次下载  |  免费

总榜

  1. 1matlab软件下载入口
  2. 未知  |  935054次下载  |  免费
  3. 2protel99se软件下载(可英文版转中文版)
  4. 78.1 MB  |  537798次下载  |  免费
  5. 3MATLAB 7.1 下载 (含软件介绍)
  6. 未知  |  420027次下载  |  免费
  7. 4OrCAD10.5下载OrCAD10.5中文版软件
  8. 0.00 MB  |  234315次下载  |  免费
  9. 5Altium DXP2002下载入口
  10. 未知  |  233046次下载  |  免费
  11. 6电路仿真软件multisim 10.0免费下载
  12. 340992  |  191187次下载  |  免费
  13. 7十天学会AVR单片机与C语言视频教程 下载
  14. 158M  |  183279次下载  |  免费
  15. 8proe5.0野火版下载(中文版免费下载)
  16. 未知  |  138040次下载  |  免费