Ok, sorry for not providing much detail.
Before I paste the code out fully, I think the issue might be exactly what you said: I'm reading the rfid through serial and trying to communicate with Processing through Serial. However, I am able to read the secondSensor in Processing fine. I don't understand why I can read one of the data streams, but not the other?
This is my Arduino code, which shows how its connected:
/*
Serial Call and Response
Language: Wiring/Arduino
This program sends an ASCII A (byte of value 65) on startup
and repeats that until it gets some data in.
Then it waits for a byte in the serial port, and
sends three sensor values whenever it gets a byte in.
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
The circuit:
* potentiometers attached to analog inputs 0 and 1
* pushbutton attached to digital I/O 2
Created 26 Sept. 2005
by Tom Igoe
modified 24 April 2012
by Tom Igoe and Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialCallResponse
*/
int val = 0;
char code[10];
int bytesread = 0;
String RFIDTAG="";
String DisplayTAG="";
int firstSensor = 4; // first analog sensor
int secondSensor = 0; // second analog sensor
int trackNum = 0;
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial.begin(2400);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(2, OUTPUT); // digital sensor is on digital pin 2
digitalWrite(2, LOW);
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
ReadSerial(RFIDTAG);
if (RFIDTAG == "190035D511"){
trackNum = 1;
}
if (RFIDTAG == "3600095756"){
trackNum = 2;
}
if (RFIDTAG == "190035E473"){
trackNum = 3;
}
if (RFIDTAG == "17007EFC59"){
trackNum = 4;
}
//firstSensor = 115;
//secondSensor = 111;
//thirdSensor = 533;
// send sensor values:
//Serial.write(firstSensor);
firstSensor = trackNum;
Serial.write(firstSensor);
Serial.write(secondSensor);
secondSensor ++;
//Serial.write(thirdSensor);
}
if(DisplayTAG!=RFIDTAG)
{
DisplayTAG=RFIDTAG;
Serial.println(RFIDTAG);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
void ReadSerial(String &ReadTagString)
{
int bytesread = 0;
int val = 0;
char code[10];
String TagCode="";
if(Serial.available() > 0) { // If data available from reader
if((val = Serial.read()) == 10) { // Check for header
bytesread = 0;
while(bytesread<10) { // Read 10 digit code
if( Serial.available() > 0) {
val = Serial.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
for(int x=0;x<10;x++) //Copy the Chars to a String
{
TagCode += code[x];
}
ReadTagString = TagCode; //Update the caller
while(Serial.available() > 0) //Burn off any characters still in the buffer
{
Serial.read();
}
//compare serial read to tag code
}
bytesread = 0;
TagCode="";
}
}
}
And this is my Processing code:
// This example code is in the public domain.
import processing.serial.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[2]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int rfid, rotary; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 2400);
}
void draw() {
background(bgcolor);
fill(fgcolor);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 2 bytes:
if (serialCount > 1 ) {
rfid = serialInArray[0];
rotary = serialInArray[1];
//fgcolor = serialInArray[2];
// print the values (for debugging purposes only):
println(rfid + "\t" + rotary + "\t");
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}