Thanks in advance to anyone who has the patience to read all of this and help me learn. I’ve highlighted specific questions and pleas for help in red
I’m attempting to learn about serial communications, particularly how to use SoftwareSerial on a Sparkfun Redboard (Arduino Uno). However I’m having some trouble understanding the basics, even after going through some of the easy examples I found online.
I’ve seen a couple examples very similar to the one below (originally seen in this discussion, https://forum.arduino.cc/index.php?topic=161151.0), but I can’t get it to work. From my understanding, I should be able to connect by USB to the Arduino, load this sketch, and then whatever I type in the Arduino IDE serial monitor should get echoed back to me. (Additionally, if I had a device hooked up that was sending responses, this sketch would display the responses to the serial monitor, but I’m not doing that yet.)
I attempted to comment on the sketch below to illustrate how I understand the statements, but please correct my comments if I’ve misunderstood a concept.
#include<SoftwareSerial.h> //Include the SoftwareSerial library
//Setup the mySerial object. We will transmit to the object using Pin 3 and receive
//from the object using Pin 2
mySerial.begin(9600); //Open the mySerial port for communication
Serial.begin(9600); //Open the serial port for communication
if (mySerial.available()) //Has the mySerial object received something? (i.e. have we
//typed something into the serial monitor and transmitted
//it to the object using Pin 3?)
Serial.write(mySerial.read()); //If yes, read the data going to the object and
//display it to the serial monitor. Basically we are
//echoing what we sent.
if (Serial.available()) //Is there data waiting in the receive buffer of the serial port?
//(i.e. has the object transmitted something that was received
//on Pin 2 which is now waiting in the serial port receive
mySerial.write(Serial.read()); //If yes, read what is in the serial buffer, and
//transmit it back to the object. I have particular
//trouble understanding this line. If mySerial sent me
//something and it’s sitting in the serial receive
//buffer, why wouldn’t we display it to the serial
//monitor? Instead it looks like we’re sending it right
//back to the mySerial object.