找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5106|回复: 0
打印 上一主题 下一主题
收起左侧

国外找的arduino循迹智能车安装方法及代码

[复制链接]
跳转到指定楼层
楼主
循迹智能车安装方法


arduino单片机源程序如下:
  1. /* Arduino Object Avoidance Car
  2. * By Kevin Cho
  3. *
  4. * Inspired by http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/
  5. * PWM setup value from :http://playground.arduino.cc/Code/PwmFrequency
  6. *
  7. * This code allows an Arduino Smart employing HC-SR04 Ping distance sensor to identify an obstacle on its path and turns
  8. * left to avoid it.
  9. *
  10. * ===== Ultrasonic Sensor (HC-SR04) =====
  11. * pins:
  12. * Echo to digital input (see pin assignment below)
  13. * Trig to digital input (see ping assignmanet below)
  14. *
  15. * ===== motor output =====
  16. * 3 outputs for the left motor control
  17. * 3 outputs for the right motor control
  18. *
  19. * See below for pin assignment for Arduino digital ports.
  20. * PWM control must be on PWM pins 3, 5, 6, 9, 10 and 11.
  21. */

  22. //Pin assignment
  23. const int usTrigPin = 3; //connects to Arduino digital pin #2
  24. const int usEchoPin = 4; //connects to Arduino digital pin #3

  25. const int lMotPos = 6;  //left motor(+)
  26. const int lMotNeg = 7;  //left motor(-1)

  27. const int rMotNeg = 8;  //right motor (-1)
  28. const int rMotPos = 11; //right motor (+1)

  29. const int led = 13;      //Arduino built-in led. No connection required

  30. //operational parameters
  31. const int paceOfSound = 29.15;  //pace of sound = 1/speed of sound
  32.                                 //              = 1/0.03435 cm/ss
  33.                                 //              = 29.15 us/cm

  34. //CALIBRATION
  35. int turningPoint = 20;  //turn when an obstacle is within 20cm (approx 8in)

  36. //wait 100msec until next sensor check.
  37. int sensDelay = 100;

  38. //used only in testing phase
  39. boolean DEBUG = false;    //change to true when debuging. See serial monitor for log

  40. // the setup routine runs once when you press reset or power on
  41. void setup() {
  42.    //set digital pin modes for ultrasonic sensors
  43.   pinMode(usTrigPin, OUTPUT);
  44.   pinMode(usEchoPin, INPUT);

  45.   //set digital pin mode for built-in led
  46.   pinMode(led, OUTPUT);

  47.   if (DEBUG) Serial.begin (115200); //set the serial monitor transfer speed
  48.   
  49.   delay (1000);  //wait 1 sec before going
  50. }


  51. void loop() {  

  52.   int distance;
  53.   
  54.   // call getDistance to get the distance to an obstacle. Ignore anything farther than 100cm
  55.   distance = getDistance();
  56.   
  57.   if (distance >= turningPoint || distance <= 0) {
  58.       goForward();
  59.       digitalWrite(led, LOW);  //led off
  60.   } else {
  61.       if (DEBUG) {
  62.         Serial.println("Alert: Turn left");
  63.       }
  64.    
  65.       turnLeft();
  66.       digitalWrite(led, HIGH);  //led on
  67.   }
  68.   
  69.   delay(sensDelay);
  70. }

  71. int getDistance() {
  72.   
  73.   long duration, distance;
  74.   
  75.   digitalWrite(usTrigPin, LOW);  // Added this line
  76.   delayMicroseconds(2); // Added this line

  77.   //send 10 micro second pulse to trigger
  78.   digitalWrite(usTrigPin, HIGH);
  79.   delayMicroseconds(10); // Added this line per spec (spec says at least 10us)
  80.   digitalWrite(usTrigPin, LOW);

  81.   //Below step wait for HIGH upto a second (default) on the Echo pin before timing out.
  82.   duration = pulseIn(usEchoPin, HIGH);   //wait for HIGH
  83.                                          
  84.   //calculate the distance in cm
  85.   distance = (duration/2) / paceOfSound;

  86.   //We don't care about distance of more than 100cm
  87.   if (distance >= 100 || distance <= 0){
  88.       if (DEBUG) Serial.println("getDistance: Out of range");
  89.       return 0;
  90.   } else {
  91.       if (DEBUG) {
  92.         Serial.print("getDistance: ");
  93.         Serial.print(distance);
  94.         Serial.println(" cm");
  95.       }
  96.       return distance;
  97.   }
  98. }  

  99. void goForward ()
  100. {
  101.   digitalWrite (lMotNeg, LOW);
  102.   digitalWrite (lMotPos, HIGH);   
  103.   digitalWrite (rMotNeg, LOW);   
  104.   digitalWrite (rMotPos, HIGH);
  105. }

  106. void turnLeft ()
  107. {
  108.   digitalWrite (lMotNeg, HIGH);  
  109.   digitalWrite (lMotPos, LOW);
  110.   digitalWrite (rMotNeg, LOW);   
  111.   digitalWrite (rMotPos, HIGH);
  112. }

  113. void turnRight ()
  114. {
  115.   digitalWrite (lMotNeg, LOW);  
  116.   digitalWrite (lMotPos, HIGH);
  117.   digitalWrite (rMotNeg, HIGH);   
  118. ……………………

  119. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
小车安装.rar (11.3 MB, 下载次数: 20)
源代码.rar (3.61 KB, 下载次数: 16)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表