1 准备
1.1 硬件与软件
- 单片机
- Arduino应用程序:
https://www.arduino.cc/en/software
1.2 走线
如图, 需要舵机,红外接收器,红外发射器以及遥控器
2 代码
Arduino在IRremote库中提供了许多例子,在已有例子的基础上稍微加以改动便可以实现红外遥控
2.1 ReceiveDump
新建了示例之后,在PinDefinitionsAndMore.h文件中复制
#define IR_RECEIVE_PIN 14
#define IR_SEND_PIN 12
在ReceiveDump中的
#include <Arduino.h>
#define MARK_EXCESS_MICROS 20
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
Include <IRremote.hpp>这一行之后粘贴,将#define RAW_BUFFER_LENGTH取消注释,变成
#include <Arduino.h>
#define RAW_BUFFER_LENGTH 750
#define MARK_EXCESS_MICROS 20
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 14
#define IR_SEND_PIN 12
将IR_RECEIVE_PIN后的14改为红外接收器的接收管脚, 将IR_SEND_PIN后的12改为红外发射器的发射管脚 最终是
#include <Arduino.h>
#define RAW_BUFFER_LENGTH 750
#define MARK_EXCESS_MICROS 20
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 6
#define IR_SEND_PIN 3
然后保存 确定,在选定目录上新建一个同名文件夹
存储任何ino(Arduino)文件时都不能使用中文名
进入文件夹,保存,上传
打开串口监视器,波特率ino文件中是115200。 对遥控器的开关键进行解码
Result as microseconds array - compensated with MARK_EXCESS_MICROS=20
uint16_t rawData[229] = {很多数}; // Protocol=UNKNOWN Hash=0x24B61CFE 115 bits (incl. gap and start) received
复制中间的一串数到记事本内。
2.2 SendRawDemo
同样打开SendRawDemo示例,进行更改 在代码中的#include <IRremote.hpp>后添加2行
#include <Arduino.h>
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
}
改成(增加IR_RECEIVE_PIN 6和IR_SEND_PIN 3的设置)
#include <Arduino.h>
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 6
#define IR_SEND_PIN 3
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
然后更改下面代码。
const uint16_t rawData[] = { 9000, 4500, 560, 560, 560, 560, 560, 1690, 560,
560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560,
560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560,
1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560,
1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690,
560 };
IrSender.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), NEC_KHZ);
将复制的源码在记事本中分成2段,第一段为偶数个数值,第二段可以为奇数个数值; 将代码更改为
const uint16_t rawData0[] = { 3080,2970, 3080,4370, 580,1570, 580,520, 580,1620, 580,520, 580,520, 530,1620, 580,1620, 580,520, 530,1620, 580,520, 530,1670, 580,1570, 580,520, 580,520, 530,570, 580,520, 530,520, 580,520, 580,520, 530,1670, 580,520, 530,1620, 580,520, 580,1570, 580,520, 580,520, 480,620, 580,520, 580,470, 530,570, 580,520, 480,620, 580,520, 580,1570, 580,1620, 530,570, 580,1570, 530,570, 580,520, 530,1620, 580,520, 580,1620, 580,1620, 530,520, 580,1620, 530,1670, 580,470, 580,520, 580,520, 530,570, 580,520, 580,470, 580,520, 580,520, 580,520, 580,520, 580,470
};
const uint16_t rawData1[] = { 580,520, 580,1620, 530,570, 580,520, 580,470, 580,520, 580,520, 530,570, 580,520, 580,470, 580,520, 580,520, 580,520, 530,570, 580,470, 580,520, 580,520, 580,520, 530,570, 580,520, 580,470, 580,520, 580,520, 530,570, 580,520, 580,470, 580,520, 580,520, 580,520, 480,620, 580,470, 580,520, 580,520, 580,520, 530,570, 580,520, 580,470, 530,570, 580,520, 480,620, 580,520, 580,470, 530,570, 580,520, 580,520, 530,570, 580,1570, 580,520, 580,1620, 530,1620, 580,520, 580,520, 580,520, 580,470, 580,1620, 580
};
IrSender.sendRaw(rawData0, sizeof(rawData0) / sizeof(rawData0[0]), NEC_KHZ);
IrSender.sendRaw(rawData1, sizeof(rawData1) / sizeof(rawData1[0]), NEC_KHZ);
保存代码,上传。成功!
|