Go Down

Topic: Serial Communication issue (Read 193 times) previous topic - next topic


Hi guys,

I am using a Arduino Mega and try out some serial communication. I use commands like print, println and even printf. It seems that I can send something but always get \r\n or nothing at the end.

Actually I just want to send a number 0 - 9999 via the serial port of the Arduino plus the \n. It seems so difficult or I am currently just retarded... 



Jun 26, 2015, 01:58 am Last Edit: Jun 26, 2015, 02:00 am by Peter_n
Hi, welcome to the forum.

Code: [Select]

Serial.print("Hello");   // print hello without \r or \n
Serial.println("Hello");  // print with \r\n  (I think)
Serial.print("Hello\n");   // print with \n


Thank you so much.... Sorry maybe I asked not properly.
I have an integer AnalogValue and the assignment  AnalogValue = 123. AnalogValue is a value between 0 and 1023.

How do I get that out of the serial port with (only) the \n at the end?

Serial.print(%u\n, AnalogValue); or something like that...

Serial.print("Hello");   // print hello without \r or \n YES
Serial.println("Hello");  // print with \r\n  (I think) YES
Serial.print("Hello\n");   // print with \n YES


You can use sprintf and build a string in a buffer according to a format.
However, with the Arduino, often a few Serial.print are used.
It is normal that Serial.print is called a number of times for a single line of text output.

Code: [Select]


The Serial.print can print float, integer, text and so on.
At the bottom of this file : https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Print.h
you see all the possibilities that Serial.print and Serial.println can do. The compiler selects the proper function.


Thank you for your help. I'll give it a try... :)


Basic servo test code that might do something similar to what you want.

Code: [Select]

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.println("servo-test"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131