Go Down

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

agroell

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... 

Cheers,

Peter_n

#1
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

agroell

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...




BTW
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

Peter_n

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]

Serial.print(AnalogValue);
Serial.print('\n');


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.

agroell

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

zoomkat

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.begin(9600);
  myservo.attach(9);
  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
    myservo.write(n);
    readString="";
  }
}


Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up