我试图使用Arduino串行显示器接收输入
我正在尝试制作一个简单的程序来打开和关闭激光器。它运行良好,直到我试图通过将2输入串行显示器来使其眨眼。它眨眼,但只有一次。我想做的就是永远眨眼,同时还等待用户输入某些东西以停止眨眼。这是我的代码。
int laserPin = 3;
int laserState;
void setup()
{
pinMode(laserPin, OUTPUT); // set pin10 as output pin
digitalWrite(laserPin, LOW); // set the pin value on low at the begin
Serial.begin(9600);
Serial.println(" Commands");
Serial.println("--------------------");
Serial.println("2- makes laser blink");
Serial.println("1- turns laser on");
Serial.println("0- turns laser off");
}
void loop()
{
Serial.println("Set laser state to: "); //Prompt User for Input
while (Serial.available() == 0) {
// Wait for User to Input Data
}
laserState = Serial.parseInt(); //Read the data the user has input
Serial.println(laserState);
if(laserState == 1){
Serial.println("Turning laser on");
digitalWrite(laserPin, HIGH);
}
else if(laserState == 0){
Serial.println("Turning laser off");
digitalWrite(laserPin, LOW);
}
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
}
这是我为激光闪烁的代码:
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
I'm trying to make a simple program to turn a laser on and off. It was working fine until I tried to be able to make it blink by inputting a 2 to the serial monitor. It blinks, but only once. What I'm trying to make it do is blink forever, while also waiting for the user to input something to be able to stop blinking. Here is my code.
int laserPin = 3;
int laserState;
void setup()
{
pinMode(laserPin, OUTPUT); // set pin10 as output pin
digitalWrite(laserPin, LOW); // set the pin value on low at the begin
Serial.begin(9600);
Serial.println(" Commands");
Serial.println("--------------------");
Serial.println("2- makes laser blink");
Serial.println("1- turns laser on");
Serial.println("0- turns laser off");
}
void loop()
{
Serial.println("Set laser state to: "); //Prompt User for Input
while (Serial.available() == 0) {
// Wait for User to Input Data
}
laserState = Serial.parseInt(); //Read the data the user has input
Serial.println(laserState);
if(laserState == 1){
Serial.println("Turning laser on");
digitalWrite(laserPin, HIGH);
}
else if(laserState == 0){
Serial.println("Turning laser off");
digitalWrite(laserPin, LOW);
}
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
}
Here is the code I made to make the laser blink:
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想一次使Arduino做一个以上的事情,则需要让循环保持运行并跟踪状态 - 而不是在循环时保持循环。
我已经采用了您的代码并进行了稍微修改,以允许读取串行,并闪光激光。
在此版本中,
laserstate
通常设置为-1-这意味着循环每次检查串行输入都不会做任何事情。当您输入串行INT时 - 这将更改
laserstate
,将执行适当的操作。打开和关闭激光后,立即将
Laserstate
设置回-1,因此不会发生进一步的操作。但是 - 如果
laserstate
将设置为2
,则它将保持在每个环路迭代的值,并且激光将继续闪烁,直到收到另一个串行输入以更改状态。还有一个标志可以确定何时显示提示(尽管每次LED闪烁时都会显示它)。
可以通过删除闪光灯中的延迟并使用
millis()
来确定何时打开和关闭激光器来使这可以更快地响应 - 因此您不必等待闪存循环读取下一个串行输入 - 但这会更复杂。If you want to make the arduino do more than one thing at a time, you need to let the loop keep running and keep track of the state - rather than holding it up with while loops.
I've taken your code and modified it slightly to allow the serial to be read, and the laser to be flashed.
In this version
laserState
is normally set to -1 - which means the loop won't do anything apart from check the serial input each time.When you input a serial int - this will change
laserState
and the appropriate action will be performed.Immediately after switching on and off the laser,
laserState
is set back to -1 so no further actions occur.However - if
laserState
is set to2
, it will stay at that value for each loop iteration and the laser will continue to flash until another serial input is received to change the state.There's also a flag to determine when to display the prompt (although it will display everytime the led flashes).
This could be made more responsive by removing the delays in the flash and having a timer using
millis()
to determine when to switch the laser on and off - so you don't have to wait for a flash cycle to read the next serial input - but that would be a bit more complicated.