我正在创建一个获取串行输入的程序,将其转换为int,然后将引脚设置为HIGH,但输入时间长,例如,如果我向Arduino打印“9000”串口将引脚13设置为高电平9000毫秒。但是,它在连接开始时将引脚设置为高电平以进行初始“握手”。有什么方法可以消除这种情况,或者反击它?消除Arduino串行噪声
这里是我当前的Arduino代码:
int Relay = 13;
//The pin that the relay is attached to
int time;
//Creates temp variable
void setup() {
//Initialize the Relay pin as an output:
pinMode(Relay, OUTPUT);
//Initialize the serial communication:
Serial.begin(9600);
}
void loop() {
while(true) {
//Check if data has been sent from the computer:
if (Serial.available()) {
//Assign serial value to temp
time = Serial.parseInt();
//Output value to relay
delay(4000);
digitalWrite(Relay, HIGH);
delay(time);
digitalWrite(Relay, LOW);
}
}
}
如果有什么办法可以让我知道我怎么能这样做呢?
谢谢:)
编辑1:
int Relay = 13;
//The pin that the relay is attached to
int time;
byte byte_1;
byte byte_2;
//Creates temp variable
void setup() {
pinMode(Relay, OUTPUT);
//Initialize the serial communication:
Serial.begin(9600);
digitalWrite(Relay, LOW);
delay(250);
}
void loop() {
//Check if data has been sent from the computer:
if (Serial.available() == 2) {
byte_1 = Serial.read();
delay(100);
byte_2 = Serial.read();
}
if(byte_1 == 16) {
digitalWrite(Relay, HIGH);
delay(byte_2);
digitalWrite(Relay, LOW);
}
byte_1 = 0;
byte_2 = 0;
}
编辑3:
package net.arduino;
import java.io.OutputStream;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
public class Main {
public static void main(String[] args) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM3");
if(portIdentifier.isCurrentlyOwned()) {
System.out.println("ERROR!!! -- Port already in use!");
}else{
CommPort commPort = portIdentifier.open("XBell", 0);
if(commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream out = serialPort.getOutputStream();
byte[] buffer = new byte[2];
buffer[0] = (byte) 16;
buffer[1] = (byte) 1000;
out.write(buffer);
out.flush();
Thread.sleep(10000);
out.close();
}else{
System.out.println("ERROR!!! -- This is not normal");
}
}
}
}
你应该能够公正'digitalWrite(继电器,LOW);'你的设置()。在将它设置为输出之前,甚至可能会这样做。通过主机操作系统配置(空闲时控制线的状态),通过禁用串行命令复位,更改引导加载程序立即将信号驱动为低电平,或者甚至通过禁用复位线保险丝(尽管这会使你的程序变得很难并且很难解开)。如果信号的错误设置具有负面现实世界的后果,您可能需要更安全的设计。 –