「Arduino Mega 2560 LESSON:12 DHT11 温度湿度センサー」の版間の差分
(ページの作成:「<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行目: | ||
== '''概要''' == | == '''概要''' == | ||
DHT11 温度湿度センサーを接続するレッスンです。 | |||
以下のようなコードです。 | |||
<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">//www.elegoo.com | ||
//2018.10.25 | |||
#include <dht_nonblocking.h> | |||
#define DHT_SENSOR_TYPE DHT_TYPE_11 | |||
static const int DHT_SENSOR_PIN = 2; | |||
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE ); | |||
/* | |||
* Initialize the serial port. | |||
*/ | |||
void setup( ) | |||
{ | |||
Serial.begin( 9600); | |||
} | |||
/* | |||
* Poll for a measurement, keeping the state machine alive. Returns | |||
* true if a measurement is available. | |||
*/ | |||
static bool measure_environment( float *temperature, float *humidity ) | |||
{ | |||
static unsigned long measurement_timestamp = millis( ); | |||
/* Measure once every four seconds. */ | |||
if( millis( ) - measurement_timestamp > 3000ul ) | |||
{ | |||
if( dht_sensor.measure( temperature, humidity ) == true ) | |||
{ | |||
dht_sensor.print_raw_data(); | |||
measurement_timestamp = millis( ); | |||
return( true ); | |||
} | |||
} | |||
return( false ); | |||
} | |||
/* | |||
* Main program loop. | |||
*/ | |||
void loop( ) | |||
{ | |||
float temperature; | |||
float humidity; | |||
/* Measure temperature and humidity. If the functions returns | |||
true, then a measurement is available. */ | |||
if( measure_environment( &temperature, &humidity ) == true ) | |||
{ | |||
Serial.print( "T = " ); | |||
Serial.print( temperature, 1 ); | |||
Serial.print( " deg. C, H = " ); | |||
Serial.print( humidity, 1 ); | |||
Serial.println( "%" ); | |||
} | |||
} | |||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
2026年10月17日 (土) 00:09時点における版
概要
DHT11 温度湿度センサーを接続するレッスンです。
以下のようなコードです。
//www.elegoo.com
//2018.10.25
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
dht_sensor.print_raw_data();
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );
}
}