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.begin(9600);
Mouse.begin();
Keyboard.begin();
serial_com.println("--HID USB Mouse Emulator--");
serial_com.println("Enter 'help' for a list of valid commands.");
serial_com.println("Ready.");
serial_com.println("Enter Command >");
}
void loop() {
if(serial_com.available()){
read_line();
read_command();
serial_com.print("Enter Command> ");
Keyboard.press(KEY_RETURN);
Keyboard.releaseAll();
}
}
void read_command(){
String command = arg[0];
command.reserve(5);
if (command == "mouse") {
mouse_functions();
}
else if (command == "key") {
keyboard_functions();
}
else if (command == "help")
help();
else
error_cmd();
error = false;
}
void keyboard_functions() {
String function = arg[1];
if (function == "cat") {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('`');
Keyboard.releaseAll();
}
else
error_arg();
if (!error)
serial_com.println(" Keyboard done!");
}
void mouse_functions() {
String function = arg[1];
function.reserve(4);
String var1 = arg[2];
var1.reserve(4);
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.
delay(responseDelay);
}
else if (function == "y") {
Mouse.move(0, var1.toInt(), 0); //Moves the mouse cursor up or down.
delay(responseDelay);
}
else if (function == "l") {
Mouse.click(MOUSE_LEFT); //Send mouse left-click
}
else if (function == "r") {
Mouse.click(MOUSE_RIGHT); //Send mouse right-click
}
else if (function == "bgn") {
Mouse.begin();
}
else if (function == "end") {
Mouse.end();
}
else
error_arg();
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]);
check_quotes(data);
}
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)serial_com.read();
last_byte_time = millis();
//serial_com.write(inChar);
if (inChar == '\n') {
inputString.trim();
serial_com.println(inputString);
read_args(inputString);
break;
}
inputString += inChar;
}
elapsed_time = millis() - last_byte_time;
if (elapsed_time > 1000) { //1 second timeout
serial_com.println("Timeout exceeded");
break;
}
}
}
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);
}
}
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.
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?
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.
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 ???
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.