Go Down

Topic: Help: RFID and serLCD (Read 929 times) previous topic - next topic

ballboii

Apr 30, 2012, 03:01 pm Last Edit: Apr 30, 2012, 03:09 pm by ballboii Reason: 1
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: [Select]

#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);
  }
}



PaulS

Code: [Select]
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: [Select]
SoftwareSerial LCD = SoftwareSerial(0, 3);
Using the hardware serial pin for SoftwareSerial is a bad idea.

Code: [Select]
void loop()
{
  receiveRFIDCardInfo();
}

Why?

Code: [Select]
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: [Select]
          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: [Select]
  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?

ballboii

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: [Select]

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.

PaulS

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.

ballboii

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);) it stops working all together.

dxw00d

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


Try swapping the order of these.

ballboii

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

dxw00d

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.

Nick Gammon

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

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

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

That's more readable, don't you think?
http://www.gammon.com.au/electronics

ballboii

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!

Nick Gammon

Code: [Select]
 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: [Select]
 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.
http://www.gammon.com.au/electronics

ballboii

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.

Go Up