Reference.SoftwareSerialExample History

Hide minor edits - Show changes to markup

January 07, 2012, at 06:21 PM by Tom Igoe -
Deleted line 0:
Changed line 5 from:
to:
September 12, 2011, at 05:58 PM by Scott Fitzgerald -
Changed line 6 from:
to:
September 12, 2011, at 05:58 PM by Scott Fitzgerald -
Deleted line 1:
Changed lines 4-6 from:

[@ /*

  SoftwareSerial example
to:
Changed lines 6-55 from:
  Sample of the SoftwareSerial library.  Listens for serial in on pin 2
  and sends it out again on pin 3.

  by Tom Igoe
  based on examples by David Mellis and Heather Dewey-Hagborg
  written: 6 Jan 2007

  • /

// include the SoftwareSerial library so you can use its functions:

  1. include <SoftwareSerial.h>

  2. define rxPin 2
  3. define txPin 3
  4. define ledPin 13

// set up a new serial port SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); byte pinState = 0;

void setup() {

  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

}

void loop() {

  // listen for new serial coming in:
  char someChar = mySerial.read();
  // print out the character:
  mySerial.print(someChar);
  // toggle an LED just so you see the thing's alive.  
  // this LED will go on with every OTHER character received:
  toggle(13);

}

void toggle(int pinNum) {

  // set the LED pin using the pinState variable:
  digitalWrite(pinNum, pinState); 
  // if pinState = 0, set it to 1, and vice versa:
  pinState = !pinState;

}

@]

to:
January 06, 2007, at 06:08 PM by Tom Igoe -
Added lines 1-2:
January 06, 2007, at 06:07 PM by Tom Igoe -
Added lines 1-56:

This is a very basic example of SoftwareSerial. It reads in a byte at a time and mirrors it back to you:

/*
  SoftwareSerial example

  Sample of the SoftwareSerial library.  Listens for serial in on pin 2
  and sends it out again on pin 3.

  by Tom Igoe
  based on examples by David Mellis and Heather Dewey-Hagborg
  written: 6 Jan 2007


*/

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define ledPin 13

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  // listen for new serial coming in:
  char someChar = mySerial.read();
  // print out the character:
  mySerial.print(someChar);
  // toggle an LED just so you see the thing's alive.  
  // this LED will go on with every OTHER character received:
  toggle(13);

}


void toggle(int pinNum) {
  // set the LED pin using the pinState variable:
  digitalWrite(pinNum, pinState); 
  // if pinState = 0, set it to 1, and vice versa:
  pinState = !pinState;
}


Share