2015年1月4日 星期日

Arduino IR remote control

ref. Ken Shirriff's blog
http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html

Step 1.
The examples/IRrecvDemo sketch provides a simple example of how to receive codes:

#include "IRremote.h"

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop() {
 


  if (irrecv.decode(&results)) {
    Serial.print(results.value, HEX);

    Serial.print(", bits is ");
    Serial.print(results.bits);
    Serial.print(", decode_type is ");
    Serial.println(results.decode_type);
    irrecv.resume(); // Receive the next value
  }
}
 
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define PANASONIC 7
#define JVC 8
#define SANYO 9
#define MITSUBISHI 10
#define UNKNOWN -1 

Step 2.

#include "IRremote.h"

int receiver = 11; // pin 11 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

void setup()  
{
  Serial.begin(9600);
  Serial.println("IR Receiver Raw Data + Button Decode Test");
  irrecv.enableIRIn(); // Start the receiver
}

void loop()  
{
  if (irrecv.decode(&results))
  {
    translateIR();
    irrecv.resume();
  } 
}

void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
  case 0xE995F5AE: 
    Serial.println(" CH-            ");
    break;

  case 0x8311AD20: 
    Serial.println(" CH+            ");
    break;

  default:
    Serial.println(" other button   ");
  }
  delay(500);


沒有留言:

張貼留言