Help with Protocol PulseDistance

Hello
I'm trying to make a program that send ir code with two buttons with arduino nano.
I'm using "ReceiveDemo.ino" to Decode the signal of the remote.
The result:

Protocol=PulseDistance Raw-Data=0xA 13 bits LSB first

Send on a 8 bit platform with: IrSender.sendPulseDistanceWidth(38, 2350, 500, 550, 1150, 1150, 600, 0xA, 13, PROTOCOL_IS_LSB_FIRST, , );

Protocol=PulseDistance Repeat gap=74500us Raw-Data=0xA 13 bits LSB first

How can i send this protocol via irsend when i push the button?

PLEASE HELP ME...THANKS.

With simplereciver.ino i got this resulte:

Received noise or an unknown (or not yet enabled) protocol
rawData[30]:
-3276750
+2300,- 600

  • 650,- 500 +1200,-1100 +1150,- 550 +1150,-1150
    +1150,- 600 +1100,- 600 + 550,- 600 + 550,- 600
  • 550,- 600 + 550,- 600 + 550,- 600 + 500,- 600
  • 550,- 600 + 550
    Sum: 22350

Received noise or an unknown (or not yet enabled) protocol
rawData[30]:
-74500
+2250,- 650

  • 500,- 600 +1150,-1150 +1150,- 600 +1100,-1200
    +1100,- 600 +1100,- 600 + 550,- 600 + 550,- 600
  • 550,- 600 + 550,- 600 + 550,- 600 + 550,- 600
  • 550,- 600 + 550
    Sum: 22350

Try like receiver is suggesting:

IrSender.sendPulseDistanceWidth(38, 2350, 500, 550, 1150, 1150, 600, 0xA, 13, PROTOCOL_IS_LSB_FIRST, , );
delayMicroseconds(74500);
IrSender.sendPulseDistanceWidth(38, 2350, 500, 550, 1150, 1150, 600, 0xA, 13, PROTOCOL_IS_LSB_FIRST, , );

its not work

i got this code that work for me with one button (BUTTON_PIN 12) but i d'ont know how to add second button to learn another code so i can make remote with 2 buttons on and off.
it will be great if someone can help me with it...thank you.

the code is:
#include <IRremote.h>
#include <EEPROM.h>

#define codeTypeEAddr 0 // 存放红外信号编码类型EEPROM地址
#define codeLenEAddr 1 // 存放红外信号编码长度EEPROM地址
#define toggleEAddr 2 // 存放红外信号RC5/RC6类型EEPROM地址
#define codeValueEAddr 3 // 存放红外信号数值EEPROM地址
#define RECV_PIN 11 // 红外接收器 OUT 引脚 --- Arduino 引脚11
#define BUTTON_PIN 12 // 按键开关 --- Arduino 引脚12
#define STATUS_PIN 13 // LED_BUILTIN // 状态显示LED --- 开发板内置LED
#define POWER_PIN 10
IRrecv irrecv(RECV_PIN); // 红外遥控接收器对象
IRsend irsend(3); // 红外遥控发射对象

decode_results results; // 储存接收到的红外遥控信息

void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // 启动红外接收
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(STATUS_PIN, OUTPUT);
pinMode(POWER_PIN, OUTPUT);
delay(10);
loadEepromValues(); // 从EEPROM中读取红外信号信息(具体信息请见函数部分)
}

// 红外信号存储变量
int codeType; // 红外信号编码类型
unsigned long codeValue; // 存放红外信号数值(如果不是raw型)
unsigned int rawCodes[RAWBUF]; // raw型信号
int codeLen; // 红外信号编码长度
int toggle; // 红外信号RC5/RC6类型

