简体   繁体   English

我如何使用 Arduino 中的串行监视器来给出像我的名字“DASH”这样的词,并且只让 LED 闪烁?

[英]How do I use Serial Monitor in Arduino to give word like my name, "DASH" and only that to blink an LED?

So,I have a task at hand and that is to use a word, like my name "DASH" as input to Serial Monitor in Arduino so that the LED in the ESP32-WROOM-32 blinks and otherwise, it won't.所以,我手头有一个任务,那就是使用一个词,比如我的名字“DASH”作为 Arduino 中串行监视器的输入,这样 ESP32-WROOM-32 中的 LED 就会闪烁,否则不会闪烁。 I am fairly new to the Arduino side and would really appreciate any guidance at all, even though it may be very little info so feel free please.我是 Arduino 方面的新手,非常感谢任何指导,即使信息可能很少,所以请随意。

I am using ESP32-WROOM-32 module and no outside LED, the one that blinks blue in the module itself.我使用的是 ESP32-WROOM-32 模块,没有外部 LED,模块本身闪烁蓝色。

Here's what I have tried:这是我尝试过的:

String incomingString;
int LED_BUILTIN=2;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {
  if (Serial.available()>0)
  {
    String incomingString=Serial.readString();
    Serial.println("Enter 'Dhaval' to blink thy LED in hue of blue!");
    if (incomingString=="Dhaval"){
      Serial.println("You have entered 'Dhaval', needless to say enjoy the beautiful baby blue LED.");
      digitalWrite(LED_BUILTIN, HIGH);
    }
    else
    {
      Serial.println("Seems that you've entered anything else but Dhaval, please just enter 'Dhaval'.");
      
    }
    
  }

}

I am aware that in this code it would be best to use for loop and if conditions to make it take each character once at a time, but not sure how to achieve that, plus I have a feeling that it may just work with this string defined as when I get to read the entry as to what I have entered and get to print it with Serial.print command it shows exactly the same string.我知道在此代码中最好使用 for 循环和 if 条件使其一次获取每个字符一次,但不确定如何实现,而且我觉得它可能只适用于此字符串定义为当我读取有关我输入的内容的条目并使用 Serial.print 命令打印它时,它显示完全相同的字符串。

What I have also thought about is using the above loops as mentioned giving them input parameters as ASCII values by each turn and confirm it and then let the final output say the whole word DASH and that only when verified lets the LED blink.我还想到的是使用上面提到的循环,每轮给它们输入参数作为 ASCII 值并确认它,然后让最后的 output 说出整个单词 DASH,只有在验证时才让 LED 闪烁。

Thank you in advance.先感谢您。

So, upon carefully reading and understanding more about Arduino and serial monitor, I feel ready to say that the problem wasn't the code, in fact it was fine.所以,在仔细阅读和了解更多关于 Arduino 和串口监视器的信息后,我准备好说问题不是代码,事实上它很好。

The problem was me and the serial monitor showing the output options.问题是我和串行监视器显示 output 选项。 It should have been "No new line" and in my case it had always been "Newline" which is what made the string input given by the user add new line to the string entered increasing the length of the string.它应该是“没有换行符”,在我的例子中它一直是“换行符”,这使得用户输入的字符串向输入的字符串添加新行,增加了字符串的长度。

Here is the code, and it works just fine given that the setting of the Serial Monitor output has been changed to "No line ending":这是代码,鉴于串行监视器 output 的设置已更改为“无行尾”,它工作得很好:

#include<string.h> #include<字符串.h>

String s1;
String s2("Dhaval");
int LED_BUILTIN=2;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {
  if (Serial.available()>0)
  {
    String s1=Serial.readString();
    Serial.println("Enter 'Dhaval' to blink thy LED in hue of blue!");
    Serial.println(s1);
    if (s1.equals(s2)){
      Serial.println("You have entered 'Dhaval', needless to say enjoy the beautiful baby blue LED.");
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
    }
    else
    {
      Serial.println("Seems that you've entered anything else but Dhaval, please just enter 'Dhaval'.");
      
    }
    
  }

}

Hope it helps some day, to someone.希望有一天对某人有所帮助。 And if you want to use "Newline" the string input will have an extra length than original intention in which case, you can just trim the string by the.trim function of the string.如果你想使用“换行符”,那么输入的字符串将比原来的意图多出一些长度,在这种情况下,你可以通过字符串的.trim function 来修剪字符串。 No issues.没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM