1.先把IRremote函式庫資料夾放進àArduinoàlibraries底下。
2.開啟IrReceive.pde測得自己的紅外線遙控的碼(在Serial Monitor可顯示IRcode)再將IRcode記錄下來,然而到主程式裡面修改成自己的紅外線碼即可。
用程式偵測
紅外線發射器
的傳輸碼 上 下 左 右 停止 5個按鍵的Keycode
long forward = 0xFF629D;
long back = 0xFFA857;
long stop = 0xFF02FD;
long left = 0xFF22DD;
long right = 0xFFC23D;
const int irReceiverPin = 2; // 紅外線接收器訊號接在 pin 2
IRrecv irrecv(irReceiverPin); // 定義 IRrecv 物件來接收紅外線訊號
decode_results Deresults; // 解碼結果將放在 decode_results 結構的 Deresult 變數裏
void setup()
{
Serial.begin(9600); // 設定Serial port, 9600 bps
irrecv.enableIRIn(); // Enable紅外線解碼
}
void loop()
{
if (irrecv.decode(&Deresults)) { // 收到一組正確紅外線訊號
// 印到 Serial port
Serial.print("irCode: ");
Serial.print(Deresults.value, HEX); // 編碼值
Serial.print(", bits: ");
Serial.println(Deresults.bits); // 編碼位元數
irrecv.resume(); // 繼續收下一組紅外線訊號
}
}
//**********************************************************
// 遙控程式
//**********************************************************
#include <IRremote.h>
int RECV_PIN = A0;
int motorIn1=5;//設定I1Port
int motorIn2=6;//設定I2Port
int motorIn3=9;//設定I3Port
int motorIn4=10;//設定I4Port
long forward = 0xFF629D;
long back = 0xFFA857;
long stop = 0xFF02FD;
long left = 0xFF22DD;
long right = 0xFFC23D;
IRrecv irrecv(RECV_PIN);
decode_results results;
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
{
Serial.println("Could not decode message");
}
else
{
if (results->decode_type == NEC)
{
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY)
{
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5)
{
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6)
{
Serial.print("Decoded RC6: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
}
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++)
{
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else
{
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
void setup()
{
pinMode(RECV_PIN, INPUT);
pinMode( motorIn1,OUTPUT);
pinMode( motorIn2,OUTPUT);
pinMode( motorIn3,OUTPUT);
pinMode( motorIn4,OUTPUT);
digitalWrite(motorIn1, LOW); //Motor Stop
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
int on = 0;
unsigned long last = millis();
void loop()
{
if (irrecv.decode(&results))
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250)
{
on = !on;
// digitalWrite(8, on ? HIGH : LOW);
digitalWrite(13, on ? HIGH : LOW);
dump(&results);
}
if (results.value == forward )
{ digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);}
if (results.value == back )
{ digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);}
if (results.value == left )
{ digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW); }
if (results.value == right )
{ digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW); }
if (results.value == stop )
{
digitalWrite(motorIn1,HIGH);//使馬達(右)STOP
digitalWrite(motorIn2,HIGH);
digitalWrite(motorIn3,HIGH);//使馬達(左)STOP
digitalWrite(motorIn4,HIGH);
}
last = millis();
irrecv.resume(); // Receive the next value
}
}
沒有留言:
張貼留言