![]() | |
極性があります |
このたび赤外線センサーを使って遠隔操作できるよう組み直してみました。なかなか便利です。
リモコンはカメラのものを転用しました。まず、コードを取得します。 IRremote というライブラリで信号を解析します。
このライブラリが曲者で、zip ファイルを登録するわけなのですが、もとある RobotIRremote なるライブラリとぶつかるらしく、そのままではコンパイルできません。
こいつはデフォルトのライブラリなので、Program Files (x86) 下の Arduino フォルダの RobotIRremote を取り除く必要があります。もしライブラリをインストールしてしまっていれば、Arduino ドキュメントフォルダ下のローカル・ライブラリの Arduino-IRremote-master を削除し、IDE を再起動です。
ここでライブラリを再インストールすると動きます。サンプルコードの IRecvDump など使い、リモートの信号を解析し、コードを取得します。
コードを取得すればあとは条件分岐でリレーを作動させます。
回路・プログラムが正しければ「カチッ」という音がして、ランプが点灯するはずです。100Vを扱うわけなので注意が必要です。あと、赤外線センサーの足の極性はそれぞれ異なるのできちんと調べる必要があります。
さっそくブレッドボード版 Arduino で組んでみました。
電源を組んで、Atmega328P を配置して、リレーモジュールを組んで、100V 電源から線をとってきて、赤外線センサーをつなぎます。
いつもの百均ケースでぴったりです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lightOff.ino.ino -- Turn on/off light by a IR remote controller | |
// Copyright (c) 2016 easai | |
// Author: easai | |
// Created: Sun Jan 10 14:13:25 2016 | |
// Keywords: Arduino, relay, ir remote | |
// Commentary: | |
// - Relay pin 7 | |
// - IR remote pin 11 | |
// - The remote code will depend on the type of the controller | |
// | |
// Code: | |
#include <IRremote.h> | |
int relayPin = 7; | |
int RECV_PIN = 11; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
boolean relayState = false; | |
void setup() | |
{ | |
pinMode(relayPin, OUTPUT); | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
turnOff(); | |
} | |
void turnOn() | |
{ | |
digitalWrite(relayPin, LOW); | |
} | |
void turnOff() | |
{ | |
digitalWrite(relayPin, HIGH); | |
} | |
void loop() | |
{ | |
if (irrecv.decode(&results)) | |
{ | |
if (results.value == 0x22AE7A29) | |
{ | |
relayState = !relayState; | |
} | |
Serial.println(results.value, HEX); | |
irrecv.resume(); // Receive the next value | |
if (relayState) | |
{ | |
turnOff(); | |
} | |
else | |
{ | |
turnOn(); | |
} | |
delay(300); | |
} | |
} | |
// lightOff.ino.ino ends here |