「Arduino Mega 2560 LESSON:10 超音波センサーモジュール SR04.cpp」の版間の差分
(ページの作成:「<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"> | ||
#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; | |||
} | |||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
2026年10月14日 (水) 14:35時点における最新版
Arduino Mega 2560 LESSON:10 超音波センサーモジュールに戻る。
#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;
}