電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>使用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.

創(chuàng)建對象和變量

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 代碼的無效循環(huán)部分

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
}

制作計算右側距離的函數(shù)

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
}

制作計算左側距離的函數(shù)

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數(shù)據(jù)手冊
  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開發(fā)指南
  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數(shù)字電路基礎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次下載  |  免費