// 记录收到的红外信号
void storeCode(decode_results *results) {
codeType = results->decode_type;
int count = results->rawlen;
if (codeType == UNKNOWN) { //如果收到的信号是无法识别的协议,则存储为raw型数据
Serial.println("Received unknown code, saving as raw");
codeLen = results->rawlen - 1;
// 存储raw型信号:
// 将首个数值放弃(间隙)
// 将信号转化为毫秒
// 细微调整信号内容,将信息变短将空档间隙变长从而取消红外接收信号的扰动
for (int i = 1; i <= codeLen; i++) {
if (i % 2) {
// 信号
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
Serial.print(" m");
}
else {
// 空档间隙
rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
Serial.print(" s");
}
Serial.print(rawCodes[i - 1], DEC);
}
Serial.println("");
}
else {
if (codeType == NEC) { //如果是NEC协议类型
Serial.print("Received NEC: ");
if (results->value == REPEAT) {
// Don't record a NEC repeat value as that's useless.
Serial.println("repeat; ignoring.");
return;
}
}
else if (codeType == SONY) { //如果是SONY协议类型
Serial.print("Received SONY: ");
}
else if (codeType == PANASONIC) { //如果是PANASONIC协议类型
Serial.print("Received PANASONIC: ");
}
else if (codeType == JVC) { //如果是JVC协议类型
Serial.print("Received JVC: ");
}
else if (codeType == RC5) { //如果是RC5协议类型
Serial.print("Received RC5: ");
}
else if (codeType == RC6) { //如果是RC6协议类型
Serial.print("Received RC6: ");
}
else {
Serial.print("Unexpected codeType "); //无法识别信号
Serial.print(codeType, DEC);
Serial.println("");
}
Serial.println(results->value, HEX); //输出信号数值
codeValue = results->value;
codeLen = results->bits;
}
writeEepromVal(); //将收到的信号信息储存于eeprom
}

// 发射红外信号
void sendCode(int repeat) {
if (codeType == NEC) { //如果是NEC协议信号
if (repeat) { //且如果是发射重复信号
irsend.sendNEC(REPEAT, codeLen); //发射NEC协议的重复信号
Serial.println("Sent NEC repeat");
}
else {
irsend.sendNEC(codeValue, codeLen); //否则发射NEC协议红外指令信号
Serial.print("Sent NEC ");
Serial.println(codeValue, HEX); //串口监视器输出红外指令信号数值
}
}
else if (codeType == SONY) { // 发射的信号是SONY协议
irsend.sendSony(codeValue, codeLen); // 发射SONY协议红外指令信号
Serial.print("Sent Sony ");
Serial.println(codeValue, HEX);
}
else if (codeType == PANASONIC) { // 发射的信号是PANASONIC协议
irsend.sendPanasonic(codeValue, codeLen); // 发射PANASONIC协议红外指令信号
Serial.print("Sent Panasonic");
Serial.println(codeValue, HEX);
}
else if (codeType == JVC) { // 发射的信号是JVC协议
irsend.sendJVC(codeValue, codeLen, false); // 发射JVC协议红外指令信号
Serial.print("Sent JVC");
Serial.println(codeValue, HEX);
}
else if (codeType == RC5 || codeType == RC6) { // 发射的信号是RC5或RC6协议
if (!repeat) {
// 新按键按下后反转toggle位
toggle = 1 - toggle;
}
// 将toggle位放入信号代码中发送
codeValue = codeValue & ~(1 << (codeLen - 1));
codeValue = codeValue | (toggle << (codeLen - 1));
if (codeType == RC5) { // 发射的信号是RC5协议
Serial.print("Sent RC5 ");
Serial.println(codeValue, HEX);
irsend.sendRC5(codeValue, codeLen);
}
else { // 发射的信号是RC6协议
irsend.sendRC6(codeValue, codeLen);
Serial.print("Sent RC6 ");
Serial.println(codeValue, HEX);
}
}
else if (codeType == UNKNOWN /* i.e. raw */) {
// 假设信号频率 38 KHz
irsend.sendRaw(rawCodes, codeLen, 38);
Serial.println("Sent raw");
}
}

int lastButtonState; // 此变量用于判断发射红外信号的按键开关所处的状态

