串行输入的Arduino控制超过1个字符

时间:2017-01-15 18:28:03

标签: arduino serial-port

我有下面的脚本,它可以工作 但我希望用onoff之类的词来控制它,而不是一个字符 我经常尝试和搜索但没有成功。

/*
Simple LED sketch
*/

int led = 13; // Pin 13

void setup()
{
    pinMode(led, OUTPUT); // Set pin 13 as digital out

    // Start up serial connection
    Serial.begin(115200); // baud rate
}

void loop()
{
    if (Serial.available()) {

        int ser = Serial.read(); //read serial as ascii integer

        if (ser == 'a') { //is this serial byte the ASCII equivalent of 0 through 9?
            digitalWrite(led, HIGH); // on
            Serial.println("aan");
        }
        else if (ser == 'u') {
            digitalWrite(led, LOW); // off
            Serial.println("uit");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用Serial.readStringUntil(terminator)从序列中读取字符串。

发送的字符串需要以换行符终止 在Arduino IDE的串行监视器中选择Newline

String cmd = "";

void loop()
{
    if (Serial.available()) {
        cmd = Serial.readStringUntil('\n');

        if (cmd == "on") {
            digitalWrite(led, HIGH); // on
            Serial.println("aan");
        }
        else if (cmd == "off") {
            digitalWrite(led, LOW); // off
            Serial.println("uit");
        }
    }
}

答案 1 :(得分:-1)

你的问题是你正在使用一个字符串,你需要声明一个字符串,这样你就可以将该字符串与输入进行比较,上面的那个人给出了一个好/非常松懈/太复杂的答案(抱歉没有冒犯我只是试图在复杂和简单的答案之间展示对比而不是冒犯你),部分是因为他没有解释他在做什么,而且因为他正在做不必要/无用的工作。 c ++中有一个名为Serial.readString()的函数,比那里的东西容易得多。假设您的问题中的编程水平(无攻击性)是对数据类型的快速回顾:

String =“”

int =整数{1,2,3,4,5,6,...}

char =''< - 注意区别于String =“”

float =浮点数{1.2,4.5,...}

(这些不是全部,有更多像字节等等,但只是确保你知道如何使用上面的内容)

    /*
    Simple LED sketch
    */

    int led = 7; // Pin 13
    String ser;  //Declare the string that is going to store what your are going to

//write in the serial

    void setup(){

        // Start up serial connection
        //It's good convention to start the serial before pinMode or any other thing         
//in setup

        Serial.begin(9600); //I don't know why you need such an high baud rate in        

//your thing, 9600 for what you have to do is more than fine, this is just at 
  //what speed the serial is read (in a very general explanation)

        pinMode(led, OUTPUT); // Set pin 13 as digital out

    //make sure you put in here wheter you want to start the led "on" or "off"

    //Eg you want it to start on, then digitalWrite(led, HIGH);


    }

    void loop(){

      ser = Serial.readString();


         if(Serial.available() == 0) {  //You can also use a while loop if you want to
    //This is telling the arduino: if there is something in the serial, then do...

          if(ser == "on"){

                Serial.println("on");
            digitalWrite(led, HIGH);

          }else if(ser == "off"){

            Serial.println("off");
            digitalWrite(led, LOW);
        }
        }

    }

希望它有所帮助! 还要注意上面的代码如何用

if(Serial.available())

这是一个非常WEIRD和SHADY声明,可能无效。那是因为你并没有真正将int值告诉函数Serial.available

正如arduino指南所指定:

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

可见here