我正在制作一个响铃的系统,当有人决定响铃或定时时。这还包括紧急响铃,例如当发生地震威胁或武装入侵者时。在武装入侵者的钟声响起.5秒(500毫秒),然后等待1.5秒(1500毫秒)并重复。然而,在第四个周期,铃声被卡住并将一直持续到我关闭arduino。我已经尝试了很多方法来尝试修复它,但它无法正常工作。我将在下面发布我的代码,有人可以看看它有什么问题吗?
谢谢!
Java for循环:
for(int i = 0; i < 21; i++) {
out.write("500".getBytes("UTF-8"));
out.flush();
Thread.sleep(2000);
}
Arduino代码:
int Relay = 13;
//The pin that the relay is attached to
//int time;
//Creates temp variable
void setup() {
Serial.begin(9600);
pinMode(Relay, OUTPUT);
}
void loop() {
while(true) {
//Check if data has been sent from the computer:
if (Serial.available()) {
int time;
//Assign serial value to temp
time = Serial.parseInt();
//Output value to relay
delay(1000);
digitalWrite(Relay, HIGH);
delay(time);
digitalWrite(Relay, LOW);
}
}
}
答案 0 :(得分:0)
您可能会尝试在检查之后和从串行读取之前(在几分钟内将一个1000毫秒的延迟)放入if循环中。当数据到达时序列化,并且您以高频率检查,程序可能会在收到所有内容之前开始读取数据。
我不知道Serial.parseInt()究竟是如何工作的,但也许可以尝试:
out.write("500\n".getBytes("UTF-8"));
你的Java循环中的