流程图



硬件

mega2560









  1. // Pin 13 has an LED connected on most Arduino boards.
  2. // give it a name:
  3. #include<string.h>
  4. //变量IO口定义
  5. int a;
  6. int a1=-1;
  7. int a2=-1;
  8. int a3=-1;
  9. char a11;
  10. char a22;
  11. char a33;
  12. int out13 = 13;
  13. int in12 = 12;
  14. int out1 = 11;
  15. int out2 = 10;
  16. int out3 = 9;
  17. int out4 = 8;
  18. int echo = 7;
  19. int trig = 6;
  20. int in1 = 5;
  21. int in2 = 4;
  22. int busy = 3;//喇叭状态
  23. // the setup routine runs once when you press reset:
  24. //启动 定义引脚输入输出
  25. void setup() {
  26. // initialize the digital pin as an output.
  27. pinMode(out1, OUTPUT);
  28. pinMode(out2, OUTPUT);
  29. pinMode(out3, OUTPUT);
  30. pinMode(out4, OUTPUT);
  31. pinMode(in1, INPUT);
  32. pinMode(in2, INPUT);
  33. pinMode(in12, INPUT);
  34. pinMode(out13, OUTPUT);
  35. pinMode(trig, OUTPUT);
  36. pinMode(echo, INPUT);
  37. pinMode(busy,INPUT); //初始化串口
  38. do Serial3.begin(9600);while(!Serial3);
  39. Serial.begin(9600); //置 0
  40. }
  41. /*
  42. 超声波模块
  43. */
  44. int chaoshengbo(){
  45. unsigned long time = 0;
  46. int time2 = 0;
  47. digitalWrite(trig, LOW);
  48. delay(1);
  49. digitalWrite(trig, HIGH);
  50. delayMicroseconds(20);
  51. digitalWrite(trig, LOW);
  52. time = pulseIn(echo,HIGH);
  53. time2 = time/58;
  54. // Serial.println(time/58);
  55. delay(300);
  56. return time2;
  57. }
  58. /*
  59. 文本转语音模块
  60. */
  61. void laba() {
  62. int err=0;
  63. int i=0;
  64. if(a>=100)a11=0x30+a1;
  65. a22=0x30+a2;
  66. a33=0x30+a3;
  67. char data[] = {0xc7,0xb0,0xb7,0xbd,a11,a22,a33,0xc0,0xe5,0xc3,0xd7};
  68. char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
  69. tou[2] = 3+sizeof(data);
  70. for(i=0;i<sizeof(tou);i++) {
  71. Serial3.write(tou[i]);
  72. err^=tou[i];
  73. }
  74. for(i= 0;i<sizeof(data);i++) {
  75. Serial3.write(data[i]);
  76. err^=data[i];
  77. }
  78. Serial3.write(err);
  79. Serial.println(Serial3.read());
  80. }
  81. /*
  82. 左转语音
  83. */
  84. void laba1() {
  85. int err=0;
  86. int i=0;
  87. char data[] = {0xD7,0xF3,0xD7,0xAA};
  88. char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
  89. tou[2] = 3+sizeof(data);
  90. for(i=0;i<sizeof(tou);i++) {
  91. Serial3.write(tou[i]);
  92. err^=tou[i];
  93. }
  94. for(i= 0;i<sizeof(data);i++) {
  95. Serial3.write(data[i]);
  96. err^=data[i];
  97. }
  98. Serial3.write(err);
  99. Serial.println(Serial3.read());
  100. }
  101. /*
  102. 右转语音
  103. */
  104. void laba2() {
  105. int err=0;
  106. int i=0;
  107. char data[] = {0xD3,0xD2,0xD7,0xAA};
  108. char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
  109. tou[2] = 3+sizeof(data);
  110. for(i=0;i<sizeof(tou);i++) {
  111. Serial3.write(tou[i]);
  112. err^=tou[i];
  113. }
  114. for(i= 0;i<sizeof(data);i++) {
  115. Serial3.write(data[i]);
  116. err^=data[i];
  117. }
  118. Serial3.write(err);
  119. Serial.println(Serial3.read());
  120. }
  121. /*
  122. 循环执行功能
  123. */
  124. // the loop routine runs over and over again forever:
  125. void loop() {
  126. //Serial.println(time);
  127. static int b,c = 0;
  128. a = chaoshengbo();
  129. a1=a/100;
  130. if(a<100)a2=a/10;
  131. else a2=a%100/10;
  132. a3=a%10;
  133. // Serial.println(a);
  134. if(a<=15.00) {c++;}
  135. else {c=0;}
  136. // Serial.println(c);
  137. if(2==c) {
  138. c=0;
  139. digitalWrite(out1, HIGH); // turn the LED on (HIGH is the voltage level)
  140. digitalWrite(out2, LOW); // turn the LED off by making the voltage LOW
  141. digitalWrite(out3, LOW); // turn the LED on (HIGH is the voltage level)
  142. digitalWrite(out4, HIGH); // turn the LED off by making the voltage LOW
  143. delay(1000);
  144. }
  145. if( digitalRead(busy)==0 ) laba();
  146. digitalWrite(out1, HIGH); // turn the LED on (HIGH is the voltage level)
  147. digitalWrite(out2, LOW); // turn the LED off by making the voltage LOW
  148. digitalWrite(out3, HIGH); // turn the LED on (HIGH is the voltage level)
  149. digitalWrite(out4, LOW); // turn the LED off by making the voltage LOW
  150. digitalWrite(out1, HIGH);//you cuo wu
  151. while(digitalRead(in1)==0) {
  152. laba2();
  153. while(digitalRead(in1)==0) {
  154. digitalWrite(out1, HIGH); // turn the LED on (HIGH is the voltage level)
  155. digitalWrite(out2, LOW); // turn the LED off by making the voltage LOW
  156. digitalWrite(out3, LOW); // turn the LED on (HIGH is the voltage level)
  157. digitalWrite(out4, HIGH); // turn the LED off by making the voltage LOW
  158. }
  159. digitalWrite(out2, LOW); // turn the LED on (HIGH is the voltage level)
  160. digitalWrite(out1, LOW); // turn the LED off by making the voltage LOW
  161. digitalWrite(out4, LOW); // turn the LED on (HIGH is the voltage level)
  162. digitalWrite(out3, LOW); // turn the LED off by making the voltage LOW
  163. }
  164. while(digitalRead(in2)==0) {
  165. laba1();
  166. while(digitalRead(in2)==0) {
  167. digitalWrite(out2, HIGH); // turn the LED on (HIGH is the voltage level)
  168. digitalWrite(out1, LOW); // turn the LED off by making the voltage LOW
  169. digitalWrite(out4, LOW); // turn the LED on (HIGH is the voltage level)
  170. digitalWrite(out3, HIGH); // turn the LED off by making the voltage LOW
  171. }
  172. digitalWrite(out2, LOW); // turn the LED on (HIGH is the voltage level)
  173. digitalWrite(out1, LOW); // turn the LED off by making the voltage LOW
  174. digitalWrite(out4, LOW); // turn the LED on (HIGH is the voltage level)
  175. digitalWrite(out3, LOW); // turn the LED off by making the voltage LOW
  176. }
  177. }

