标题: arduino编程之超声波智能交通灯 [打印本页]

作者: 束缚111    时间: 2017-7-18 20:40
标题: arduino编程之超声波智能交通灯
通过arduino IDE写的一个利用超声波检测是否有人的一个智能交通灯程序
资料下载:
智能交通灯.7z (1.26 KB, 下载次数: 23)

  1. int T = 2;
  2. int E = 3;

  3. int RedPin2=13;
  4. int YellowPin2=12;
  5. int GreenPin2=11;
  6. int RedPin1=10;
  7. int YellowPin1=9;
  8. int GreenPin1=8;


  9. float cm;
  10. int i=0;

  11. void setup()
  12. {
  13.   pinMode(RedPin1,OUTPUT);
  14.   pinMode(YellowPin1,OUTPUT);
  15.   pinMode(GreenPin1,OUTPUT);  
  16.   pinMode(RedPin2,OUTPUT);
  17.   pinMode(YellowPin2,OUTPUT);
  18.   pinMode(GreenPin2,OUTPUT);
  19.   
  20.   Serial.begin(9600);
  21.   pinMode(T, OUTPUT);
  22.   pinMode(E, INPUT);
  23. }
  24. void loop()
  25. {
  26.   digitalWrite(RedPin1,HIGH);
  27.   digitalWrite(GreenPin2,HIGH);

  28.   digitalWrite(T, LOW); //低高低电平发一个短时间脉冲去TrigPin
  29.   delayMicroseconds(2);
  30.   digitalWrite(T, HIGH);
  31.   delayMicroseconds(10);
  32.   digitalWrite(T, LOW);
  33.   cm = pulseIn(E, HIGH) / 58.0; //将回波时间换算成cm
  34.   cm = (int(cm * 100.0)) / 100.0; //保留两位小数
  35.   Serial.print(cm);
  36.   Serial.print("cm");
  37.   Serial.println();
  38.   delay(1000);

  39.   if(cm>19 && cm<24)           //二号路
  40.   {
  41.       digitalWrite(RedPin1,HIGH);
  42.       digitalWrite(GreenPin2,HIGH);
  43.   }
  44.   
  45.   else if(12.5<cm && cm<32)    //一号路
  46.   {
  47.             if(i==0)
  48.             {
  49.               for(i=0;i<1;i++)
  50.               {
  51.                 digitalWrite(GreenPin2,LOW);
  52.                 digitalWrite(YellowPin2,HIGH);
  53.                 delay(2000);
  54.                 digitalWrite(YellowPin2,LOW);
  55.               }
  56.             }
  57.             
  58.             while(i)
  59.             {
  60.               digitalWrite(RedPin1,LOW);
  61.               digitalWrite(RedPin2,HIGH);
  62.               digitalWrite(GreenPin1,HIGH);
  63.               digitalWrite(GreenPin2,LOW);

  64.               digitalWrite(T, LOW); //低高低电平发一个短时间脉冲去TrigPin
  65.               delayMicroseconds(2);
  66.               digitalWrite(T, HIGH);
  67.               delayMicroseconds(10);
  68.               digitalWrite(T, LOW);
  69.               cm = pulseIn(E, HIGH) / 58.0; //将回波时间换算成cm
  70.               cm = (int(cm * 100.0)) / 100.0; //保留两位小数
  71.               Serial.print(cm);
  72.               Serial.print("cm");
  73.               Serial.println();
  74.               delay(1000);

  75.               if(cm<12.5 || cm>32)
  76.               {
  77. ……………………

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



作者: casanava    时间: 2017-7-28 09:07
兄台,请讲清楚一点,比如,超声波发射器离地面多高?
作者: lemon0210    时间: 2017-9-27 10:32
功能介绍能否更详细一点呢
作者: s5b690    时间: 2017-10-4 17:58
新人加入,下载学习.




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1