Let's Make Robots!

Arduino Serial Question

Hello,

I was wondering if anyone could tell me how to send a string to the arduino and just echo it back to the serial monitor but also create a NEW LINE.  Right now I got it to take a string and echo it back exactly but every time I send the string form the computer to the ard it just tacks the echo from the ard right back onto the end of the previous data that is showing in the monitor.  

 

For example: 

I get this:

hello from arduino!!hello from arduino!!

 

I want this:

hello from arduino!!

hello from arduino!!

 

Can anybody lead me in the right direction?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

What are you trying to accomplish?  Echoing every character?  Try using an if/else statement that sends the  Serial.println(""); statement when it detects the enter key, or echos the typed character if it doesn't.

I am trying to send a string to the arduino and then the arduino just simply echos the same thing back to the monitor.  everytime time i send a string, the string that the arduino sends back will be on a new line.

I tried that but all it does is make the string in the monitor look like this.

Example:

h

e

l

l

o

 

 

Here is the code:

void loop()

{

int data;

   //while(Serial.available() == 0);

  if(Serial.available() != 0)

{

data = Serial.read();

Serial.print((char)data);

}

Serial.println("");

}

You do only want to add this carriage return when you receive a carriage return. everything else should be copied one-to-one but the cr needs an extra.


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

void loop()
{
  if(Serial.available())
  {
    char data = Serial.read();
    Serial.print(data);
    if (data == '\r')
      Serial.println("");
  }
}

 

But I think your problem is something else... you are using the build in serial console in the Arduino IDE, right? that one defaults to NOT sending carriage returns, or line feeds, not even when you press that key. Maybe what you want to do is to enable transmitting some sort of line end. have a look at the selector at the bottom of the window. well, I'm just guessing here....

welp...for me knowing as much about computers I feel embarassed that I didnt see that option at the bottom of the window.  LOL. I just chose the new line option from the option list and wha laaa. It works.  Thanks mogul!!!

One solution:

Serial.println("");

Will do a carriage return.

sorry for posting it incorrectly.  I forgot to hit the reply link first.