基于Arduino Uno开发板的红外遥控开发
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 // D5
#define IR_SEND_PIN 12 // D6 - D4/pin 2 is internal LED
在ReceiveDump中的
#include <Arduino.h>
//#define RAW_BUFFER_LENGTH 750 // 750 is the value for air condition remotes.
/*
* You can change this value accordingly to the receiver module you use.
* The required value can be derived from the timings printed here.
* Keep in mind that the timings may change with the distance
* between sender and receiver as well as with the ambient light intensity.
*/
#define MARK_EXCESS_MICROS 20 // recommended for the cheap VS1838 modules
//#define RECORD_GAP_MICROS 12000 // Activate it for some LG air conditioner protocols
//#define DEBUG // Activate this for lots of lovely debug output from the decoders.
#include "PinDefinitionsAndMore.h" //Define macros for input and output pin etc.
#include <IRremote.hpp>
//+=============================================================================
Include <IRremote.hpp>这一行之后粘贴,将#define RAW_BUFFER_LENGTH取消注释,变成
#include <Arduino.h>
#define RAW_BUFFER_LENGTH 750 // 750 is the value for air condition remotes.
/*
* You can change this value accordingly to the receiver module you use.
* The required value can be derived from the timings printed here.
* Keep in mind that the timings may change with the distance
* between sender and receiver as well as with the ambient light intensity.
*/
#define MARK_EXCESS_MICROS 20 // recommended for the cheap VS1838 modules
//#define RECORD_GAP_MICROS 12000 // Activate it for some LG air conditioner protocols
//#define DEBUG // Activate this for lots of lovely debug output from the decoders.
#include "PinDefinitionsAndMore.h" //Define macros for input and output pin etc.
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 14 // D5
#define IR_SEND_PIN 12 // D6 - D4/pin 2 is internal LED
//+=============================================================================
将IR_RECEIVE_PIN后的14改为红外接收器的接收管脚,
将IR_SEND_PIN后的12改为红外发射器的发射管脚
最终是
#include <Arduino.h>
#define RAW_BUFFER_LENGTH 750 // 750 is the value for air condition remotes.
/*
* You can change this value accordingly to the receiver module you use.
* The required value can be derived from the timings printed here.
* Keep in mind that the timings may change with the distance
* between sender and receiver as well as with the ambient light intensity.
*/
#define MARK_EXCESS_MICROS 20 // recommended for the cheap VS1838 modules
//#define RECORD_GAP_MICROS 12000 // Activate it for some LG air conditioner protocols
//#define DEBUG // Activate this for lots of lovely debug output from the decoders.
#include "PinDefinitionsAndMore.h" //Define macros for input and output pin etc.
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 6 // D5
#define IR_SEND_PIN 3 // D6 - D4/pin 2 is internal LED
//+=============================================================================
然后保存
确定,在选定目录上新建一个同名文件夹
存储任何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" //Define macros for input and output pin etc.
#include <IRremote.hpp>
// On the Zero and others we switch explicitly to SerialUSB
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
}
改成(增加IR_RECEIVE_PIN 6和IR_SEND_PIN 3的设置)
#include <Arduino.h>
#include "PinDefinitionsAndMore.h" //Define macros for input and output pin etc.
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 6 // 自行设置
#define IR_SEND_PIN 3 // 自行设置
// On the Zero and others we switch explicitly to SerialUSB
#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif
然后更改下面代码。
const uint16_t rawData[] = { 9000, 4500/*Start bit*/, 560, 560, 560, 560, 560, 1690, 560,
560/*0010 0x4 of 16 bit address LSB first*/, 560, 560, 560, 560, 560, 560, 560, 560/*0000*/, 560, 1690, 560, 1690, 560,
560, 560, 1690/*1101 0xB*/, 560, 1690, 560, 1690, 560, 1690, 560, 1690/*1111*/, 560, 560, 560, 560, 560, 560, 560,
1690/*0001 0x08 of command LSB first*/, 560, 560, 560, 560, 560, 560, 560, 560/*0000 0x00*/, 560, 1690, 560, 1690, 560,
1690, 560, 560/*1110 Inverted 8 of command*/, 560, 1690, 560, 1690, 560, 1690, 560, 1690/*1111 inverted 0 of command*/,
560 /*stop bit*/}; // Using exact NEC timing
IrSender.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
将复制的源码在记事本中分成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
}; // Using exact NEC timing
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
}; // Using exact NEC timing
IrSender.sendRaw(rawData0, sizeof(rawData0) / sizeof(rawData0[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
IrSender.sendRaw(rawData1, sizeof(rawData1) / sizeof(rawData1[0]), NEC_KHZ); // Note the approach used to automatically calculate the size of the array.
保存代码,上传。成功!