LEDHead - serial problem

I am trying to get a LEDHead board by Rogue Robotics to read in from a software serial port (NewSoftSerial) with an RFID reader and place that serial data onto the first serial port of the LEDHead which has a Seeedstudio Bluetooth Bee hooked up. Unfortunately, while the RFID reader works for a straight to serial (Serial1 -> Serial) it seems not to be working under this new program. As a note, I want to use the second serial port (Serial1) for a Argent Radio Shield, so hooking the RFID reader to the second serial port is not an option.

The Bluetooth Bee will connect to both my Android phone and my Mac Mini, but no output from the RFID is seen on either device.

I'll let the code do the rest of the talking for me as I've hit a brick wall...!

//Arduino RFID Bluetooth Modem Code v4.01
//By Shane Morris, Gothuro Morris Rail Industries
//4th August 2011

//Happy Birthday Grace <3

#include <NewSoftSerial.h>   //Software Serial Port
#define RxD 5
#define TxD 4
 
#define DEBUG_ENABLED  1
 
NewSoftSerial rfidSerial(RxD,TxD);

 byte data[5];  //For holding the ID we receive
 int val = 0;

 void setup()
 {
   pinMode(RxD, INPUT);
   pinMode(TxD, OUTPUT);
   setupBlueToothConnection();
  
   // Start serial port 19200 bps
   //Serial1.begin(4800);
   rfidSerial.begin(19200);
   
   // Setting Auto Read Mode - EM4102 Decoded Mode - No password
   // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
   rfidSerial.print(0xFF,BYTE);  //Header
   rfidSerial.print(0x01,BYTE);  //Reserved
   rfidSerial.print(0x09,BYTE);  //Length (Command + Data)
   rfidSerial.print(0x87,BYTE);  //Command (0x87 sets auto mode behavior
   rfidSerial.print(0x01,BYTE);  //Data 1: Enable Auto-Read
   rfidSerial.print(0x03,BYTE);  //Data 2: Mode – Parity decoded – Manchester RF/64
   rfidSerial.print(0x02,BYTE);  //Data 3: Total number of block to be read (2)
   rfidSerial.print(0x00,BYTE);  //Data 4: No password expected
   rfidSerial.print(0x10,BYTE);  //Data 5: Password byte 1
   rfidSerial.print(0x20,BYTE);  //Data 6: Password byte 2
   rfidSerial.print(0x30,BYTE);  //Data 7: Password byte 3
   rfidSerial.print(0x40,BYTE);  //Data 8: Password byte 4
   rfidSerial.print(0x37,BYTE);  //Checksum
   
   delay(500);
   rfidSerial.flush();
   /*Serial.flush();
   Serial.println();
   Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");
 */
 }

 void loop()
 {
 
   val = rfidSerial.read();
   while (val != 0xff)
   {  //On Successful read, first byte will always be 0xFF
      val = rfidSerial.read();
      delay(1000);
   }
   
   //we already read the header (0xff)
   rfidSerial.read();              // reserved
   rfidSerial.read();              // length
   rfidSerial.read();              // command (indicates tag data)
   data[0] = rfidSerial.read();    // we read data 1
   data[1] = rfidSerial.read();    // we read data 2
   data[2] = rfidSerial.read();    // we read data 3
   data[3] = rfidSerial.read();    // we read data 4
   data[4] = rfidSerial.read();    // we read data 5
   rfidSerial.read();              // checksum
   
   // Indentify RFID Card
   /*
   boolean j_card = true;
   boolean d_card = true;
   */
   Serial.println("\r\n");
   Serial.println("Card found - Code: ");
   for (int i=0; i<5; i++)
   {
     if (data[i] < 16) Serial.println("0");
     Serial.print(data[i], HEX);
     Serial.print(0x00, HEX);
      /*
     //cross-check
     if (data[i] != jeremy[i]) j_card = false;
     if (data[i] != david[i]) d_card = false;
 */  
 }
   //Serial.println();
   
 }
 
 void setupBlueToothConnection()
{
    Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
    delay(1000);
    Serial.print("\r\n+STWMOD=0\r\n");
    Serial.print("\r\n+STNA=SeeeduinoBluetooth\r\n");
    Serial.print("\r\n+STAUTO=0\r\n");
    Serial.print("\r\n+STOAUT=1\r\n");
    Serial.print("\r\n+STPIN=0000\r\n");
    delay(2000); // This delay is required.
    Serial.print("\r\n+INQ=1\r\n");
    delay(2000); // This delay is required.
}

