carriage return issue

I do no think that my question requires any code even though it is a coding question, it is pretty straight forward: How do I put a carriage return in my code in the IDE? I have a set of commands that run perfectly if i hit the enter key in between each command.

I do believe that if i know how to put a carriage return in my code, this will fix the issue.

I have looked elsewhere online and seen a few answers: CR, \r, \n, \r\n; however non have succeded.

This program is to meant to allow me to have a mouse go through a computer and extract data without an operator needing to handle the mouse. An automatic mouse if you will.

//Creates a terminal interface so a user can send commands to the mcu from a COM port.

#define serial_com mySerial //Application, uses FTDI COM port.
//#define serial_com Serial //Debugging, uses same COM port as MCU.

#include "Mouse.h"
#include "Keyboard.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 16); // RX, TX

String arg[4];
boolean error = false;

void setup() {
  serial_com.println("--HID USB Mouse Emulator--");
  serial_com.println("Enter 'help' for a list of valid commands.");
  serial_com.println("Enter Command >");

void loop() {
    serial_com.print("Enter Command> ");;

void read_command(){
  String command = arg[0];
  if (command == "mouse") {
  else if (command == "key") {
  else if (command == "help")
  error = false;

void keyboard_functions() {
  String function = arg[1];

  if (function == "cat") {;;'`');
  if (!error)
    serial_com.println(" Keyboard done!");

void mouse_functions() {
  String function = arg[1];
  String var1 = arg[2];
  int responseDelay = 2;

  if (function == "x") {
    Mouse.move(var1.toInt(), 0, 0); //Moves the mouse cursor left or right a specified number of pixels. Min/Max is -127 to 127.
  else if (function == "y") {
    Mouse.move(0, var1.toInt(), 0); //Moves the mouse cursor up or down. 
  else if (function == "l") {; //Send mouse left-click
  else if (function == "r") {; //Send mouse right-click
  else if (function == "bgn") {
  else if (function == "end") {
  if (!error)
    serial_com.println(" Mouse done!");

void read_args(String data) {
  int arg_count = 0;
  int arg_index[4];
  int data_length = data.length();
  for (int index = 0; index < data_length; index++) {
    if (data.charAt(index) == ' ') {
      arg_index[arg_count] = index;
      arg_count += 1;
  arg[0] = data.substring(0, arg_index[0]);
  arg[1] = data.substring(arg_index[0] + 1, arg_index[1]);
  arg[2] = data.substring(arg_index[1] + 1, arg_index[2]);
  arg[3] = data.substring(arg_index[2] + 1, arg_index[3]);

void read_line() {
  String inputString = "";
  unsigned long last_byte_time = 0;
  unsigned long elapsed_time = 0;
  while (true) {
    if (serial_com.available()) {
      char inChar = (char);
      last_byte_time = millis();
      if (inChar == '\n') {
      inputString += inChar;
    elapsed_time = millis() - last_byte_time;
    if (elapsed_time > 1000) { //1 second timeout
      serial_com.println("Timeout exceeded");

void check_quotes(String data) {
  int first_quote = data.indexOf("\"");
  if (first_quote != -1) {
    int second_quote = data.lastIndexOf("\"");
    arg[2] = data.substring(first_quote + 1, second_quote);

Carriage return is '\r'.
The serial monitor may not handle it correctly, but a proper terminal emulator should.

would you put it at the beginning or end of the main loop?

Why do you think it would solve your pb? What is your pb (besides using way too many Strings)?

I want to be able to move my mouse cursor so many pixels at a time by sending serial commands and have the option to right click, left click or type a few buttons on the keyboard. This is supposed to emulate a HID mouse and keyboard for a test.

I am able to move the mouse, right click, left click and type a few buttons on the keyboard. However, in order to execute one command, and then execute a second, I need to send a blank command. I just press enter. And then I am able to execute the second command no problem. That is why I believe it is a carriage return issue. I could be totally wrong with this as well as my time playing with C has been pretty brief thus far.

How is you serial monitor configured to send the commands?

I have a usb/ftdi cable connected to a com port on my computer and I am using my serial monitor to communicate through that port to the micro. On the micro arduino side of that cable, i am directly wiring the ground from the cable to the ground on the micro, and then wiring my txd and rxd pins of the ftdi cable to pin 10 and 16 on my micro arduino.

Here is the pinout of the arduion micro that i am using:

So 1) I am not sure if that really answers your question or not and 2) Should I have my txd and rxd from the ftdi cable connected to the tx and rx pins on my micro arduino?

I am using my serial monitor to communicate through that port to the micro.

The Serial Monitor application does not do anything until you press the Send button, or hit enter (which is the shortcut key).

You need an application that sends characters as they are entered. NOTHING on the Arduino end can make the PC application send data any differently.

Would I want to use something like putty or terraterm for that?

Would I want to use something like putty or terraterm for that?

Whether you want to, or not, is immaterial.

You NEED to.

Would you be able to explain to me, like I'm 5, why that is? I don't have a lot of experience with this stuff.

How did you post your last message? I did not see each character appear as you typed. I didn't see it until you clicked the Post button.

You did see the data, so the data was sent from the keyboard to the screen as you typed. You did not have to hit enter after each character in order to see it.

If you want the data to go from your keyboard to the Arduino AS YOU TYPE IT, you can not use a system (like the Serial Monitor app) that buffers the data and sends it all at once.

i type it into the serial monitor and then send enter to send it to the arduino. I don't need it all to go to the arduino as i type it. I just want to be able to type a command, hit enter, and send that command to the arduino.

So the serial monitor "buffers" the data so I can only send so much data per command, correct? and are you saying that this is not the case with putty?

So the serial monitor "buffers" the data so I can only send so much data per command, correct?

The Serial Monitor app expects the send small amounts of data. You are free, of course, to abuse it and type novels. It will send the data as fast as it can, overflowing the serial buffer on the Arduino, if it is not reading the data fast enough.

and are you saying that this is not the case with putty?

Putty can be configured to send characters as you type them, or to buffer them and send them all at once.

How would you go about buffering them to send all at once?

Can you let us know how you set up your arduino console preferences for sending data?

Are you sending CR/LF or just LF or just CR?

I had it sending both CR and NL (new line) in the serial monitor

so you are saying that you type in something in the console that is set to send your text + \r\n to the arduino, the arduino receives this, does the right thing and then if you enter a new command and hit return it's not taken into account ?

side note:

what's the deal in your code with all the Strings duplication

  String command = arg[0];  // <-- why don't you use directly arg[0] ???
  command.reserve(5);       // <-- why do you reserve memory after copying something in the string ???
  String function = arg[1];// <-- why don't you use directly arg[1] ???
  function.reserve(4);// <-- why do you reserve memory after copying something in the string ???
  String var1 = arg[2];      // <-- why don't you use directly arg[2] ???
  var1.reserve(4);// <-- why do you reserve memory after copying something in the string ???

Correct, that is exactly what I am saying.

As for the code, I am not sure, I inherited this code and have not made too many changes aside from experimenting. I had thought that if i reserved memory, that it would be saved for the string and that I would make sure that the string itself was less than or equal to the amount of bytes that I reserved. This is probably wrong though I realize and not the way that I should've have tried to fix this probably. I am not sure if it is doing anything at all even.

I can try and just use directly the arg[x]'s to clean it up a bit.