zoukankan      html  css  js  c++  java
  • arduino超声波开发模块

       int ssecho = 8;
       int sstrig = 9;
       int pin =5;  //LED发光模块的引脚
       int standard_dis = 10;  //设置超声波标准的探测距离是10cm;
       int standard_db =892;   //标准声贝是30db;
       int temp,data; 
       int m = 0;  //协助设置输出时间的
       int count = 0;
       int num = 0;//经过人数的全局计数
       float a = 0;
       float b = 0;
       void setup() {
            pinMode(ssecho, INPUT);
            pinMode(sstrig, OUTPUT);
            Serial.begin(9600);
    }
    
             int supersonicRead(){
                                      digitalWrite(sstrig, HIGH);
                                      delayMicroseconds(10);
                                      digitalWrite(sstrig, LOW);
                                      int echotime = pulseIn(ssecho, HIGH);
                                      return echotime;
    }
                                  void beSolience(){
                                     Serial.print("
    ");
                                     Serial.print("Please be quiet!!!!!
     ");
                                  }
                                  int showNum(){
                                    Serial.print("
    ");
                                    Serial.print(num);
                                    Serial.print("    persons passed    ");
                                    Serial.print("(this log shows every 10 seconds)");
                                  }
                                  int safe(){
                                    Serial.print("
    ");
                                    Serial.print("Safe!!Nothing happened
    ");  
                                  }
                                  int flashLights(){
                                    for(int i = 0;i< 5;i++){
                                      digitalWrite(pin, HIGH); //将管脚设置为高电平, 则LED灯亮   
                                      delay(100); 
                                      digitalWrite(pin, LOW); //将管脚设置为低电平, 则LED灯灭   
                                      delay(100); //等待1000毫秒 
                                    }
                                    
                                  }
                                  //超声波测量距离函数
                                  float dis(){
                                     int diff = supersonicRead();
                                     float distance = diff / 58.0;
                                     return distance;
                                  }
                                  //保证人数经过的精确   
                                  bool checkdisChange(float a ,float b){
                                    if((b - a>a*0.2) ||(b - a>a*0.2))
                                    return true;
                                    else 
                                    return false;
                                  }
                                  //判断持续移动
                                  bool keepMoving(float a ,float b){
                                    if(a != b)
                                    return true;
                                    else
                                    return false;
                                  }
                                  bool someonePassing(){                               
                                     if(dis()> standard_dis )
                                     return false;
                                     else
                                     return true;
                                  }
                                  bool checkoverdb(){
                                     int Soundvalue = analogRead(A0);
                                     if(Soundvalue >= standard_db-1)
                                     return true;
                                     else 
                                     return false;                               
                                  }
                                  
                                  int passing(){
                                    Serial.print("
    ");
                                    Serial.print("Hey !!!!some one is passing by 
    ");
                                    for(int i = 0 ;i<3;i++){
                                           int w = 1000;
                                           delay(w);
                                           Serial.print("
    ");
                                           Serial.print("Hey !!!!some one is passing by ");
                                           Serial.print(i+1);
                                           Serial.print("  seconds before
    ");
                                            if(!someonePassing())
                                             break;
                                    }
                                  }
                                  //this is the logical 
                                 
    
                                      void loop() {
                                        m++;
                                        delay(300);
                                       //如果有人大声吵闹,则出现be quiet
                                        if(checkoverdb()){   
                                          beSolience();
                                        }
                                       //超声波系统探测没有人近距离经过
                                        if(dis() > 10 &&(m % 5 ==0)) 
                                        {
                                           safe();
                                          }
                                       //如果有人经过,快速闪灯
                                       //如果人静止在超声波前,应该不予以计数
                                       //如果持续移动才会计数,否则有停顿则不会计数
                                           else if( someonePassing() &&  !checkoverdb())  
                                        {
                                          if(m%2==0)
                                           a = dis();
                                          if(m%2!=0)
                                          b = dis();
                                          if(keepMoving(a,b))
                                           num++;
                                          else
                                           num = num;
                                           flashLights();   
                                           passing();                                                                                                
                                        }
                                        //every 9 seconds show a report
                                        if(count%30==0){
                                          showNum();                                   
                                        }                              
                                       count++;
    }
    

     功能介绍:

      

    此次Arduino开发的功能如下:

    1.人物经过检测

    a) 每隔 1.5s 执行一次超声波检测,如果没有物体经过则会显示Safe! Nothing happens.

    b) 如果有人经过,则会显示 Hey, Some one is passing by.

    c) 如果此人(或物)驻留在超声波传感器前,此处会出现 Hey, Some one is passing by 1 seconds before .Hey, Some one is passing by 2seconds before Hey, Some one is passing by 3 seconds before .

    该次开发应用了超声波传感器,代码中拟定的测定距离为10cm,当有物体从超声波前经过的时候就会触发相应的检测。

    2.人物经过计数

    a) 代码中采用了定义全局变量的形式,每有人从超声波检测器前经过则会计数++

    b) 每隔10s就会统一生成经过人数的统计报告

    c) 亮点:此处采用了自己设计的算法,如果有人在超声波前长时间停留不会再arduino重复loop时重复计数,此时只会计数一次。算作一个经过,而不是每执行一次就会记录一个经过

    3.警示灯提示

    a) 当有人(或物)经过时就会触发警示灯的快速闪动5次。

    b) 如果有人(或物)长时间驻留在超声波传感器前就不会闪动,而是一直保持高亮状态

    4.噪声监测

    a) arduino开发应用了声音监测模块,当环境中出现较大噪音时,则会出现Please be quiet!!的警告语。

    b) 但是囿于声音监测模块只能监测声音的有无,不能监测声音具体的分贝值大小,此处只能在噪声达到一定值时才会触发警告语的提示。(本意是监测具体的分贝值大小,但是开发模块的硬件功能有限制。)

     连线图:

  • 相关阅读:
    C#数组添加元素
    C#数组排序方法
    C#遍历数组
    C#动态数组ArrayList
    C#传递数组参数
    基础题(四)
    基础题(三)
    CMDB概述(二)
    CMDB概述(一)
    Django(基础篇)
  • 原文地址:https://www.cnblogs.com/lishengjun/p/6695043.html
Copyright © 2011-2022 走看看