สร้างเว็บEngine by iGetWeb.com
Cart รายการสินค้า (0)

Interfacing IR detector with Arduino

Interfacing IR detector with Arduino

Interfacing IR detector with Arduino



วันนี้ นะครับเรามาทดสอบการเขียนโปรแกรมให้  arduino รับค่าจาก Remote TV หรือ remote รุ้นต่างๆ ผ่าน  IR 38 kHz

ต่อวงจรตามภาพเลบนะครับ ใช้ IR 38kHz  และ LED + R 300 E 1 ตัวก็สามารถใช้งานได้แล้วครับ

ก่อนอื่น เราต้องใส่ library  irremote.h  ใน library ของ Arduino ก่อนนะครับ สามารถหาโหดได้ทั่วไปครับ 

มาดูวงจรกันนะครับจะเห็นได้ว่าค่อนค้างงานครับ มีแค่ตัว IR และตัว LED ตัวแสดงผลให้เราทราบนะครับ

การที่เราจะรู้ว่าเมื่อกด Remote ปุ่มไหนแล้วให้ arduino ทำงานกับตัวปุ่มนั้นคือ ให้ทำการอ่านค่าจากตัว remote รุ้นนั้นๆก่อนโดยการทดสอบกด remote แล้วดูค่าทาง serial monitor 

ซึ้งใช้ code ของตัวนี้นะครับเพื่อดูค่าจากปุ้มนั้นๆ

if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);      คำสั่งนี้จะแสดงค่าจากปุ่มกดที่เรากด จาก remote แล้วแสดงออกทาง serial monitor

irrecv.resume(); // Receive the next value

}

code ด้านล่างนี้จะแสดงค่า ปุ่มที่เรากดแล้วมาแสดงบน  serial monitor แล้วให้เราจดบันทึกค่าของปุ่มนั้น เพื่อจะนำค่ามาใช้เมื่อเวลาเรากดปุ่ม

//**************************************


#include <irremote.h>

int RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the ir receiver


}

void loop() {


if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);

irrecv.resume(); // Receive the next value

}

}

//**************************************

code ด้านล่างนี้จะเป็น code ที่เรารู้ค่าของปุ่มนั้นๆ แล้วก็ตะทำให้เรา สามารถกด remote แล้วให้ LED ที่บนบอร์ด สามารถติดได้

และเมื่อกดอีกปุ่มนึงก็จะสามารถทำให้ LED ดับได้

//************************************


#include <irremote.h>

int RECV_PIN = 2;
int led1 = 13;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the ir receiver
pinMode(led1, OUTPUT);


}

void loop() {


if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);


if(results.value == 16705559) //  ตั้งค่าของปุ่มที่ต้องการให้ LED ติด
digitalWrite(led1, HIGH); // set the LED on
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
if(results.value == 16656599) //  ตั้งค่าของปุ่มที่ต้องการให้ LED ดับ
digitalWrite(led1, LOW); // set the LED off
irrecv.resume(); // Receive the next value

}

}
}

เพียงเท่านี้เราก็สามารถสั่งงานผ่าน remote TV ได้แล้วครับสามารถนำไปประยุกต์เปิดปิด พัดลม หลอดไฟ หรืออื่นๆ ตามที่เราต้องการครับ

code ตัวอย่างอื่นๆ

Example Program - Receive

 

#include <IRremote.h>

const int RECV_PIN = 6;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.decode_type == NEC) {
      Serial.print("NEC: ");
    } else if (results.decode_type == SONY) {
      Serial.print("SONY: ");
    } else if (results.decode_type == RC5) {
      Serial.print("RC5: ");
    } else if (results.decode_type == RC6) {
      Serial.print("RC6: ");
    } else if (results.decode_type == UNKNOWN) {
      Serial.print("UNKNOWN: ");
    }
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
}

view