极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19442|回复: 5

四路红外避障小车[多图][有程序]

[复制链接]
发表于 2015-5-10 20:44:03 | 显示全部楼层 |阅读模式
本帖最后由 我的孤独 于 2015-5-13 13:52 编辑

四路红外避障小车

    程序是自己写的,还算可以吧。还自己画了个arduino pro mini的小型扩展板,方便接线。

  1. /*****************************  智能避障小车 By LiCJ  **************************/
  2.             
  3. int left_moto2 = 2;//left 左
  4. int left_moto4 = 4;
  5. int right_moto7 = 7;//right 右
  6. int right_moto8 = 8;
  7. int pwmleft=5; // pwm调速左
  8. int pwmright=3; // pwm调速右
  9. int Rspeed=66;
  10. int BZQL;  //避障前左 13
  11. int BZQR;  //避障前右 12
  12. int BZL;  //避障左    11
  13. int BZR;  //避障右    10
  14. int a=0;

  15. void setup()
  16. {
  17. /******************************  电机输出口   ***********************************/
  18.   pinMode(left_moto2,1);  //端口2输出
  19.   pinMode(left_moto4,1);  
  20.   pinMode(right_moto7,1);
  21.   pinMode(right_moto8,1);
  22.   pinMode(pwmleft,1);
  23.   pinMode(pwmright,1);

  24. /*******************************    红外避障读取口    **************************/
  25.   pinMode(13,INPUT);    //端口13输入,红外传感器输入口
  26.   pinMode(12,INPUT);
  27.   pinMode(11,INPUT);
  28.   pinMode(10,INPUT);
  29. }

  30. /*******************************    电机输出模式    *****************************/
  31. void qian() //前进
  32. {
  33.   digitalWrite(right_moto7,1);  //数字端口7输出高电平
  34.   digitalWrite(right_moto8,0);  
  35.   digitalWrite(left_moto2,1);
  36.   digitalWrite(left_moto4,0);
  37.   analogWrite(pwmleft,80);
  38.   analogWrite(pwmright,Rspeed);
  39. }
  40. void hou()  //后退
  41. {
  42.   digitalWrite(right_moto7,0);
  43.   digitalWrite(right_moto8,1);
  44.   digitalWrite(left_moto2,0);
  45.   digitalWrite(left_moto4,1);
  46.   analogWrite(pwmleft,80);
  47.   analogWrite(pwmright,Rspeed);
  48. }
  49. void zuo()  //左
  50. {
  51.     digitalWrite(left_moto2,1);
  52.     digitalWrite(left_moto4,0);
  53.     digitalWrite(right_moto7,0);
  54.     digitalWrite(right_moto8,0);
  55.     analogWrite(pwmleft,0);
  56.     analogWrite(pwmright,255);
  57. }
  58. void you() //右
  59. {
  60.    digitalWrite(left_moto2,0);
  61.    digitalWrite(left_moto4,0);
  62.    digitalWrite(right_moto7,1);
  63.    digitalWrite(right_moto8,0);
  64.    analogWrite(pwmleft,255);
  65.    analogWrite(pwmright,0);
  66. }
  67. void yuanzuo()//原地左转
  68. {
  69.    digitalWrite(right_moto7,0);
  70.    digitalWrite(right_moto8,1);
  71.    digitalWrite(left_moto2,1);
  72.    digitalWrite(left_moto4,0);
  73.    analogWrite(pwmleft,255);
  74.    analogWrite(pwmright,225);
  75. }
  76. void yuanyou() //原地右转
  77. {
  78.    digitalWrite(right_moto7,1);
  79.    digitalWrite(right_moto8,0);
  80.    digitalWrite(left_moto2,0);
  81.    digitalWrite(left_moto4,1);
  82.    analogWrite(pwmleft,255);
  83.    analogWrite(pwmright,225);
  84. }
  85. void ting()  // 停
  86. {
  87.     digitalWrite(right_moto7,0);
  88.    digitalWrite(right_moto8,0);
  89.    digitalWrite(left_moto2,0);
  90.    digitalWrite(left_moto4,0);
  91.    analogWrite(pwmleft,0);
  92.    analogWrite(pwmright,0);
  93. }

  94. void loop()
  95. {
  96.       for(int b=0;b=10000;b--)
  97.       {
  98.         BZQL=digitalRead(13);BZQR=digitalRead(12);//读取前面的避障模块状态
  99.         BZL=digitalRead(11);BZR=digitalRead(10);//读取侧面的避障模块状态
  100.         
  101. /*******************************  前 进  *************************************/
  102.         if(BZQL==HIGH&&BZQR==HIGH&&BZL==HIGH&&BZR==HIGH)
  103.         {
  104.           qian();
  105.         }
  106.         if(BZQL==HIGH&&BZQR==HIGH&&BZL==LOW&&BZR==LOW)
  107.         {
  108.           qian();
  109.         }
  110.         
  111. /*****************************  一个避障传感器的状态  ************************/
  112.         if(BZQL==LOW&&BZQR==HIGH&&BZL==HIGH&&BZR==HIGH)
  113.         {
  114.           yuanyou();
  115.         }
  116.         if(BZQL==HIGH&&BZQR==LOW&&BZL==HIGH&&BZR==HIGH)
  117.         {
  118.           yuanyou();
  119.         }        
  120.         if(BZQL==HIGH&&BZQR==HIGH&&BZL==LOW&&BZR==HIGH)
  121.         {
  122.           yuanyou();
  123.         }        
  124.         if(BZQL==HIGH&&BZQR==HIGH&&BZL==HIGH&&BZR==LOW)
  125.         {
  126.           yuanzuo();
  127.         }
  128. /***************************  两个避障传感器的状态  *************************/  

  129.         
  130.         if(BZQL==LOW&&BZQR==HIGH&&BZL==LOW&&BZR==HIGH)
  131.         {
  132.           yuanyou();
  133.         }
  134.          if(BZQL==HIGH&&BZQR==LOW&&BZL==HIGH&&BZR==LOW)
  135.         {
  136.           yuanzuo();
  137.         }
  138.         /*********************************************/
  139.         if(BZQL==LOW&&BZQR==HIGH&&BZL==HIGH&&BZR==LOW)
  140.         {
  141.           hou();
  142.           delay(400);
  143.           yuanzuo();
  144.           a+=1;
  145.         }
  146.         if(BZQL==LOW&&BZQR==LOW&&BZL==HIGH&&BZR==HIGH&&a>0)
  147.         {
  148.           a=0;
  149.           hou();
  150.           delay(300);
  151.           yuanzuo();
  152.         }
  153.         /************************************************/
  154.         if(BZQL==HIGH&&BZQR==LOW&&BZL==LOW&&BZR==HIGH)
  155.         {
  156.           a=0;
  157.           hou();
  158.           delay(400);
  159.           you();
  160.         }
  161.         if(BZQL==LOW&&BZQR==LOW&&BZL==HIGH&&BZR==HIGH&&a==0)
  162.         {
  163.           hou();
  164.           delay(300);
  165.           yuanyou();
  166.         }
  167.    
  168. /***************************  三个避障传感器的状态  *************************/  
  169.         if(BZQL==LOW&&BZQR==LOW&&BZL==LOW&&BZR==HIGH)
  170.         {
  171.           hou();
  172.           delay(300);
  173.           you();
  174.         }
  175.         if(BZQL==HIGH&&BZQR==LOW&&BZL==LOW&&BZR==LOW)
  176.         {
  177.           hou();
  178.           delay(300);
  179.           zuo();
  180.         }
  181.         if(BZQL==LOW&&BZQR==HIGH&&BZL==LOW&&BZR==LOW)
  182.         {
  183.           hou();
  184.           delay(300);
  185.           you();
  186.         }
  187.         if(BZQL==LOW&&BZQR==LOW&&BZL==HIGH&&BZR==LOW)
  188.         {
  189.           hou();
  190.           delay(300);
  191.           zuo();
  192.         }
  193. /***************************  四个避障传感器的状态  *************************/  
  194.         if(BZQL==LOW&&BZQR==LOW&&BZL==LOW&&BZR==LOW)
  195.         {
  196.           hou();
  197.         }
  198.   }
  199. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-5-10 20:47:34 | 显示全部楼层
学校电子科技社团的工作室,读大学就是好啊,图片上的不是我哦

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-5-10 22:13:04 | 显示全部楼层
int a=0有什么作用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-11 08:43:27 来自手机 | 显示全部楼层
Briancui 发表于 2015-5-10 22:13
int a=0有什么作用

用来判断前进遇到障碍物是往左转还是右转,不过有先决条件。
回复 支持 反对

使用道具 举报

发表于 2015-6-9 18:52:25 | 显示全部楼层
好,收了看看,学习学习
回复 支持 反对

使用道具 举报

发表于 2015-10-10 00:09:38 | 显示全部楼层
可以把接线图给出来吗?
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 16:04 , Processed in 0.035883 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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