Arduino+传感器系列之超声测距传感器【Arduino库函数编写例子】

11 篇文章 41 订阅

Arduino 和 HC-SR04 超声波传感器 测距
https://blog.csdn.net/ling3ye/article/details/51407328
这个例子很详细的解释了传感器的使用方法,很棒。

超声波测距的两种模式****US-100
http://www.i-makers.info/blog/1825.html

网上找了一个超声外部库,学习怎么写Arduino的库函数下载地址https://download.csdn.net/download/y511374875/10304407

头文件

#ifndef SR04_H
#define SR04_H

#if defined(ARDUINO) && ARDUINO >= 100
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif
//#include "pins_arduino.h"

#include <inttypes.h>

#define PULSE_TIMEOUT 150000L   // 100ms
#define DEFAULT_DELAY 10
#define DEFAULT_PINGS 5
class SR04 {public:

    /**
    * Constructor
    * Ultrasonic sensor SR04, four connections pins
    * VCC, ECHO, TRIGGER, GND
    * <br>
    * \param echoPin digital INPUT-Pin for measuring distance
    * \param triggerPin if 10us high a trigger signal is generated from 
    *                   SR04
    *
    * \return void
    */
    SR04(int echoPin, int triggerPin);

    /**
    * Do a measurment for this sensor. Return distance as long
    * in centimenter
    * \return long distance in centimeter
    */
    long Distance();

    /**
    * Do count measurents and calculate the average. 
    * To avoid defilement from ow/high peaks, min/max values
    * are substracted from the average
    *
    * \param wait delay between measurements, default = DEFAULT_DELAY/ms
    * \param count number of measurements, default DEFAULT_PINGS
    * \return long distance in centimeter
    **/
    long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS);

    /**
    * Do only a ping. Get result with methode getDistance()
    * 
    * \param keine
    */
    void Ping() ;

    /**
    * return latest distance. Methode Ping() should be called before
    * \param keine
    * \return Distanz in Zentimeter
    */
    long getDistance();


private:
    /**
    * Do the measurement calculation and return result in centimeter
    * SR04 measure echo time to obstacle and return way. 
    * <br>
    * Sound travels with 340m/sec
    * <br>
    * Example: Obstace 100cm away from SR04. Measure time is 100cm to
    * obstacle and 100cm return = 200cm
    * <br>
    * 1sec = 1000ms = 1.000.000uS
    * 1.000.000 / 340 = Distance in microseconds for 100cm
    * 2941uS fuer 100cm = 5882 uS fuer 200cm
    *
    * duration / 5882 * 100 = distance in cm
    */  
    long MicrosecondsToCentimeter(long duration);

    long _currentDistance;
    int _echoPin, _triggerPin;
    long _duration, _distance;
    bool _autoMode;
};
#endif

CPP文件


#include "SR04.h"

SR04::SR04(int echoPin, int triggerPin) {
    _echoPin = echoPin;
    _triggerPin = triggerPin;
    pinMode(_echoPin, INPUT);
    pinMode(_triggerPin, OUTPUT);
    _autoMode = false;
    _distance = 999;
}


long SR04::Distance() {
    long d = 0;
    _duration = 0;
    digitalWrite(_triggerPin, LOW);
    delayMicroseconds(2);
    digitalWrite(_triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(_triggerPin, LOW);
    delayMicroseconds(2);
    _duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT);
    d = MicrosecondsToCentimeter(_duration);
    delay(25);
    return d;
}

long SR04::DistanceAvg(int wait, int count) {
    long min, max, avg, d;
    min = 999;
    max = 0;
    avg = d = 0;

    if (wait < 25) {
        wait = 25;
    }

    if (count < 1) {
        count = 1;
    }

    for (int x = 0; x < count + 2; x++) {
        d = Distance();

        if (d < min) {
            min = d;
        }

        if (d > max) {
            max = d;
        }

        avg += d;
    }

    // substract highest and lowest value
    avg -= (max + min);
    // calculate average
    avg /= count;
    return avg;
}

void SR04::Ping() {
    _distance = Distance();
}

long SR04::getDistance() {
    return _distance;
}

long SR04::MicrosecondsToCentimeter(long duration) {
    long d = (duration * 100) / 5882;
    //d = (d == 0)?999:d;
    return d;
}

例子:

#include "SR04.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
   Serial.begin(9600);
   Serial.println("Example written by Coloz From Arduin.CN");
   delay(1000);
}

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(1000);
}

可以借鉴怎么写Arduino的库函数。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值