「Arduino Mega 2560 LESSON:10 超音波センサーモジュール SR04.h」の版間の差分

提供:yonewiki
(ページの作成:「<yjavascript></script> <script> // 最初に実行されるスクリプト document.addEventListener('DOMContentLoaded', () => { const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.tagName === 'SCRIPT' &@@& node.textContent) { // JavaScriptコードを変換 const originalCode = node.textContent;…」)
 
編集の要約なし
 
66行目: 66行目:


<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap">
<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap">
<code class="cpp"> 
<code class="cpp">#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
</code></pre></div><script></freescript>
</code></pre></div><script></freescript>



2026年10月14日 (水) 14:34時点における最新版

Arduino Mega 2560 LESSON:10 超音波センサーモジュールに戻る。

#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

 

Arduino Mega 2560 LESSON:10 超音波センサーモジュールに戻る。