如何在arduino中存储和调用多个字母?

How to store & call multiple letters in arduino?

我是编程新手,我正在尝试编写一个根据串行输入调整 LED 的程序。

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
char *myStrings[] = {"bathdim","bathbright","livingdim","livingbright","beddim","bedbright"};
void setup(){
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
 Serial.begin(9600);
}
void loop() {
if(Serial.read() == *myStrings[0]){
    Serial.println(*myStrings[0]);  // prints "bathdim" on the serial monitor
    digitalWrite(Bathroom,0);       // turns lights off
    delay(100);
  }
  if(Serial.read() == *myStrings[1]){ // prints "bathbright" on the serial monitor
    Serial.println(*myStrings[1]); // turns lights on
    digitalWrite(Bathroom,1);
    delay(100);
  }
  if(Serial.read() == *myStrings[2]){
    Serial.println(*myStrings[2]);
    digitalWrite(Livingroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[3]){
    Serial.println(*myStrings[3]);
    digitalWrite(Livingroom,1);
    delay(100);
  }
}

当 运行 代码时,我输入了“bathbright”,它应该可以打开灯,但我没有工作,只有 Serial.println(*myStrings[1])打印字母“b”,而不是“bathbright”

有人能帮忙吗?

编辑

ok,我去掉了*,现在代码如下:

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
char *myStrings[] = {"bathdim","bathbright","livingdim","livingbright","beddim","bedbright"};
void setup(){
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
 Serial.begin(9600);
}
void loop() {
if(Serial.read() == *myStrings[0]){
    Serial.println(myStrings[0]);
    digitalWrite(Bathroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[1]){
    Serial.println(myStrings[1]);
    digitalWrite(Bathroom,1);
    delay(100);
  }
  if(Serial.read() == *myStrings[2]){
    Serial.println(myStrings[2]);
    digitalWrite(Livingroom,0);
    delay(100);
  }
  if(Serial.read() == *myStrings[3]){
    Serial.println(myStrings[3]);
    digitalWrite(Livingroom,1);
    delay(100);
  }
}

该代码最初运行良好,但在输入 bathbright 和 bathdim 之后,串行打印未打印出正确的字词,并且 LED 并不总是响应...

最终代码:

#define Bathroom 13  //LED connected to pin13 named bathroom
#define Livingroom 9 //LED connected to pin9 named livingroom
void setup(){
 Serial.begin(9600);
 pinMode(Bathroom, OUTPUT);
 pinMode(Livingroom, OUTPUT);
}

void loop(){
    String str = Serial.readString();
    if(str.indexOf("bathdim") > -1){
      Serial.println("dimming bathroom");
      digitalWrite(Bathroom,0);
    } 
    if(str.indexOf("bathbright") > -1){
      Serial.println("lighting up bathroom");
      digitalWrite(Bathroom,1);
    }
    if(str.indexOf("livingdim") > -1){
      Serial.println("dimming Livingroom");
      digitalWrite(Livingroom,0);
    } 
    if(str.indexOf("livingbright") > -1){
      Serial.println("lighting up Livingroom");
      digitalWrite(Livingroom,1);
    } 
}

*(myStrings[0]) 将获得数组中的第一个 (char *),它指向包含 bathdim 的起始内存位置,然后使用打印第一个字符的 * 取消引用它,所以你得到字母 b.

只需删除取消引用,删除 *

编辑 - 看到你更新的问题,星号没有消失,但无论如何尝试使用 Serial.readString() 读入一个字符串,你一次读入 1 个字节

只需删除 * !因为 *string 指的是字符串中的第一个 case!所以*string[0]指的是字符串数组中第一个case的第一个字母!

Serial.read() 只会读取第一个字节。您必须循环读取整个字符串(和足够大的缓冲区)并使用比较函数来比较字符串。

作为测试的替代方法,您可以使用单字节代码 - 例如 'a''b''c''d'.

要打印数组中的第二个字符串,可以使用Serial.println(myStrings[1])


这是一个使用单个字符的示例:

void loop() {

    if (Serial.available()) {

        int value = Serial.read();

        switch (value) {
            case 'a':
                digitalWrite(Bathroom, 0);
                break;
            case 'b':
                digitalWrite(Bathroom, 1);
                break;
            case 'c':
                digitalWrite(Livingroom, 0);
                break;
            case 'd':
                digitalWrite(Livingroom, 1);
                break;
            default :
                break;
        }

        if (value >= 'a') {   // avoid delaying on newlines
            delay(100); 
        }
    }
}