Pages: [1]   Go Down
Author Topic: Help: RFID and serLCD  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

I am slightly new to Arduino and their products but one of my friends at University got me into it, and I am using it to do a Uni project.

I am using a serial enabled LCD from Seeeduino ( Backpack (http://littlebirdelectronics.com/products/serial-enabled-lcd-backpack) and just a normal 2x16 5V LCD screen attached) and also a RFID module (http://littlebirdelectronics.com/products/125khz-rfid-module-uart).

I have got both code of these elements working on their own, thanks to the internet, but am currently struggling to get them to work together.

I am not exactly sure what the issue is. But it only stops working when i include the LCD coding, including LCD.begin etc.

At the moment I am only trying to make the LCD output the value of the RFID card.

Thanks for your help in advance!
Willing to read anything you have to throw at me!

Code:
#include <SoftwareSerial.h>

SoftwareSerial RFID = SoftwareSerial(2, 2);
SoftwareSerial LCD = SoftwareSerial(0, 3);

const int LCDdelay=100;

void lcdPosition(int row, int col)
{
  LCD.write(0xFE);   //command flag
  LCD.write((col + row*64 + 128));    //position
  delay(LCDdelay);
}

void clearLCD()
{
  LCD.write(0xFE);   //command flag
  LCD.write(0x01);   //clear command.
  delay(LCDdelay);
}

void backlightOn()
{  //turns on the backlight
  LCD.write(0x7C);   //command flag for backlight stuff
  LCD.write(157);    //light level.
  delay(LCDdelay);
}

void backlightOff()
{  //turns off the backlight
  LCD.write(0x7C);   //command flag for backlight stuff
  LCD.write(128);     //light level for off.
   delay(LCDdelay);
}

void serCommand()
{   //a general function to call the command flag for issuing all other commands   
  LCD.write(0xFE);
}

void setup()
{
  pinMode(3, OUTPUT);
  Serial.begin(9600);
  RFID.begin(9600);
  LCD.begin(9600);      // maximum bandwidth issue
}

void loop()
{
  receiveRFIDCardInfo();
}

void receiveRFIDCardInfo()
{
  static byte data[4];
  static byte temp[14];
  byte len;
  static int i = 0;
  unsigned long currentId;
  if(RFID.available())
  {
      temp[i++] = RFID.read();
      if(14 == i)
      {
        if( 0x02 == temp[0] && 0x03 == temp[13])
        {
          data[0] = Transform(temp[3])*16 + Transform(temp[4]);
          data[1] = Transform(temp[5])*16 + Transform(temp[6]);
          data[2] = Transform(temp[7])*16 + Transform(temp[8]);
          data[3] = Transform(temp[9])*16 + Transform(temp[10]);
          currentId = (unsigned long)data[0]*16777216 + (unsigned long)data[1]*65536 + (unsigned long)data[2]*256 + (unsigned long)data[3];
          Serial.println("\nCurrentID:");
          Serial.print(currentId,DEC);
          i=0;
         
          clearLCD();
          lcdPosition(0,0);
          LCD.print(currentId, DEC);
        }
      }
  }
}

byte Transform(byte dat)
{
  if(dat >= 0x30 && dat <= 0x39)
  {
    return (dat - 0x30);
  }else if(dat >= 0x41 && dat <= 0x46)
  {
    return (dat - 55);
  }
}

« Last Edit: April 30, 2012, 08:09:25 am by ballboii » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial RFID = SoftwareSerial(2, 2);
Do you have the TX pin of the RFID connected to the same Arduino pin as the RX pin? That's a most unusual way of doing things.

Code:
SoftwareSerial LCD = SoftwareSerial(0, 3);
Using the hardware serial pin for SoftwareSerial is a bad idea.

Code:
void loop()
{
  receiveRFIDCardInfo();
}
Why?

Code:
void receiveRFIDCardInfo()
{
  static byte data[4];
  static byte temp[14];
  byte len;
  static int i = 0;
  unsigned long currentId;
  if(RFID.available())
  {
Why are data and temp static? Why is i static?

Looks like you forgot to put RFID in listen mode.

Code:
          currentId = (unsigned long)data[0]*16777216 + (unsigned long)data[1]*65536 + (unsigned long)data[2]*256 + (unsigned long)data[3];
Bit shifting would be so much more efficient.

Code:
  if(dat >= 0x30 && dat <= 0x39)
  {
    return (dat - 0x30);
  }else if(dat >= 0x41 && dat <= 0x46)
  {
    return (dat - 55);
  }
What's with all the magic numbers? Why the mix of hex and decimal?
Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use static cause its the way I was taught. And later it can be used throughout the program, although I understand at the moment there is no where else for it to be used.

And they are was just playing around with it, and was easier for me to remember to connect both to two.

And can you explain

Code:
SoftwareSerial LCD = SoftwareSerial(0, 3);

and how 3 is a hardware pin? There is only an output for the LCD.

and the magic numbers are beyond me, I adapted the sample code which came with the component when I purchased it.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and how 3 is a hardware pin? There is only an output for the LCD.
3 isn't a hardware serial port pin. 0 is. It doesn't matter that there will never be input to the pin from the LCD. The SoftwareSerial instance owns the pin, and will do what it wants with it. That WILL conflict with what the Serial instance does with the pin, screwing up any serial input.

The RFID instance will have similar issues, with one pin being both input and output.

Quote
and the magic numbers are beyond me
Perhaps you should see what they correspond to on an ASCII table, then. Perhaps that will prompt you to change them to meaningful values.
Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand that they are in HEX, and I understand how to change them etc... Just not exactly what they are doing. And I changed the pin locations, but still doesn't work, I think it has to do with the amount of serial "access"? As it will work with one of the components (LCD screen by itself or the RFID). But when you include the other beginning of the serial (LCD.begin(9600)smiley-wink it stops working all together.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  RFID.begin(9600);
  LCD.begin(9600);      // maximum bandwidth issue

Try swapping the order of these.
Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all of your help! Works as it should now.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm glad it works. Now I suggest you look at SoftwareSerial, and figure out why that fixed it. PaulS hinted at it earlier.

Quote
Looks like you forgot to put RFID in listen mode.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(dat >= 0x30 && dat <= 0x39)

Google for an ASCII code chart. This is the same as:

Code:
  if(dat >= '0' && dat <= '9')

That's more readable, don't you think?
Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I still don't really see how using ASCII code is more readable, I understand it may look nicer, but at least I relatively, or with a bit of maths, know the value used.

And I did some reading and understand a bit better now, but hopefully by the end of it know it back to front!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if(dat >= 0x30 && dat <= 0x39)
  {
    return (dat - 0x30);
  }else if(dat >= 0x41 && dat <= 0x46)

OK, so you are comparing an incoming digit to be in the range 0 to 9, or A to F?

And you don't think this is more readable? When it mentions 0-9 and A-F and not some hex equivalent?

Code:
 if(dat >= '0' && dat <= '9')
  {
    return (dat - '0');
  }else if(dat >= 'A' && dat <= 'F')

Oh well, whatever works for you I guess.

Quote
I relatively, or with a bit of maths, know the value used.

With my method you don't need the maths.
Logged

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a good point, I appreciate it and will remember for future projects, but it currently forces me to remember how to do it, as with many of my classes they still have some form of HEX, BIN and OCT conversions.
Logged

Pages: [1]   Go Up
Jump to: