简体   繁体   English

Arduino 串行监视器和 LED

[英]Arduino Serial Monitor and LED's

I'm using an Arduino Pro Mini 168 @ 5v and am currently trying to control individual LED's using the Serial Monitor.我正在使用Arduino Pro Mini 168 @ 5v ,目前正在尝试使用串行监视器控制单个 LED。 I am having a bit of trouble with my code and execution of the program.我的代码和程序执行遇到了一些麻烦。

I used the following code and got the issue on compile saying "Home_Control:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]"我使用以下代码并在编译时遇到问题,说“Home_Control:25:错误:ISO C++禁止指针和整数之间的比较[-fpermissive]”

//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 ");
    }


  }
}

I altered the code to this but it doesn't function on the Arduino.我将代码更改为此,但它在 Arduino 上不起作用。 I recieve jibberish for the print functions as well, not sure if related:我也收到了打印功能的乱码,不确定是否相关:

//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 ");
    }


  }
}

Attached is also the setup for my Arduino, if you see any errors in my wiring or my code please let me know!附件也是我的 Arduino 的设置,如果您发现我的接线或代码中有任何错误,请告诉我! :) :)

Pictures: ALBUM图片:专辑

Your second code is the most "correct", in my opinion, because you are using less chars.在我看来,您的第二个代码是最“正确的”,因为您使用的字符较少。

As others have pointed out, the first one is wrong because you can't compare chars and strings.正如其他人指出的那样,第一个是错误的,因为您无法比较字符和字符串。 If you want to use strings use Ramesh-X's answer, even if I don't like variable size buffers.如果您想使用字符串,请使用 Ramesh-X 的答案,即使我不喜欢可变大小的缓冲区。

Anyway, the second example prints, I think, a lot of lines because you also interprete the new line chars.无论如何,我认为第二个示例打印了很多行,因为您还解释了新行字符。 Moreover I think a switch statement is more clean.此外,我认为 switch 语句更干净。 So, just replace it with所以,只需将其替换为

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;
    }
}

EDIT: just to test the serial communication, use this sketch:编辑:只是为了测试串行通信,使用这个草图:

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);
    }
}

This should print back what you type, but as soon as you write lowercase letters it will print uppercase (eg if you write Hello there! it should print HELLO THERE! )这应该打印回您输入的内容,但是一旦您写出小写字母,它就会打印出大写字母(例如,如果您写Hello there!它应该打印HELLO THERE!

Try this and let us know if it works试试这个,让我们知道它是否有效

Try this one.试试这个。

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 = "";
  }
}

What you meant by jibberish is not clear.你说的jibberish是什么意思不清楚。 If you meant that you get unwanted characters, check your baud rate.如果您的意思是收到不需要的字符,请检查您的波特率。 If you are getting another String that you typed, other than what you expected, check again your code.如果您得到另一个您输入的字符串,而不是您所期望的,请再次检查您的代码。

This code will work if there is no problem in baud rate..如果波特率没有问题,此代码将起作用..

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

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