Arduino串行监视器和LED

时间:2015-12-01 05:13:16

标签: c++ arduino

我正在使用 Arduino Pro Mini 168 @ 5v ,目前正在尝试使用串行监视器控制单个LED。我的代码和程序的执行有点麻烦。

我使用了以下代码并在编译时遇到了问题 “Home_Control:25:错误:ISO C ++禁止指针和整数之间的比较[-fpermissive]” < / p>

//Variable definitions
int LEDone = 3; //Grey wire
int LEDtwo = 5; //Yellow wire
int LEDthree = 6; //Brown wire


void setup() {
  Serial.begin(9600);
  pinMode(LEDone, OUTPUT); //Set Pins to OUTPUT
  pinMode(LEDtwo, OUTPUT);
  pinMode(LEDthree, OUTPUT);

}

void loop() {
 if (Serial.available() > 0)
  {
    char inputCommand = Serial.read();
    Serial.print(inputCommand);   
    Serial.print("        ");      
    Serial.print(inputCommand);
    Serial.print("       ");     
    Serial.print(char(inputCommand));

    if(inputCommand == "one")
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      Serial.print(" Lights set to Low ");
    }

    if(inputCommand == "two")
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      Serial.print(" Lights set to Medium ");
    }

    if(inputCommand == "three")
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      Serial.print(" Lights set to High ");
    }


  }
}

我将代码更改为此,但它在Arduino上不起作用。我也接受了打印功能的乱码,不确定是否相关:

//Variable definitions
int LEDone = 3; //Grey wire
int LEDtwo = 5; //Yellow wire
int LEDthree = 6; //Brown wire


void setup() {
  Serial.begin(9600);
  pinMode(LEDone, OUTPUT); //Set Pins to OUTPUT
  pinMode(LEDtwo, OUTPUT);
  pinMode(LEDthree, OUTPUT);

}

void loop() {
 if (Serial.available() > 0)
  {
    char inputCommand = Serial.read();
    Serial.print(inputCommand);   
    Serial.print("        ");      
    Serial.print(inputCommand);
    Serial.print("       ");     
    Serial.print(char(inputCommand));

    if(inputCommand == '1')
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      Serial.print(" Lights set to Low ");
    }

    if(inputCommand == '2')
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      Serial.print(" Lights set to Medium ");
    }

    if(inputCommand == '3')
    {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      Serial.print(" Lights set to High ");
    }


  }
}

附件也是我的Arduino的设置,如果你看到我的线路或我的代码有任何错误,请告诉我! :)

图片:ALBUM

2 个答案:

答案 0 :(得分:1)

在我看来,你的第二个代码是最正确的#34;因为你使用较少的字符。

正如其他人所指出的那样,第一个是错误的,因为你无法比较字符和字符串。如果你想使用字符串,请使用Ramesh-X的答案,即使我不喜欢可变大小的缓冲区。

无论如何,第二个例子打印,我认为,很多行,因为你也解释了新的行字符。此外,我认为switch语句更干净。所以,只需用

替换它
if (Serial.available() > 0)
{
    char inputCommand = Serial.read();
    if ((inputCommand != '\r') && (inputCommand != '\n'))
    {
        Serial.print(inputCommand);   
        Serial.print("        ");      
        Serial.print(inputCommand);
        Serial.print("       ");     
        Serial.print(char(inputCommand));
    }

    switch (inputCommand)
    {
    case '1':
        digitalWrite(3, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        Serial.print(" Lights set to Low ");
        break;
    case '2':
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, LOW);
        Serial.print(" Lights set to Medium ");
        break;
    case '3':
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        Serial.print(" Lights set to High ");
        break;
    }
}

编辑:只是为了测试串行通信,请使用此草图:

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    if (Serial.available() > 0)
    {
        char inputCommand = Serial.read();
        if ((inputCommand >= 'a') && (inputCommand <= 'z'))
            inputCommand = inputCommand + 'A' - 'a';
        Serial.print(inputCommand);
    }
}

这应该打印回你输入的内容,但是一旦你写下小写字母,它就会打印大写(例如,如果你写Hello there!,它应该打印HELLO THERE!

试试这个并告诉我们它是否有效

答案 1 :(得分:0)

试试这个。

String data = "";
void loop() {
  if(Serial.available())
  {
    data += (char)Serial.read();
  }

  if(inputCommand == "one")
  {
    digitalWrite(3, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    Serial.print(" Lights set to Low ");
    data = "";
  }

  if(inputCommand == "two")
  {
    digitalWrite(3, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    Serial.print(" Lights set to Medium ");
    data = "";
  }

  if(inputCommand == "three")
  {
    digitalWrite(3, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    Serial.print(" Lights set to High ");
    data = "";
  }
}

jibberish的含义不明确。如果您的意思是不想要的字符,请检查您的波特率。如果您要获得另一个键入的字符串,而不是您期望的字符串,请再次检查您的代码。

如果波特率没有问题,此代码将起作用。