arduino mega 避障报距小车的更多相关文章

  1. 具有避障和寻线功能的Arduino小车

    标签:  Arduino  乐高  机器人 创客对于成年人来说,多半是科技娱乐,或者是一种是一种向往科技的人生态度,总是希望自己不仅可以看到或者听到科技的资讯,还希望能够亲身制作科技玩意,从而更好地体 ...

  2. 【雕爷学编程】Arduino动手做(45)---红外避障传感器

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  3. [TPYBoard - Micropython 之会python就能做硬件 9] 五分种学会用TPYBoard V102 制作避障小车(升级版)

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 感谢山东萝卜电子科技公司授权   一.实验器材 1.TPYboard V102板 ...

  4. 智能循迹避障小车&抢答器

    智能循迹避障小车 →视频链接← 抢答器

  5. [TPYBoard - Micropython之会python就能做硬件 8] 学习使用超声波模块制作避障小车

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604   一.实验器材 1.TPYboard V102板  一块 2.电机驱动模块L2 ...

  6. SLAM+语音机器人DIY系列:(六)SLAM建图与自主避障导航——2.google-cartographer机器人SLAM建图

    摘要 通过前面的基础学习,本章进入最为激动的机器人自主导航的学习.在前面的学习铺垫后,终于迎来了最大乐趣的时刻,就是赋予我们的miiboo机器人能自由行走的生命.本章将围绕机器人SLAM建图.导航避障 ...

  7. win7下Arduino Mega 2560驱动安装失败解决办法

    因为玩四轴用的apm的飞控板,而其需要安装此驱动,曾经在win8使用其,但是因为win8有相对应的数字证书保护措施(应该是这样的,因为好久了记不清楚了),以至于我每次都需要长按shift重启电脑关闭此 ...

  8. Bug避障算法简介

    移动机器人智能的一个重要标志就是自主导航,而实现机器人自主导航有个基本要求--避障.避障是指移动机器人根据采集的障碍物的状态信息,在行走过程中通过传感器感知到妨碍其通行的静态和动态物体时,按照一定的方 ...

  9. 机器人局部避障的动态窗口法(dynamic window approach) (转)

    源:机器人局部避障的动态窗口法(dynamic window approach) 首先在V_m∩V_d的范围内采样速度: allowable_v = generateWindow(robotV, ro ...

随机推荐

  1. struts2文件上传1

    <form action="hello/UploadAction_upload.action" enctype="multipart/form-data" ...

  2. HNOI2019 简要题解

    HNOI 2019 简要题解 没想到自己竟也能有机会写下这篇题解呢. LOJ Luogu Day1T1 鱼 枚举\(AD\)两点后发现\(BC\)与\(EF\)相对独立,因此只需要计算合法的\(BC\ ...

  3. python——psutil的使用(获取进程信息)

    import psutil psutil.pids() [1, 2, 3, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 26, 27, 28, 2 ...

  4. skipper prometheus 监控

    skipper 是支持prometheus监控的,只是没有启用,需要添加参数 -enable-prometheus-metrics 测试使用的是一个简单nginx web ,同时使用docker-co ...

  5. scikit-learn数据集下载太慢的问题

    有时候用scikit-learn在线下载数据时太慢,因为网络或者其他原因,这时候我们可以先把数据集下载到本地,然后再把这个数据集放到scikit-learn的data中,首先我们需要找到 scikit ...

  6. 数学沉思录:古今数学思想的发展与演变 (Mario Livio 著)

    第1章 神秘的数学 (已看) 发现还是发明 第2章 神秘学:命理学家和哲学家 (已看) 毕达哥拉斯 进入柏拉图的洞穴 第3章 魔法师:大师和异端 (已看) 给我一个支点:我将撬起地球 阿基米德重写稿 ...

  7. Wireshark常用过滤使用方法

    过滤源ip.目的ip. 在wireshark的过滤规则框Filter中输入过滤条件.如查找目的地址为192.168.101.8的包,ip.dst==192.168.101.8:查找源地址为ip.src ...

  8. 漫谈 C++ 的 内存堆 实现原理

    如果我来设计 C++ 的 内存堆 , 我会这样设计 : 进程 首先会跟 操作系统 要 一块大内存区域 , 我称之为 Division , 简称 div . 然后 , 将这块 div 作为 堆 , 就可 ...

  9. Spring技术内幕总结 - IoC容器的实现

    IoC:Inversion of Control,控制反转,即依赖对象的获得被反转了(DI:dependency inversion,依赖注入)在Spring中,IoC容器是实现这个模式的载体.它可以 ...

  10. spring-AOP框架(基于配置文件的方式配置AOP)

    .xml: ref-指向,order-指定优先级