Hi. I have an Arduino UNO and Mega. I am trying to use Software Serial, so the Mega can send data to the Uno. From there, the Uno can carry out a function.
Here is my code:
Mega Code:
//MEGA CODE
#include <SoftwareSerial.h>
SoftwareSerial mySerial(50, 14); // RX, TX
void setup() {
Serial.begin(4800);
mySerial.begin(4800);
}
void loop() { // run over and over
mySerial.write("Message From Mega to Uno");
delay(1000);
}
Uno Code:
//UNO CODE
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX
void setup() {
Serial.begin(4800);
mySerial.begin(4800);
Serial.print("Ready");
}
void loop() { // run over and over
Serial.print(mySerial.read());
}
But, on the Serial monitor, (of the Uno) I get a bunch of gibberish! (See attachment) What the mega code is supposed to do is send a bit of data (in this case send "Message From Mega to Uno") Then in the Uno code it is supposed to take that data and display it on the Serial Monitor. See the Attachment for what the Serial Monitor (Of the Uno, that is) looks like.
Pins 0 and 1 on the UNO are for the hardware serial port. So in effect you have both Serial and mySerial on the same pins. Move your SoftwareSerial to different pins.
Pin 14 on the Mega2560 is also a hardware serial pin, TX3. Why not use one of the hardware serial ports on the Mega anyway. There are 4.
OldSteve:
Pins 0 and 1 on the UNO are for the hardware serial port. So in effect you have both Serial and mySerial on the same pins. Move your SoftwareSerial to different pins.
Pin 14 on the Mega2560 is also a hardware serial pin, TX3. Why not use one of the hardware serial ports on the Mega anyway. There are 4.
Updated the code with new pins.
Mega:
//MEGA CODE
#include <SoftwareSerial.h>
SoftwareSerial mySerial(50, 51); // RX, TX
void setup() {
Serial.begin(4800);
mySerial.begin(4800);
}
void loop() { // run over and over
mySerial.write("Message From Mega to Uno");
delay(1000);
}
Uno Code:
//UNO CODE
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13); // RX, TX
void setup() {
Serial.begin(4800);
mySerial.begin(4800);
Serial.print("Ready");
}
void loop() { // run over and over
Serial.print(mySerial.read());
delay(1000);
}
When I activate the Uno alone, I get a bunch of "-1-1-1-1-1" on the Uno's Serial Monitor (I assume that means nothing is coming through the port.) When the activate the Mega, I get signals on the Serial Monitor! However, these signals are just random numbers, and mean nothing to me. I assume they are a 'code' for the incoming data, but then how do I decode these random numbers to what I want them to be? (Actual text!)
mySerial.read() returns -1 if there is no character received (red the reference). You can use mySerial.available() to check if there is actually something to read.
Yay! I got it transmitting!
Now I have some modified code on the Uno:
#include <SoftwareSerial.h>
SoftwareSerial Master(12, 13); // RX, TX
void setup()
{
Master.begin(4800);
}
void loop() {
char v = Master.read();
if (v == 'cca') {
// DO SOMETHING HERE
}
}
However, it does not do the thing I put in the "DO SOMETHING HERE" spot. From some testing with other code, I can see that the Mega does transmit 'cca.' However, the Uno does not seem to preforming the If statement! Can someone tell me why this is.
(I also modified the Mega code. Now it only transmits the data ONCE, rather than over and over again in the loop.)
You could look up 'strcmp()' or 'strncmp()' for string comparison. Just don't be tempted to use the "String" class - it's evil.
Also multiple ASCII characters are a C string. Must be nul terminated with '\0' and enclosed within double quotation marks.
'a' for a single character, "abcdef" for a string.
(The example you linked showed a single character comparison.)
Edit: Also, 'v' would need to be a char array, big enough for the full string plus terminating nul, not a single char variable.
As ieee488 said, "You really should put into some effort into actually learning the language instead of shotgunning this."