Sapere Aude, quia Veritas Vos Liberabit.
敢于知道,因为真理使我们自由。
准备工具
1、两个Adruino开发板
2、315M发射、接收模块
3、杜邦线若干(面包板可选)
4、电脑一台(有两个USB接口)
PS:如果距离相对较远的话别忘了外接天线
Arduino接线
开发板(Arduino发射) | 发射模块 |
---|---|
VCC | VCC |
GND | GND |
10 | data |
开发板(Arduino接收) | 接收模块 |
---|---|
VCC | VCC |
GND | GND |
2 | data |
RC-Switch库引入
打开连接直接下载zip版本的,然后在Arduino引入库
发射模块代码
/*
Example for different sending methods
https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
// Optional set protocol (default is 1, will work for most outlets)
// mySwitch.setProtocol(2);
// Optional set pulse length.
// mySwitch.setPulseLength(320);
// Optional set number of transmission repetitions.
// mySwitch.setRepeatTransmit(15);
}
void loop() {
/* Same switch as above, but using decimal code */
mySwitch.send(1, 24);
delay(2000);
mySwitch.send(2, 24);
delay(2000);
// /* See Example: TypeA_WithDIPSwitches */
// mySwitch.switchOn("11111", "00010");
// delay(1000);
// mySwitch.switchOff("11111", "00010");
// delay(1000);
// /* Same switch as above, but using binary code */
// mySwitch.send("000000000001010100010001");
// delay(1000);
// mySwitch.send("000000000001010100010100");
// delay(1000);
// /* Same switch as above, but tri-state code */
// mySwitch.sendTriState("00000FFF0F0F");
// delay(1000);
// mySwitch.sendTriState("00000FFF0FF0");
// delay(1000);
// delay(20000);
}
在一个窗口粘贴好代码, 设置接收Arduino的端口,编译并上传
接收模块代码
/*
Simple example for receiving
https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
}
void loop() {
if (mySwitch.available()) {
Serial.print("Received ");
Serial.println( mySwitch.getReceivedValue() );
// Serial.print(" / ");
// Serial.print( mySwitch.getReceivedBitlength() );
// Serial.print("bit ");
// Serial.print("Protocol: ");
// Serial.println( mySwitch.getReceivedProtocol() );
delay(1500);
mySwitch.resetAvailable();
}
}
新创建一个窗口粘贴接收代码,设置端口,编译并上传
打开接收板的串口监视器观察接收数据
预期接收数据1和2交替循环