As you can see, I've kludged together some example code where necessary, and adapted it to work, but this whole problem is a little vexing as I'm not sure where the problem lies...!

Thanks in advance for any help you can give.

Boris.

As you can see, I've kludged together

Kludge is right.

NewSoftSerial rfidSerial(RxD,TxD);

 byte data[5];  //For holding the ID we receive
 int val = 0;

 void setup()
 {
   pinMode(RxD, INPUT);
   pinMode(TxD, OUTPUT);

NewSoftSerial owns those pins. Why are you mucking with them?

   while (val != 0xff)
   {  //On Successful read, first byte will always be 0xFF
      val = rfidSerial.read();
      delay(1000);
   }

Get rid of the stupid delay. The while loop won't terminate until a 0xff arrives. There is no reason to sit around doing nothing for a full second after each failure to read a 0xff.

   //we already read the header (0xff)
   rfidSerial.read();              // reserved
   rfidSerial.read();              // length
   rfidSerial.read();              // command (indicates tag data)
   data[0] = rfidSerial.read();    // we read data 1
   data[1] = rfidSerial.read();    // we read data 2
   data[2] = rfidSerial.read();    // we read data 3
   data[3] = rfidSerial.read();    // we read data 4
   data[4] = rfidSerial.read();    // we read data 5
   rfidSerial.read();              // checksum

A few nano-seconds after the 0xff arrives, you expect 9 values to also have arrived. You seem to have a misunderstanding about how (slow) serial data transmission works.

but this whole problem is a little vexing as I'm not sure where the problem lies...!

What is also vexing is that you haven't even described what the problem is. You talk about Serial1, but there is no reference to Serial1 in the code (which only exists on a Mega).

Some links to the devices in question would be useful, along with a clear description of the problem. What exactly works? What exactly doesn't work?

Ok, while I may have the bull by the horns in terms of the speed of the serial line, I most definitely don't have said bull by the horns when it comes to knowing what my device has.

Feast your eyes on: Rogue Robotics rDuino LEDHead - the device is a 644P, which I believe is also used in the Sangiuno... so your Mega comment is wildly misinformed.

I thought it would be obvious describing what the program should do would allow for an explanation of what it is not doing. But, for the record, put quite simply, the device is not, in terms of the program, reading the RFID value, and then transmitting it over the Bluetooth link to either device (computer or phone).

The LEDs light up on the RFID reader, so it is reading. This has also been verified by simple code over the USB link. The Bluetooth Bee is also connecting, the LEDs and the devices are indicating a connection. So, ergo, the problem lies in the code somewhere.

I am not the best at code, my speciality lies in designing the hardware and simple logic. I understand conceptually what the code is saying, but not its intricate specifics. Even so, I know alot more about Arduino code than say, C or Pascal.

I will, however, take your tips. But I offer one in return - I have been on the old Arduino forum and found the people there rather helpful and encouraging. Now, you might be having a bad day, a plane could've fell out of the sky, who knows? But it is no reason to be condescending to someone over a misunderstanding in code, let alone something that you don't know about. I'd rather no answer than a lecture.

Have a good one.

Boris.

Feast your eyes on: Rogue Robotics rDuino LEDHead - the device is a 644P, which I believe is also used in the Sangiuno... so your Mega comment is wildly misinformed.

The 644P is NOT an Arduino. The Sanguino is NOT an Arduino. They are clones/imitations. The only real Arduino that has multiple hardware serial ports is the Mega. So, no, my comment was not misinformed.

for the record, put quite simply, the device is not, in terms of the program, reading the RFID value, and then transmitting it over the Bluetooth link to either device (computer or phone).

So, the key is to figure out WHERE the failure occurs. Is it getting a value, but not sending it anywhere, or is it not getting a value, and therefore has nothing to send?

I pointed out some issues in the code. Is it safe to assume that you have corrected them and still have the issue? If so, post the modified code.

I'd rather no answer than a lecture.

Then feel free to not answer.

So what was "Is there a problem with my code?" has quickly turned into a pointing match of "But its a clone! Thats an imitation!"

They can be programed in a similar fashion to an Arduino. They have cores similar to an Arduino. Semantics aside, they are an Arduino, if not formed in the spirit of the community, then the spirit of the people who use them.

If you have nothing intelligent to say Paul, I really would prefer no answer.

Does anyone have anything intelligent to say about my code?

I'd like to help you. Really.

But, I'm confused about the relationship between Serial1 mentioned in your initial post and the code that you posted which doesn't mention Serial1 at all.

I merely observed that Serial1 is only supported on the Mega, which I hoped would lead you to describe what it is you actually have (which you did). You then accused me of being misinformed, when in reality, I was uninformed. And, whose fault was that?

Regardless, have you done anything about the code issues I pointed out? If so, what, and did the changes make any difference?

Ok, I can accept uninformed. But I'm certain you would've known about the 644P used in the Sanguino...?

I have not made the modifications yet, as instructed, as it is almost 11pm here, and I have some other, much more pressing matters that need attention. I can make the modifications tomorrow afternoon, and post back here. Obviously, first on the list is those serial concerns.

Serial1 isn't mentioned in the code as it addresses the Radio Shield, which isn't hooked up, but in the final (!) software revision will mirror the output to the Bluetooth Bee (so, same data goes to both hardware serial ports).

Something that warrants investigation is whether the NewSoftserial library will support 19200 baud comms - I am unsure, but I have read there is indeed a limitation. That too, I will do tomorrow afternoon.

I suppose, explaining this complex a system, and keeping track of the serial ports as I go through the "modules" (RFID -> USB, Bluetooth -> phone/ computer, and Radio Shield -> radio network) is a concern, and I still don't possess another RF modem to at least decode the data transmitted over the PMR network, I have to go by sound alone. I do know what packet data should sound like, thankfully.

Another thing is, alot of other people would do this differently, someone suggested I use NFC which unfortunately didn't exist for the Arduino until very recently, and I am unable to program on the phone side of things due to lack of libraries for App Inventor. Additionally, the PMR network would not be addressed either by a commodity mobile phone.

So, until tomorrow, I bid you good night, or good morning, as it happens to be where you are...!

Something that warrants investigation is whether the NewSoftserial library will support 19200 baud comms - I am unsure, but I have read there is indeed a limitation. That too, I will do tomorrow afternoon.

NewSoftSerial supports baud rates up to 115200.

Ok, I've done some investigation, and made the modifications you suggested - I've commented them out for the time being.

It seems, after accessing the Bluetooth Bees serial line from ZTerm, the Bluetooth Bee does indeed set up correctly, then theres nothing. This is while the red LED lights up on the RFID Bee, indicating a successful read. So the problem is isolated in the section of code that goes from the software serial port to the hardware serial port. What, exactly, the problem is, I am unable to tell you.

I'll post my modified code, please note the comments everywhere now (!).

//Arduino RFID Bluetooth Modem Code v4.01
//By Shane Morris, Gothuro Morris Rail Industries
//4th August 2011

//Happy Birthday Grace <3

#include <NewSoftSerial.h>   //Software Serial Port
#define RxD 5
#define TxD 4
 
#define DEBUG_ENABLED  1
 
NewSoftSerial rfidSerial(RxD,TxD);

 byte data[5];  //For holding the ID we receive
 int val = 0;

 void setup()
 {
//   pinMode(RxD, INPUT);
//   pinMode(TxD, OUTPUT);
   setupBlueToothConnection();
  
   // Start serial port 19200 bps
   //Serial1.begin(4800);
   rfidSerial.begin(19200);
   
   // Setting Auto Read Mode - EM4102 Decoded Mode - No password
   // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
   rfidSerial.print(0xFF,BYTE);  //Header
   rfidSerial.print(0x01,BYTE);  //Reserved
   rfidSerial.print(0x09,BYTE);  //Length (Command + Data)
   rfidSerial.print(0x87,BYTE);  //Command (0x87 sets auto mode behavior
   rfidSerial.print(0x01,BYTE);  //Data 1: Enable Auto-Read
   rfidSerial.print(0x03,BYTE);  //Data 2: Mode – Parity decoded – Manchester RF/64
   rfidSerial.print(0x02,BYTE);  //Data 3: Total number of block to be read (2)
   rfidSerial.print(0x00,BYTE);  //Data 4: No password expected
   rfidSerial.print(0x10,BYTE);  //Data 5: Password byte 1
   rfidSerial.print(0x20,BYTE);  //Data 6: Password byte 2
   rfidSerial.print(0x30,BYTE);  //Data 7: Password byte 3
   rfidSerial.print(0x40,BYTE);  //Data 8: Password byte 4
   rfidSerial.print(0x37,BYTE);  //Checksum
   
   delay(500);
 //  rfidSerial.flush();
   /*Serial.flush();
   Serial.println();
   Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");
 */
 }

 void loop()
 {
 
   val = rfidSerial.read();
   while (val != 0xff)
   {  //On Successful read, first byte will always be 0xFF
      val = rfidSerial.read();
   //   delay(1000);
   }
   
   //we already read the header (0xff)
   rfidSerial.read();              // reserved
   rfidSerial.read();              // length
   rfidSerial.read();              // command (indicates tag data)
   data[0] = rfidSerial.read();    // we read data 1
   data[1] = rfidSerial.read();    // we read data 2
   data[2] = rfidSerial.read();    // we read data 3
   data[3] = rfidSerial.read();    // we read data 4
   data[4] = rfidSerial.read();    // we read data 5
   rfidSerial.read();              // checksum
   
   // Indentify RFID Card
//   Serial.println("\r\n");
   Serial.println("Card found - Code: ");
   for (int i=0; i<5; i++)
   {
     if (data[i] < 16) Serial.println("0");
     Serial.print(data[i], HEX);
  //   Serial.print(0x00, HEX);
 }
   //Serial.println();
   
 }
 
 void setupBlueToothConnection()
{
    Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
    delay(1000);
    Serial.print("\r\n+STWMOD=0\r\n");
    Serial.print("\r\n+STNA=SeeeduinoBluetooth\r\n");
    Serial.print("\r\n+STAUTO=0\r\n");
    Serial.print("\r\n+STOAUT=1\r\n");
    Serial.print("\r\n+STPIN=0000\r\n");
    delay(2000); // This delay is required.
    Serial.print("\r\n+INQ=1\r\n");
    delay(2000); // This delay is required.
}

There is a string sent over the serial ("Card found") that is also not transmitting. This is independent of the serial data from the RFID Bee, leaving me to think, just possibly, the serial data actually isn't being accepted by the Bluetooth Bee after its setup and connected. Which seems odd, I had it working. Admittedly, that was with a character being sent from the phone to the Bee, then a string from the Bee to the phone... something else that warrants investigation - am I using the right mode on the Bluetooth Bee?

Ok, I've ruled out the Bluetooth Bee, as I have disconnected it, and made a program (after the break) to just place the RFID value onto the Serial line.

Code is as follows:

//Arduino RFID Bluetooth Modem Code v4.01
//By Shane Morris, Gothuro Morris Rail Industries
//4th August 2011

//Happy Birthday Grace <3

#include <NewSoftSerial.h>   //Software Serial Port
#define RxD 5
#define TxD 4
 
#define DEBUG_ENABLED  1
 
NewSoftSerial rfidSerial(RxD,TxD);

 byte data[5];  //For holding the ID we receive
 int val = 0;

 void setup()
 {
  
   // Start serial port 19200 bps
   Serial.begin(9600);
   //Serial1.begin(4800);
   rfidSerial.begin(19200);
   
   // Setting Auto Read Mode - EM4102 Decoded Mode - No password
   // command: FF 01 09 87 01 03 02 00 10 20 30 40 37
   rfidSerial.print(0xFF,BYTE);  //Header
   rfidSerial.print(0x01,BYTE);  //Reserved
   rfidSerial.print(0x09,BYTE);  //Length (Command + Data)
   rfidSerial.print(0x87,BYTE);  //Command (0x87 sets auto mode behavior
   rfidSerial.print(0x01,BYTE);  //Data 1: Enable Auto-Read
   rfidSerial.print(0x03,BYTE);  //Data 2: Mode – Parity decoded – Manchester RF/64
   rfidSerial.print(0x02,BYTE);  //Data 3: Total number of block to be read (2)
   rfidSerial.print(0x00,BYTE);  //Data 4: No password expected
   rfidSerial.print(0x10,BYTE);  //Data 5: Password byte 1
   rfidSerial.print(0x20,BYTE);  //Data 6: Password byte 2
   rfidSerial.print(0x30,BYTE);  //Data 7: Password byte 3
   rfidSerial.print(0x40,BYTE);  //Data 8: Password byte 4
   rfidSerial.print(0x37,BYTE);  //Checksum
   
   delay(500);
 //  rfidSerial.flush();
   //Serial.flush();
   Serial.println();
   Serial.println("RFID module started in Auto Read Mode, Waiting for Card...");
 }

 void loop()
 {
 
   val = rfidSerial.read();
   while (val != 0xff)
   {  //On Successful read, first byte will always be 0xFF
      val = rfidSerial.read();
   //   delay(1000);
   }
   
   //we already read the header (0xff)
   rfidSerial.read();              // reserved
   rfidSerial.read();              // length
   rfidSerial.read();              // command (indicates tag data)
   data[0] = rfidSerial.read();    // we read data 1
   data[1] = rfidSerial.read();    // we read data 2
   data[2] = rfidSerial.read();    // we read data 3
   data[3] = rfidSerial.read();    // we read data 4
   data[4] = rfidSerial.read();    // we read data 5
   rfidSerial.read();              // checksum
   
   
   Serial.println("\r\n");
   Serial.println("Card found - Code: ");
   for (int i=0; i<5; i++)
   {
     if (data[i] < 16) Serial.println("0");
     Serial.print(data[i], HEX);
  //   Serial.print(0x00, HEX);
     
 }
   //Serial.println();
   
 }

The message "Waiting for card" appears, but unfortunately, the "Card found" along with the card number doesn't. Hopefully this gives you some insight.

   //we already read the header (0xff)
   rfidSerial.read();              // reserved
   rfidSerial.read();              // length
   rfidSerial.read();              // command (indicates tag data)
   data[0] = rfidSerial.read();    // we read data 1
   data[1] = rfidSerial.read();    // we read data 2
   data[2] = rfidSerial.read();    // we read data 3
   data[3] = rfidSerial.read();    // we read data 4
   data[4] = rfidSerial.read();    // we read data 5
   rfidSerial.read();              // checksum

You have just received the header to break out of the while loop, but the rest of the data has not yet arrived. But, you read it anyway...

The message "Waiting for card" appears, but unfortunately, the "Card found" along with the card number doesn't. Hopefully this gives you some insight.

This tells me that the rfidSerial instance is never receiving the header, or possibly any data. It looks like either the rfid is not connected, is not connected correctly (to the tx pin, the rx pin, and ground), or is not sending the data that you think it is.

Try printing everything received, to narrow down the problem.

Hello again,

Unfortunately real life got in the way of the project. My apoligies.

You're saying, with the while loop, we've broken it, and yet, there may not be some data there to read, is that correct? My understanding of the NewSoftSerial library is that its an interrupt based scheme, whereas a hardware serial port is rather different... The reason I mention this is because I had the RFID Bee working on the hardware serial port as part of test code, but then moved to the software serial to accomodate the Bluetooth Bee (on Serial), and eventually the Radio Shield (on Serial1).

So it seems prudent to allow a (small!) delay to give the RFID Bee time to send, the Arduino time to catch up. Or perhaps I am barking up the wrong tree?

If there is indeed an issue with the RFID Bee and the NewSoftSerial that can't be ironed out, then all I'd need to do is a small rewire to make the RFID Bee on Serial1, and the Radio Shield on NewSoftSerial. As it stands, the Radio Shield doesn't receive data, it only transmits it. Nor do I want to implement the code to receive on the PMR section as its not needed for my application.

These are just thoughts I'm tossing around as I ease my mind back into the thinking I need to get this working.

I've come to the realisation that for some reason, NewSoftSerial doesn't play well with my RFID Bee. No matter. I've now coded using the second hardware serial port on the LEDHead, and gotten some output...!

First, the code:

#include <NewSoftSerial.h>
#define TxD 4
#define RxD 5

NewSoftSerial modemSerial(RxD, TxD);

byte data[5];  //For holding the ID we receive

void setup() {
  setupBlueToothConnection();
  //modemSerial.begin(4800);
  Serial1.begin(19200);
}

void loop() {
  if (Serial1.available() > 0) {
    Serial1.read();
    Serial1.read();
    Serial1.read();
    Serial1.read();
    data[0] = Serial1.read();
    data[1] = Serial1.read();
    data[2] = Serial1.read();
    data[3] = Serial1.read();
    data[4] = Serial1.read();
    Serial1.read();
     Serial.println("Card found - Code: ");
  for (int i=0; i<5; i++)
  {
    if (data[i] < 16) Serial.println("0");
    Serial.print(data[i], HEX);
    Serial.print(0x00, HEX);
  }
    //Serial1.println(val2, HEX);
  }
}

void setupBlueToothConnection()
{
  Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  delay(1000);
  Serial.print("\r\n+STWMOD=0\r\n");
  Serial.print("\r\n+STNA=Modem\r\n");
  Serial.print("\r\n+STAUTO=0\r\n");
  Serial.print("\r\n+STOAUT=1\r\n");
  Serial.print("\r\n+STPIN=0000\r\n");
  delay(2000); // This delay is required.
  Serial.print("\r\n+INQ=1\r\n");
  delay(2000); // This delay is required.
}

This is a very stripped down version of what I was using before, since I knew the Bluetooth code worked, no need to change that. I took out the "while" loop that prevented access to the routine to read the RFID card, and got this output:

ü

+STWMOD=0



+STNA=Modem



+STAUTO=0



+STOAUT=1



+STPIN=0000



+INQ=1

Card found - Code: 

FF0FF0FF0FF0FF0Card found - Code: 

0

00230460520210Card found - Code: 

FF0FF0FF0FF0FF0Card found - Code: 

0

002304802C0FD0

That was the output when I waved two different RFID cards over the reader. Now I'll send the output to NewSoftSerial, being the Radio Shield. As noted in my last post, its not going to receive any data, just send it.

Of course, any help with getting the output to an acceptable level would be great, I honestly don't know where the "FF0FF..." comes from, and realising this system will be using a battery to power it, additionally, the phone will have limited power too...