我正在尝试通过射频发射器重现一个简单的 433mhz 信号。我已经使用 RCSwitch 来捕获这个信号,当我编写一个简单的“发送”代码时,它工作得很好——它通过无线插座关掉了灯。我的问题与这个黑客并没有真正的关系。
如果我使用下面的代码,我的信号就会正确输出。
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
mySwitch.enableTransmit(3);
mySwitch.setPulseLength(183);
mySwitch.setRepeatTransmit(6);
}
void loop() {
mySwitch.send(5526835, 24);
delay(5000);
}
但是,当我尝试使用串行输入对此代码执行更复杂的操作时,我的信号不起作用。据我所知,pulseLength 设置为 350,而不是我需要的 183。
我的代码有点长,所以我不会发布,但我要求来自 Serial 的输入。“11”表示设备 1 开启,“10”表示设备 1 关闭。代码似乎按预期工作,甚至给出了输出信号,但脉冲长度回到 350。“中断”是否发生了什么或者串行输入对我的输出信号有什么影响?我什至玩过中断,但仍然无法让这个更复杂的例子工作。
编辑:-询问什么设备的代码
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int inputData; int signalInt = 0;
void setup() {
Serial.begin(9600);
mySwitch.enableTransmit(3);
mySwitch.setPulseLength(183);
mySwitch.setProtocol(1);
mySwitch.setRepeatTransmit(6);
}
void loop(){
Serial.println("Enter Device ID Integer:");
startProcess();
}
void startProcess(){
while (Serial.available() > 0){
inputData = Serial.parseInt();
convertToDevice(inputData);
}
startProcess();
}
void convertToDevice(int input){
String x = String(input);
toggleDevice(x.charAt(0), x.charAt(1));
}
void toggleDevice(char deviceId, char sig){
signalInt = 0;
delay(2000); //only here to see if this helped
if(deviceId == '1'){
if(sig == '0'){ // device off
mySwitch.send(5526844, 24);
delay(2000);
startProcess();
}
if(sig == '1'){ // device on
mySwitch.send(5526835, 24);
delay(2000);
startProcess();
}
} // end device 1
if(signalInt == 0){
Serial.println('Nope, wrong code');
startProcess();
}
}