Pages: [1]   Go Down
Author Topic: Don't understand NewSoftSerial example...  (Read 1310 times)
0 Members and 1 Guest are viewing this topic.
Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All...

I'm trying to understand the NewSoftSerial example...

Code:
#include <NewSoftSerial.h>

NewSoftSerial mySerial(2, 3);

void setup() 
{
  Serial.begin(57600);
  Serial.println("Goodnight moon!");

  // set the data rate for the NewSoftSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop()                     // run over and over again
{

  if (mySerial.available()) {
      Serial.print((char)mySerial.read());
  }
  if (Serial.available()) {
      mySerial.print((char)Serial.read());
  }
}

I see tha it instansiates an instance of NewSoftSerial. Okay, simple enough.

Why in the setup function is he using the standard Serial library to set the port speed and output some text?

Then, why does he use NewSoftSerial to change the baud rate?

When i compile and run this example on my Uno, I open a terminal program, set it to the correct com port, then set the speed to 57600 and I see the Goodnight Moon text. But then I don't see Hello World. If I set my baud rate to 4800 I only see a little junk.

Can some kind soul please explain what this app is supposed to do?

Thanks...

Logged

North Wales
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like NewSoftSerial is setting up a serial link on pins 2 and 3 to communicate with another device, and using the standard serial port to let you know what is happening. In order to see the Hello World, you need to connect another serial device to pins 2 and 3 and set the baud rate to 4800 on that link.

edit: Looking at the constructor in the header file, pin 2 is the receive (Rx) pin, and pin 3 is the transmit (Tx) pin.
« Last Edit: February 05, 2011, 04:12:22 am by Paul J » Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The goodnight moon example uses 2 serial ports, the standard hardware one connected to the Serial Monitor.  The newsoftserial port is intended to be connected to another arduino running the same sketch and they have a conversation of sorts.  
Logged


Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh okay, thanks. Is there a way to use NewSoftSerial with the standard serial port?

I looked for docs on it, no joy...
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay so looking through the code, I see that there is only one constructor and it wants pin numbers for the IO. So does that mean I need to know which pins are connected to the UART, or does it mean I can only use this library for serial IO on the digital input/output pins?

Thanks...

Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it. If I instansiate NewSoftSerial with pins 0 and 1 it uses the system port. Maybe I'll add those values as defaults on the constructor, or else add a default constructor to my copy of the library.

Thanks everyone!
Logged

Pages: [1]   Go Up
Jump to: