2

I am attempting to control a stepper motor with Sparkfun's Big Easy Driver, and I see the line "Serial.println();" they put in the example code. Why is there no argument and what function does this serve?

//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define MS3 6
#define EN  7

//Declare variables for functions
char user_input;
int x;
int y;
int state;

void setup() {
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS3, OUTPUT);
  pinMode(EN, OUTPUT);
  resetBEDPins(); //Set step, direction, microstep and enable pins to default states
  Serial.begin(9600); //Open Serial connection for debugging
  Serial.println("Begin motor control");
  Serial.println();
  //Print function list for user selection
  Serial.println("Enter number for control option:");
  Serial.println("1. Turn at default microstep mode.");
  Serial.println("2. Reverse direction at default microstep mode.");
  Serial.println("3. Turn at 1/16th microstep mode.");
  Serial.println("4. Step forward and reverse directions.");
  Serial.println();
}
  • 2
    prints everything in the argument, followed by a newline .... in other words, prints just a newline – jsotola Apr 10 '19 at 17:32
  • 1
    when you post code in a question, select all of the code and press ctrl-k or click the {} button .... all of the code will be indented by 4 spaces, which causes it to be displayed as code ...... upvote for being concerned with code formatting – jsotola Apr 10 '19 at 17:35
  • That's simple. Thanks! So if you had printed text (surrounded by quotation marks) before, the same effect would be achieved by putting in a "\n", correct? – Jackalakalaka Apr 10 '19 at 18:27
  • yes, serial.print("\n"); gives same result as serial.println(); – jsotola Apr 10 '19 at 23:43
  • @jsotola: No, Serial.println() outputs a '\r' before the '\n', which Serial.print("\n") doesn't. – Edgar Bonet Apr 11 '19 at 7:21
2

It simply prints a newline to the serial monitor. Just for spacing.

2

To complement jose can u c's answer: the Arduino Serial object uses CRLF as the end-of-line marker. That's an ASCII CR (carriage return, or '\r' in C) followed by ASCII LF (line feed, '\n' in C). Thus,

Serial.println();

is equivalent to

Serial.print("\r\n");
  • This is a cool expansion on some fundamental formatting stuff. Thank you for the info – Jackalakalaka Apr 10 '19 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.