質問

First of all, sorry for the confusing title. I'm trying to make a simple program on my arduino that echos the serial input received from the serial monitor. My code is this:

String string= "";
String string2 = "";

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

}

void loop() {


    string = "";

    while(Serial.available() > 0)
    {
        string += (char) Serial.read();
        Serial.flush();

    }


    if(string != "")
    {
        Serial.println(string); 
    }

}

But when I upload it and open the serial monitor, and input anything it is spread over several lines, as so:

Input: Why are you doing this?

W
hy 
are y
ou doin
g this?

I've been stuck on this for hours now. My device is the Arduino Uno (Offical), I'm running on windows 7. Thanks in advance for any help.

Edit: Serial.print(string) returns nothing, leaves the console screen blank.

役に立ちましたか?

解決

Use Serial.print(string); instead of println()

println() Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

Ref: http://arduino.cc/en/Serial/Println

他のヒント

Just make a little delay in your while loop so the code is:

String string= "";
String string2 = "";

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

}

void loop() {


    string = "";

    while(Serial.available() > 0)
    {
        string += (char) Serial.read();
        Serial.flush();
        delay(10);

    }


    if(string != "")
    {
        Serial.println(string); 
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top