
Sapere Aude, quia Veritas Vos Liberabit.
敢于知道,因為真理使我們自由,
準備工具
1、兩個Adruino開發板
2、315M發射、接收模塊
3、杜邦線若干(面包板可選)
4、電腦一臺(有兩個USB介面)
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交替回圈

效果展示

實物圖

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/246226.html
標籤:其他