void loop() {
digitalWrite(POWER_PIN, HIGH);
int buttonState = !digitalRead(BUTTON_PIN); // 读取当前的按键开关状态(检查用户是否按下了按键开关)
if (lastButtonState == HIGH && buttonState == LOW) { // 如果按键开关是被按下后再抬起的

Serial.println("Released");                         // 通过串口监视器输出"按键抬起"
irrecv.enableIRIn();                                // 启动红外接收器信号接收

}

if (buttonState) { // 如果按键开关处于被按下的状态

Serial.println("Pressed, sending");                 // 通过串口监视器输出"按键按下"
digitalWrite(STATUS_PIN, HIGH);                     // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(点亮LED)
sendCode(lastButtonState == buttonState);           // 更新按键开关状态变量
digitalWrite(STATUS_PIN, LOW);                      // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(熄灭LED)
delay(50);                                          // 信号发射间歇

} else if (irrecv.decode(&results)) { // 如果按键开关处于没有被按下的状态,则实时检查红外接收器并对接收到的信号进行解码
// 如果接收到的红外信号可以通过decode函数成功解码
digitalWrite(STATUS_PIN, HIGH); // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(点亮LED)
storeCode(&results); // 将解码的红外信号信息进行储存(非EEPROM储存)
irrecv.resume(); // 恢复红外接收器
digitalWrite(STATUS_PIN, LOW); // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(熄灭LED)
}
lastButtonState = buttonState; // 更新按键开关状态变量
}

// 通过EEPROM读取红外信号信息
// 每次Arduino通电后,都会从EEPROM中读取存储的红外信号信息。
// 从而确保"学霸遥控器"在断电后依然可以保持上一次运行时所存储的
// 红外信号信息。
void loadEepromValues(){
codeType = EEPROM.read(codeTypeEAddr);
delay(10);
codeLen = EEPROM.read(codeLenEAddr);
delay(10);
toggle = EEPROM.read(toggleEAddr);
delay(10);
toggle = EEPROM.read(toggleEAddr);
delay(10);
EEPROM.get(codeValueEAddr, codeValue);
}

// 将红外信号信息储存于EEPROM
// 每一次"学霸遥控器"接收到新的红外遥控信号
// 都将最新接收的红外遥控器储存于EEPROM
// 从而确保信号信息不会因为Arduino断电而丢失
void writeEepromVal(){
EEPROM.write(codeTypeEAddr, codeType);
delay(10);
EEPROM.write(codeLenEAddr, codeLen);
delay(10);
EEPROM.write(toggleEAddr, toggleEAddr);
delay(10);
EEPROM.put(codeValueEAddr, codeValue);
delay(10);
}

Please, wrap your code in < CODE > block or paste between these markup ticks:

```
paste code here
```

1 Like
#include <IRremote.h>
#include <EEPROM.h>
 
#define codeTypeEAddr     0  // 存放红外信号编码类型EEPROM地址
#define codeLenEAddr      1  // 存放红外信号编码长度EEPROM地址
#define toggleEAddr       2  // 存放红外信号RC5/RC6类型EEPROM地址
#define codeValueEAddr    3  // 存放红外信号数值EEPROM地址  
#define RECV_PIN 11          // 红外接收器 OUT 引脚 --- Arduino 引脚11
#define BUTTON_PIN 12               // 按键开关 --- Arduino 引脚12
#define STATUS_PIN 13     // LED_BUILTIN      // 状态显示LED --- 开发板内置LED
#define POWER_PIN 10
IRrecv irrecv(RECV_PIN);   // 红外遥控接收器对象
IRsend irsend(3);             // 红外遥控发射对象
 
decode_results results;    // 储存接收到的红外遥控信息
 
void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // 启动红外接收
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(STATUS_PIN, OUTPUT);
  pinMode(POWER_PIN, OUTPUT);
  delay(10);
  loadEepromValues();  // 从EEPROM中读取红外信号信息(具体信息请见函数部分)
}
 
// 红外信号存储变量
int codeType; // 红外信号编码类型
unsigned long codeValue; // 存放红外信号数值(如果不是raw型)
unsigned int rawCodes[RAWBUF]; // raw型信号
int codeLen;    // 红外信号编码长度
int toggle;     // 红外信号RC5/RC6类型
 
