「Arduino Mega 2560 LESSON:14 赤外線受信モジュール」の版間の差分
(ページの作成:「<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"> | |||
<code class="cpp">//www.elegoo.com | |||
//2023.05.05 | |||
#include "IRremote.h" | |||
int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11 | |||
/*-----( Declare objects )-----*/ | |||
IRrecv irrecv(receiver); // create instance of 'irrecv' | |||
//vairable uses to store the last decodedRawData | |||
uint32_t last_decodedRawData = 0; | |||
/*-----( Function )-----*/ | |||
void translateIR() // takes action based on IR code received | |||
{ | |||
// 受信したプロトコル名を表示 | |||
Serial.print("Protocol: "); | |||
Serial.println(getProtocolString(irrecv.decodedIRData.protocol)); | |||
// Check if it is a repeat IR code | |||
if (irrecv.decodedIRData.flags) | |||
{ | |||
//set the current decodedRawData to the last decodedRawData | |||
irrecv.decodedIRData.decodedRawData = last_decodedRawData; | |||
Serial.println("REPEAT!"); | |||
} else | |||
{ | |||
//output the IR code on the serial monitor | |||
Serial.print("IR code:0x"); | |||
Serial.println(irrecv.decodedIRData.decodedRawData, HEX); | |||
} | |||
//map the IR code to the remote key | |||
switch (irrecv.decodedIRData.decodedRawData) | |||
{ | |||
//標準NECプロトコル | |||
case 0xBA45FF00: Serial.println("POWER"); break; | |||
case 0xB847FF00: Serial.println("FUNC/STOP"); break; | |||
case 0xB946FF00: Serial.println("VOL+"); break; | |||
case 0xBB44FF00: Serial.println("FAST BACK"); break; | |||
case 0xBF40FF00: Serial.println("PAUSE"); break; | |||
case 0xBC43FF00: Serial.println("FAST FORWARD"); break; | |||
case 0xF807FF00: Serial.println("DOWN"); break; | |||
case 0xEA15FF00: Serial.println("VOL-"); break; | |||
case 0xF609FF00: Serial.println("UP"); break; | |||
case 0xE619FF00: Serial.println("EQ"); break; | |||
case 0xF20DFF00: Serial.println("ST/REPT"); break; | |||
case 0xE916FF00: Serial.println("0"); break; | |||
case 0xF30CFF00: Serial.println("1"); break; | |||
case 0xE718FF00: Serial.println("2"); break; | |||
case 0xA15EFF00: Serial.println("3"); break; | |||
case 0xF708FF00: Serial.println("4"); break; | |||
case 0xE31CFF00: Serial.println("5"); break; | |||
case 0xA55AFF00: Serial.println("6"); break; | |||
case 0xBD42FF00: Serial.println("7"); break; | |||
case 0xAD52FF00: Serial.println("8"); break; | |||
case 0xB54AFF00: Serial.println("9"); break; | |||
default: | |||
Serial.println(" other button "); | |||
}// End Case | |||
//store the last decodedRawData | |||
last_decodedRawData = irrecv.decodedIRData.decodedRawData; | |||
delay(500); // Do not get immediate repeat | |||
} //END translateIR | |||
void setup() /*----( SETUP: RUNS ONCE )----*/ | |||
{ | |||
Serial.begin(9600); | |||
Serial.println("IR Receiver Button Decode"); | |||
irrecv.enableIRIn(); // Start the receiver | |||
}/*--(end setup )---*/ | |||
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/ | |||
{ | |||
if (irrecv.decode()) // have we received an IR signal? | |||
{ | |||
translateIR(); | |||
irrecv.resume(); // receive the next value | |||
} | |||
}/* --(end main loop )-- */ | |||
</code></pre></div><script></freescript> | |||
いろいろなリモコンコードを取得するのは以下のコード | |||
<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">#define RAW_BUFFER_LENGTH 750 | ||
#include <IRremote.hpp> | |||
const int IR_RECEIVE_PIN = 11; | |||
void setup() { | |||
Serial.begin(115200); | |||
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); | |||
Serial.println("IR Remote Analyzer"); | |||
Serial.println("TV / Recorder / Air Conditioner"); | |||
Serial.println("Toshiba / Daikin signal investigation"); | |||
} | |||
void loop() { | |||
if (IrReceiver.decode()) { | |||
Serial.println(); | |||
Serial.println("============================"); | |||
// 受信バッファが足りなかった場合 | |||
if (IrReceiver.decodedIRData.flags & | |||
IRDATA_FLAGS_WAS_OVERFLOW) { | |||
Serial.println("WARNING: Buffer overflow!"); | |||
} | |||
// ライブラリによるプロトコル解析 | |||
IrReceiver.printIRResultShort(&Serial); | |||
// 受信ビット数 | |||
Serial.print("Bits: "); | |||
Serial.println(IrReceiver.decodedIRData.numberOfBits); | |||
// リピート判定 | |||
if (IrReceiver.decodedIRData.flags & | |||
IRDATA_FLAGS_IS_REPEAT) { | |||
Serial.println("Repeat signal"); | |||
} | |||
// 生のパルスデータ | |||
Serial.println("--- Raw Timing ---"); | |||
IrReceiver.printIRResultRawFormatted(&Serial, true); | |||
Serial.println("============================"); | |||
IrReceiver.resume(); | |||
} | |||
} | |||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
2026年10月22日 (木) 00:08時点における版
概要
赤外線受信モジュールを接続するレッスンです。
完成図は以下のとおりです。
コードは以下のとおりです。
//www.elegoo.com
//2023.05.05
#include "IRremote.h"
int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
//vairable uses to store the last decodedRawData
uint32_t last_decodedRawData = 0;
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
{
// 受信したプロトコル名を表示
Serial.print("Protocol: ");
Serial.println(getProtocolString(irrecv.decodedIRData.protocol));
// Check if it is a repeat IR code
if (irrecv.decodedIRData.flags)
{
//set the current decodedRawData to the last decodedRawData
irrecv.decodedIRData.decodedRawData = last_decodedRawData;
Serial.println("REPEAT!");
} else
{
//output the IR code on the serial monitor
Serial.print("IR code:0x");
Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
}
//map the IR code to the remote key
switch (irrecv.decodedIRData.decodedRawData)
{
//標準NECプロトコル
case 0xBA45FF00: Serial.println("POWER"); break;
case 0xB847FF00: Serial.println("FUNC/STOP"); break;
case 0xB946FF00: Serial.println("VOL+"); break;
case 0xBB44FF00: Serial.println("FAST BACK"); break;
case 0xBF40FF00: Serial.println("PAUSE"); break;
case 0xBC43FF00: Serial.println("FAST FORWARD"); break;
case 0xF807FF00: Serial.println("DOWN"); break;
case 0xEA15FF00: Serial.println("VOL-"); break;
case 0xF609FF00: Serial.println("UP"); break;
case 0xE619FF00: Serial.println("EQ"); break;
case 0xF20DFF00: Serial.println("ST/REPT"); break;
case 0xE916FF00: Serial.println("0"); break;
case 0xF30CFF00: Serial.println("1"); break;
case 0xE718FF00: Serial.println("2"); break;
case 0xA15EFF00: Serial.println("3"); break;
case 0xF708FF00: Serial.println("4"); break;
case 0xE31CFF00: Serial.println("5"); break;
case 0xA55AFF00: Serial.println("6"); break;
case 0xBD42FF00: Serial.println("7"); break;
case 0xAD52FF00: Serial.println("8"); break;
case 0xB54AFF00: Serial.println("9"); break;
default:
Serial.println(" other button ");
}// End Case
//store the last decodedRawData
last_decodedRawData = irrecv.decodedIRData.decodedRawData;
delay(500); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (irrecv.decode()) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}/* --(end main loop )-- */
いろいろなリモコンコードを取得するのは以下のコード
#define RAW_BUFFER_LENGTH 750
#include <IRremote.hpp>
const int IR_RECEIVE_PIN = 11;
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR Remote Analyzer");
Serial.println("TV / Recorder / Air Conditioner");
Serial.println("Toshiba / Daikin signal investigation");
}
void loop() {
if (IrReceiver.decode()) {
Serial.println();
Serial.println("============================");
// 受信バッファが足りなかった場合
if (IrReceiver.decodedIRData.flags &
IRDATA_FLAGS_WAS_OVERFLOW) {
Serial.println("WARNING: Buffer overflow!");
}
// ライブラリによるプロトコル解析
IrReceiver.printIRResultShort(&Serial);
// 受信ビット数
Serial.print("Bits: ");
Serial.println(IrReceiver.decodedIRData.numberOfBits);
// リピート判定
if (IrReceiver.decodedIRData.flags &
IRDATA_FLAGS_IS_REPEAT) {
Serial.println("Repeat signal");
}
// 生のパルスデータ
Serial.println("--- Raw Timing ---");
IrReceiver.printIRResultRawFormatted(&Serial, true);
Serial.println("============================");
IrReceiver.resume();
}
}