Thanks for pointing this horrible failure out to me before I ran into it. I couldn't find any reference to SoftwareSerial2 so I read up on interrupts on the Arduino. I didn't even realize hanging code would happen so I'm glad you pointed it out.
I ended up looking through the forums and found this post: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207173276/0#0
The code I found ended up working out great after I read a bit about attachInterrupt(). Edited the code and moved some things around and now I believe its working great.
Code:
// RFID reader for Arduino
// Wiring version by BARRAGAN http://people.interaction-ivrea.it/h.barragan
// Modified for Arudino by djmatic
// And pieces from Arduino Playground http://www.arduino.cc/playground/Learning/PRFID
// And then modified again by thekidd
// 12.04.2008 - Added interrupt to ensure program does not hang on RFID reader.
#include <avr/interrupt.h>
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#define ledPin 13
// set up a soft serial port
SoftwareSerial mySerial(rxPin, txPin);
int val = 0;
char code[10];
int bytesread = 0;
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT); // Set rxPin as INPUT to accept SOUT from RFID pin
pinMode(txPin, OUTPUT); // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(ledPin, OUTPUT); // Let the user know whats up
// set the data rate for the serial ports
mySerial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
Serial.begin(9600); // Serial feedback for debugging in Wiring
// say something
Serial.println("Hello World!");
attachInterrupt(0, readRFID, CHANGE); // Setup interrupt for SOUT on RFID reader
digitalWrite(txPin, LOW); // Activate the RFID reader
}
void loop() {
// Do Something
}
void readRFID() {
digitalWrite(ledPin, LOW); // Turn off debug LED - Event triggered!
if((val = mySerial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
val = mySerial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
if(bytesread == 10) { // If 10 digit read is complete
digitalWrite(txPin, HIGH); // deactivate RFID reader
digitalWrite(ledPin, HIGH); // activate LED to show an RFID card was read
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
}
bytesread = 0;
digitalWrite(txPin, LOW); // Activate the RFID reader
}
}
I hope this accomplishes what you brought up as I am still trying to grasp C/Wiring.
Now I'm wondering if I can do the same thing with the GPS. I guess not since its always sending in NEMA mode. Maybe using Parallax GPS' smart mode is the way to go but I'm going to guess that smart mode isn't integrated on all GPS units.
I know for sure that I do not want to set it up on hardware serial. I thinking I need the serial port for Xbee. Any ideas how the GPS could be setup on software serial without creating the hang ups you mentioned John? Oh and does anyone see any problems with the code?
Help is much appreciated!