简体   繁体   English

Arduino IDE,串行读取。 我究竟做错了什么?

[英]Arduino IDE, Serial Read. What am I doing wrong?

what i am trying to do: using arduino, 1- start with the LED on pin 13 off 2- read from serial monitor 3- if entering "1" => LED on, if 0 => LED off, else => print "incorrect" I am new to Arduino, and I have tried my best.我正在尝试做的事情:使用 arduino,1- 从引脚 13 上的 LED 开始关闭 2- 从串行监视器读取 3- 如果输入“1”=> LED 打开,如果 0 => LED 关闭,否则 => 打印“不正确”我是 Arduino 的新手,我已经尽力了。 However, when I key in "1", the LED does not turn on.但是,当我键入“1”时,LED 不亮。 can somebody spot my mistake and teach me why it is wrong?有人能发现我的错误并教我为什么错了吗?

here is my code:这是我的代码:

    String command;
void setup(){
    Serial.begin(9600);
    command.reserve(5);
    command = "1";
    command += "0";
    pinMode(13,OUTPUT);
  }
  void loop(){
  digitalWrite(13,LOW);
  if(Serial.available()){
    command = Serial.readStringUntil('\n');
    if(command.equals("1")){
      digitalWrite(13,HIGH);
      }
      else if(command.equals("0")){
      digitalWrite(13,LOW);
      }
      else{
            Serial.println("Invalid command");
      }
    }
  }

It has to be buffered first because the serial is read byte after byte.它必须首先被缓冲,因为串行是逐字节读取的。 to do that we use a "character type c" aka char c.为此,我们使用“字符类型 c”又名 char c。 we loop that until we have the entire serial read (byte after byte).我们循环它,直到我们完成整个串行读取(一个字节接一个字节)。

After that we need the readstring (which is the total of all bytes we read) and compare it with a String.之后,我们需要读取字符串(这是我们读取的所有字节的总和)并将其与字符串进行比较。 The thing we got between the "".我们在“”之间得到的东西。 Im not 100% sure why we use ".indeof" but I believe the reason is because they are not the same type so instead it looks for comparison, so if more then 1 comparison is found it will return true.我不是 100% 确定我们为什么使用“.indeof”,但我相信原因是因为它们不是同一类型,所以它会寻找比较,所以如果发现超过 1 个比较,它将返回 true。

Now we still want to see if we have invalid input but to do that we need to detect if something is not 1, 2, or 3. I made a comparison which handles the readString as an int and looks if that int is lower then 1 ||现在我们仍然想看看我们是否有无效的输入,但要做到这一点,我们需要检测某些东西是否不是 1、2 或 3。我做了一个比较,它将 readString 处理为 int 并查看该 int 是否低于 1 || (or) higher than 3 so it will ignore everything between 1 and 3 but act on everything else. (或)高于 3,因此它将忽略 1 和 3 之间的所有内容,但对其他所有内容采取行动。

String readString;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(13,OUTPUT);

  digitalWrite(13,LOW); //led off at start
}

void loop() {

  

  while (Serial.available()) {
    delay(10);  //small delay to allow input buffer to fill
    if (Serial.available() > 0) {
      char c = Serial.read();  //gets one byte from serial buffer
      if (c == ',') {
        break;
      }  //breaks out of capture loop to print readstring
      readString += c;
    } //makes the string readString
  }
  if (readString.length() > 0) {
    Serial.println(readString); //prints string to serial port out

    if (readString.indexOf("1") >= 0) {
      Serial.println("LED ON");
      digitalWrite(13,HIGH);

    }
    if (readString.indexOf("2") >= 0) {                      
      Serial.println("LED OFF");
      digitalWrite(13,LOW);
    }
    if (readString.indexOf("3") >= 0) {
      Serial.println("LED TOGGLE");
      digitalWrite(13,!digitalRead(13));
    }
    if ((readString.toInt() < 1 ) || (readString.toInt() > 3)) {
      Serial.println("INVALID INPUT");
    }
  }
  readString = ""; // empty readString so the next Serial input can be stored in there
}

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

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