Hi! This is my first post, so forgive me if this is in the wrong category. Currently I have two Arduinos, one Uno and one Mega2560 Rev3. I want the Mega to send GCode data over serial to the Uno, which will then command a CNC shield to step via GRBL. Through the IDE (with Uno selected) I can type something like "G01 x10" and have my X motor move 10 steps.
However, when I try to send ANY data from the Mega to the Uno, whether or not GRBL is on the Board, it receives nothing. I have read the Serial Input Basics thread, and copied the fifth example's code over, and it still receives nothing, with or without GRBL on the board. I have done Mega to Mega communication before, and it has worked fine with that code, so I don't know why it isn't working now. Wiring is UNO Rx -> MEGA Tx, UNO Tx -> MEGA RX, and UNO GND -> MEGA GND. I have read that I should use Serial1 instead of Serial, and even then it hasn't worked.
Hi! As I am using a CNC Shield, all available pins are taken and I'm 95% sure they all have some use by the shield. I currently have a second shield below the CNC shield which has RX, TX, and GND soldered in, as that would be the only way to use rx/tx while the shield is on. As every pin has a use, RX and TX would be the only open pins to send data to/from as they wouldn't be used for anything else. If this is not the case, I will give SoftwareSerial a shot. Thanks!
Correction, it was Example 5, not Example 3. The Uno does not receive any data sent by the Mega, regardless of if that is a single byte or a string. The RX light doesn't light up at all either, and I'm unsure why this is.
UNO code
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}
However, it would seem GRBL removes my ability to use things such as Serial.read(), so I wouldn't be able to use this with GRBL. I believe it does read any data sent in automatically though, since it can respond to data sent from my laptop.
Do not use RX and TX on the Uno, they are intended for program uploading and debugging and are connected internally to the chip that communicates with the PC. Use SoftwareSerial instead on the Uno, and any two digital pins for RX and TX.
On the Mega, use hardware Serial1 or Serial2 or Serial3. The corresponding pins are indicated on the pinout diagram.
I am almost sure it is working??? Post an annotated schematic, not a frizzy showing exactly how you have wired this. Show all hardware items even resistors and capacitors. Show all power sources. Be sure to include links to all hardware items.
I am 95% sure that all pins of the CNC shield are being used, sorry if that wasn't clear.
Attached is the schematic, I hope it's enough. Link to the purchased CNC Shield, the UNO was included with the shield. I got my Mega2560 a long time ago, and I am unsure where from, I believe it is from Elegoo though.
All digital pins have a use in either stepping or stopping a motor on x, y, or z. A4 and A5 connect to I2C on the board, so I might be able to use those, and RX/TX are free.
Hi, sorry for the late response. That seemed to work, I was unaware that the uno had to be unplugged! Thanks all for your help.
For the sake of completion I will post the code I am running
Now, on my mega, I'm running this code
void setup() {
Serial.begin(9600);
}
void loop() {
delay(1000);
Serial.write("$\n"); //this is the equivalent of a "help" command for GRBL. \n is needed because it expects to be on a terminal.
Serial.println(Serial.readString()); //only returns numbers otherwise
}
And on my Uno (no longer connected to laptop, powered by 9v battery)