使用带有IRremote库的Arduino远程克隆

时间:2017-06-13 18:18:19

标签: arduino clone

我尝试使用Arduino克隆AC遥控器。我使用以下代码读取了IR信号:

#include <IRremote.h>

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

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

// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) {
    Serial.print("Unknown encoding: ");
  } 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: ");
  } else if (results->decode_type == PANASONIC) {   
    Serial.print("Decoded PANASONIC - Address: ");
    Serial.print(results->panasonicAddress,HEX);
    Serial.print(" Value: ");
  } else if (results->decode_type == JVC) {
     Serial.print("Decoded JVC: ");
  }
  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 loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    dump(&results);
    irrecv.resume(); // Receive the next value
  }
}

然后我使用此代码将其发送给AC:

#include <IRremote.h>

IRsend irsend;

void setup() {
}

void loop() {
  for (int i = 0; i < 3; i++) {
    irsend.sendNEC(0xC64E80C, 32);
    delay(40);
  }
  delay(5000); //5 second delay between each signal burst
}

我使用2个单独的Arduinos进行测试。但不知何故,即使hexa值相同,内容也不会以相同的方式发送。

使用遥控器发送的IR信号有108个值,Arduino的IR信号只有68个值。

ARDUINO OUTPUT

我尝试用我的Arduino红外LED更换远程红外LED,它工作得很好。这不是硬件问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您好我做了同样的事情。我用它来获取GREE AC的AC代码。我发现它有两个星期的斗争和一个名为AnalysIR的论坛。如果您在下面的代码中发现任何问题或方法让我知道。

AC代码通常很长,而且上面使用的代码并没有正确,所以我给你的示例代码在Arduino草图中运行,以便你可以测试。

   #define LEDPIN 13
  //you may increase this value on Arduinos with greater than 2k SRAM
  #define maxLen 800

       volatile  unsigned int irBuffer[maxLen]; 
       //stores timings - volatile because changed by ISR
       volatile unsigned int x = 0;
       //Pointer thru irBuffer - volatile because changed by ISR

     void setup() {
     Serial.begin(115200); //change BAUD rate as required
     attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);
    //set up ISR for receiving IR signal
     }

 void loop() {
   // put your main code here, to run repeatedly:

   Serial.println(F("Press the button on the remote now - once only"));
    delay(5000); // pause 5 secs
  if (x) { //if a signal is captured
digitalWrite(LEDPIN, HIGH);//visual indicator that signal received
Serial.println();
Serial.print(F("Raw: (")); //dump raw header format - for library
Serial.print((x - 1));
Serial.print(F(") "));
detachInterrupt(0);//stop interrupts & capture until finshed here

for (int i = 1; i < x; i++) { //now dump the times
  if (!(i & 0x1)) Serial.print(F("-"));
  Serial.print(irBuffer[i] - irBuffer[i - 1]);
  Serial.print(F(", "));
                     }

   x = 0;
   Serial.println();
   Serial.println();
   digitalWrite(LEDPIN, LOW);//end of visual indicator, for this time
   attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//re-enable ISR for            receiving IR signal
        }

      }

  void rxIR_Interrupt_Handler() {
    if (x > maxLen) return; //ignore if irBuffer is already full
     irBuffer[x++] = micros(); //just continually record the time-stamp of signal transitions

    }

完成此操作后,您将获得正确的代码以将其发送给AC。

有点棘手的部分

一旦你将收到原始值的代码,你将不得不对它进行一些修改,例如我得到原始值并将其转换为数组。

我收到的内容 Raw(16): - 27750,4464,696,1612,692,516,688,516,688,1620,692,512,692,1612,696,1612,692,512,692,516,688,1616, 696,1612,692,512,696,512,692,512,692,512,696,512,692,512,692,512,696,512,692,512,692,512,696,1612,692, 1612,692,516,688,516,692,516,688,516,688,516,692,1612,692,516,688,1620,688,516,688,520,684,1620,660,544, 664,19960,628,1680,628,1676,628,580,628,1676,628,580,624,580,628,576,628,580,628,584,620,576,628,580,628, 576,628,576,628,580,624,1680,628,580,624,580,628,580,624,580,628,576,628,580,624,580,628,576,628,580, 628,576,628,580,624,580,628,576,628,1680,624,580,628,1676,628,1680,628

要将上述原始值转换为数组,请删除第一个值-27750并将3800而不是该值。以下是数组样本。**

unsigned int data [] = {3800,4464,696,1612,692,516,688,516,688,1620,692,512,692,1612,696,1612,692,512,692,516, 688,1616,696,1612,692,512,696,512,692,512,692,512,696,512,692,512,692,512,696,512,692,512,692,512,696, 1612,692,1612,692,516,688,516,692,516,688,516,688,516,692,1612,692,516,688,1620,688,516,688,520,684,1620, 660,544,664,19960,628,1680,628,1676,628,580,628,1676,628,580,624,580,628,576,628,580,628,584,620,576,628, 580,628,576,628,576,628,580,624,1680,628,580,624,580,628,580,624,580,628,576,628,580,624,580,628,576, 628,580,628,576,628,580,624,580,628,576,628,1680,624,580,628,1676,628,1680,628, };

现在将这些值发送到IR发射器,请使用以下代码。

      #include<IRremote.h>
       IRsend irsend;
       int KHz = 38;                    // default frequency

      unsigned int data[] = {3800, 4464, 696, 1612, 692, 516, 688, 516, 688, 1620, 692, 512, 692, 1612, 696, 1612, 692, 512, 692, 516, 688, 1616, 696, 1612, 692, 512, 696, 512, 692, 512, 692, 512, 696, 512, 692, 512, 692, 512, 696, 512, 692, 512, 692, 512, 696, 1612, 692, 1612, 692, 516, 688, 516, 692, 516, 688, 516, 688, 516, 692, 1612, 692, 516, 688, 1620, 688, 516, 688, 520, 684, 1620, 660, 544, 664, 19960, 628, 1680, 628, 1676, 628, 580, 628, 1676, 628, 580, 624, 580, 628, 576, 628, 580, 628, 584, 620, 576, 628, 580, 628, 576, 628, 576, 628, 580, 624, 1680, 628, 580, 624, 580, 628, 580, 624, 580, 628, 576, 628, 580, 624, 580, 628, 576, 628, 580, 628, 576, 628, 580, 624, 580, 628, 576, 628, 1680, 624, 580, 628, 1676, 628, 1680, 628,
        };


    void setup() 
   {
     Serial.begin(9600);   
     irsend.enableIROut(38);
   }

    void loop()
   {

    for (int i=0; i<1 ; i++){
    irsend.sendRaw(data, sizeof(data) / sizeof(int),38 ); 
    delay(40);
          }


 }