// 记录收到的红外信号
void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;
  if (codeType == UNKNOWN) {    //如果收到的信号是无法识别的协议,则存储为raw型数据
    Serial.println("Received unknown code, saving as raw");  
    codeLen = results->rawlen - 1;
    // 存储raw型信号:
    // 将首个数值放弃(间隙)
    // 将信号转化为毫秒
    // 细微调整信号内容,将信息变短将空档间隙变长从而取消红外接收信号的扰动
    for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        // 信号
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        Serial.print(" m");
      } 
      else {
        // 空档间隙
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        Serial.print(" s");
      }
      Serial.print(rawCodes[i - 1], DEC);
    }
    Serial.println("");
  }
  else {
    if (codeType == NEC) {  //如果是NEC协议类型
      Serial.print("Received NEC: ");
      if (results->value == REPEAT) {
        // Don't record a NEC repeat value as that's useless.
        Serial.println("repeat; ignoring.");
        return;
      }
    } 
    else if (codeType == SONY) {  //如果是SONY协议类型
      Serial.print("Received SONY: ");
    } 
    else if (codeType == PANASONIC) {  //如果是PANASONIC协议类型
      Serial.print("Received PANASONIC: ");
    }
    else if (codeType == JVC) {  //如果是JVC协议类型
      Serial.print("Received JVC: ");
    }
    else if (codeType == RC5) {  //如果是RC5协议类型
      Serial.print("Received RC5: ");
    } 
    else if (codeType == RC6) {  //如果是RC6协议类型
      Serial.print("Received RC6: ");
    } 
    else {
      Serial.print("Unexpected codeType "); //无法识别信号
      Serial.print(codeType, DEC);
      Serial.println("");
    }
    Serial.println(results->value, HEX); //输出信号数值
    codeValue = results->value;
    codeLen = results->bits;
  }
  writeEepromVal();  //将收到的信号信息储存于eeprom
}
 
// 发射红外信号
void sendCode(int repeat) {
  if (codeType == NEC) {  //如果是NEC协议信号
    if (repeat) {         //且如果是发射重复信号
      irsend.sendNEC(REPEAT, codeLen);     //发射NEC协议的重复信号
      Serial.println("Sent NEC repeat");
    } 
    else {
      irsend.sendNEC(codeValue, codeLen);  //否则发射NEC协议红外指令信号
      Serial.print("Sent NEC ");
      Serial.println(codeValue, HEX);      //串口监视器输出红外指令信号数值
    }
  } 
  else if (codeType == SONY) {             // 发射的信号是SONY协议
    irsend.sendSony(codeValue, codeLen);   // 发射SONY协议红外指令信号
    Serial.print("Sent Sony ");
    Serial.println(codeValue, HEX);
  } 
  else if (codeType == PANASONIC) {            // 发射的信号是PANASONIC协议
    irsend.sendPanasonic(codeValue, codeLen);  // 发射PANASONIC协议红外指令信号
    Serial.print("Sent Panasonic");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == JVC) {                    // 发射的信号是JVC协议
    irsend.sendJVC(codeValue, codeLen, false);   // 发射JVC协议红外指令信号
    Serial.print("Sent JVC");
    Serial.println(codeValue, HEX);
  }
  else if (codeType == RC5 || codeType == RC6) {  // 发射的信号是RC5或RC6协议
    if (!repeat) {
      // 新按键按下后反转toggle位
      toggle = 1 - toggle;
    }
    // 将toggle位放入信号代码中发送
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {       // 发射的信号是RC5协议
      Serial.print("Sent RC5 ");
      Serial.println(codeValue, HEX);
      irsend.sendRC5(codeValue, codeLen);
    } 
    else {   // 发射的信号是RC6协议
      irsend.sendRC6(codeValue, codeLen);
      Serial.print("Sent RC6 ");
      Serial.println(codeValue, HEX);
    }
  } 
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // 假设信号频率 38 KHz
    irsend.sendRaw(rawCodes, codeLen, 38);
    Serial.println("Sent raw");
  }
}
 
int lastButtonState;   // 此变量用于判断发射红外信号的按键开关所处的状态
 
void loop() {
  digitalWrite(POWER_PIN, HIGH);
  int buttonState = !digitalRead(BUTTON_PIN);           // 读取当前的按键开关状态(检查用户是否按下了按键开关)
  if (lastButtonState == HIGH && buttonState == LOW) {  // 如果按键开关是被按下后再抬起的
    
    Serial.println("Released");                         // 通过串口监视器输出"按键抬起"
    irrecv.enableIRIn();                                // 启动红外接收器信号接收
  }                                                     
 
  if (buttonState) {                                    // 如果按键开关处于被按下的状态
    
    Serial.println("Pressed, sending");                 // 通过串口监视器输出"按键按下"
    digitalWrite(STATUS_PIN, HIGH);                     // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(点亮LED)
    sendCode(lastButtonState == buttonState);           // 更新按键开关状态变量
    digitalWrite(STATUS_PIN, LOW);                      // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(熄灭LED)
    delay(50);                                          // 信号发射间歇
    
  } else if (irrecv.decode(&results)) {                 // 如果按键开关处于没有被按下的状态,则实时检查红外接收器并对接收到的信号进行解码
                                                        // 如果接收到的红外信号可以通过decode函数成功解码
    digitalWrite(STATUS_PIN, HIGH);                     // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(点亮LED)
    storeCode(&results);                                // 将解码的红外信号信息进行储存(非EEPROM储存)
    irrecv.resume();                                    // 恢复红外接收器
    digitalWrite(STATUS_PIN, LOW);                      // 闪烁状态显示红外LED告知用户当前"学霸遥控器"正在发射红外信号(熄灭LED)
  }
  lastButtonState = buttonState;                        // 更新按键开关状态变量
}
 
// 通过EEPROM读取红外信号信息
// 每次Arduino通电后,都会从EEPROM中读取存储的红外信号信息。
// 从而确保"学霸遥控器"在断电后依然可以保持上一次运行时所存储的
// 红外信号信息。
void loadEepromValues(){
  codeType = EEPROM.read(codeTypeEAddr);
  delay(10);
  codeLen = EEPROM.read(codeLenEAddr);
  delay(10);
  toggle = EEPROM.read(toggleEAddr);  
  delay(10);
  toggle = EEPROM.read(toggleEAddr);  
  delay(10);
  EEPROM.get(codeValueEAddr, codeValue);
}
 
// 将红外信号信息储存于EEPROM
// 每一次"学霸遥控器"接收到新的红外遥控信号
// 都将最新接收的红外遥控器储存于EEPROM
// 从而确保信号信息不会因为Arduino断电而丢失
void writeEepromVal(){
   EEPROM.write(codeTypeEAddr, codeType);
   delay(10);
   EEPROM.write(codeLenEAddr, codeLen);
   delay(10);
   EEPROM.write(toggleEAddr, toggleEAddr);
   delay(10);
   EEPROM.put(codeValueEAddr, codeValue);
   delay(10);
}

1 Like

Do you want another BUTTON_PIN ? Or do you want to use another button on the IR Remote?

I need another button on the remote that will eventually connect to another input on the Arduino besides pin 12, let's say pin 8. For example pin 12 will be the "on" button and pin 8 will be the "off" button. The 2 buttons should learn from the source remote.

Do you want a button on the REMOTE to activate a button pin on the Arduino? I think your code should wait for the REMOTE button to be pressed and send a code, then pull the "second button pin" HIGH or LOW.

What does this mean?

I will explain from the beginning. I have a projector remote that has an "on" button and an "off" button. The idea is to create a remot that clone the original remote (only 2 buttons). I need a code that will store the ir code of the "off" button in a certain place (bank1) on eeprom and second ir code of the "on" button in another place in the eeprom (bank2). When I press a button it will extract the code stored in bank1 from the eeprom and transmit it via ir. Pressing the second button will extract the code in bank2 and transmit it via IR.

Follow this to store an IR remote code and transmit it...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.