A novice in microcontroller needs help

Hi … I am a beginner in learning the Arduino microcontroller.

But I need help right now.

I use RFID ID-12 and combine it with using the Arduino microcontroller ATMega8535’s framework.

But I have a little problem.

I want the reading of the RFID ID-12 can be displayed by the LCD 16x2. I’ve followed the tutorial in “http://arduino.cc/playground/Code/ID12” and try to combine it with “LiquidCrystal SerialPrint”.

But still the results are out. I do not understand why? If anyone can help?

About coding that I made are:

# Include <LiquidCrystal.h>
# Include <SoftwareSerial.h>

# Define rxPin 24
# Define txPin 25

SoftwareSerial serial = SoftwareSerial (rxPin, txPin);

/ / Initialize the library with the numbers of the interface pins
LiquidCrystal lcd (16, 17.18, 20, 21, 22, 23);

void setup () {
  lcd.begin (16, 2);
  / / Initialize the serial communications:
  Serial.begin (9600);
  
  pinMode (rxPin, INPUT);
  pinMode (txPin, OUTPUT);
  / / Set the data rate for the port SoftwareSerial
  serial.begin (9600);
}

void loop ()
{
  byte i = 0;
  byte val = 0;
  byte code [6];
  byte checksum = 0;
  bytesread byte = 0;
  tempbyte byte = 0;
    
  char c = serial.read ();
  serial.print (c);
  delay (1);

  if (Serial.available ()> 0)
  {
    
    while (Serial.available ()> 0)
      {
        if ((val = Serial.read ()) == 2)
        {/ / Check for header
          bytesread = 0;
          while (bytesread <12)
          {/ / Read 10 digit code + 2 digit checksum
            if (Serial.available ()> 0)
            {
              val = Serial.read ();
              if ((val == 0x0D) | | (val == 0x0A) | | (val == 0x03) | | (val == 0x02))
              {/ / If header or stop bytes before the 10 digit reading
                break; / / stop reading
              }

              / / Do Ascii / Hex conversion:
              if ((val> = '0 ') && (val <= '9'))
              {
                val = val - '0 ';
              }
              else if ((val> = 'A') && (val <= 'F'))
              {
                val val = 10 + - 'A';
              }

              / / Every two hex-digits, add byte to code:
              if (bytesread & 1 == 1)
              {
                / / Make some space for this hex-digit by
                / / Shifting the previous hex-digits with 4 bits to the left:
                code [bytesread >> 1] = (val | (tempbyte << 4));

                if (bytesread >> 1! = 5)
                {/ / If we're at the checksum byte,
                  checksum ^ = code [bytesread >> 1]; / / Calculate the checksum ... (XOR)
                };
              }
              else
              {
                tempbyte = val; / / Store the first hex digit first ...
              };

              bytesread + + / / ready to read next digit
          }
      }

      / / Output to Serial:

      if (bytesread == 12)
      {/ / If 12 digit read is complete
        Serial.print ("5-byte code:");
        for (i = 0; i <5; i + +)
        {
          if (code [i] <16) Serial.print ("0");
          Serial.print (code [i], HEX);
          Serial.print ("");
        }
        Serial.println ();

        Serial.print ("Checksum:");
        Serial.print (code [5], HEX);
        Serial.println (code [5] == checksum? "- Passed.": "- Error.");
        Serial.println ();
      }
            
      bytesread = 0;
      }
        
      }
       / / Display each character to the LCD
       lcd.write (serial.read ());
  }
}

Moderator edit: Italics removed by inserting CODE TAGS.

Please help me…

Please help me....

Please modify your post, select the code part and press the # button. This will put [ code] tags around it, That will make the look better :)

Thanks,

robtillaart:

Please help me....

Please modify your post, select the code part and press the # button. This will put [ code] tags around it, That will make the look better :)

Thanks,

I'm sorry... I am 14 years old and I never talk like this before. I really want to learn Arduino. But I use ATMega8535. Can you help me to solve my problem above?

I want to display the results of the RFID-ID 12 on a 16x2 LCD. But I do not know how.

Break you project down into pieces. Make sure the RFID unit is sending the data that you think it is.

Can you make a seperate function that just reads the RFID?

Make a seperate function to write to the display so that you can send it data from the serial monitor and display it. Then when both modules are working, then put them together in one program.

Right now you are trying to debug 2 parts at the same time. Does the RFID work properly? Don't know. Does the Display section work properly? Don't know. Break the project down into pieces and make sure each piece works by itself, then combine them. That way you can test each piece on ots own when you make a change.

kf2qd: Break you project down into pieces. Make sure the RFID unit is sending the data that you think it is.

Can you make a seperate function that just reads the RFID?

Make a seperate function to write to the display so that you can send it data from the serial monitor and display it. Then when both modules are working, then put them together in one program.

Right now you are trying to debug 2 parts at the same time. Does the RFID work properly? Don't know. Does the Display section work properly? Don't know. Break the project down into pieces and make sure each piece works by itself, then combine them. That way you can test each piece on ots own when you make a change.

Yes, I've tried it step by step... N then I tried to combine it into one program n showed it on LCD 16x2 using usart. But the result is a strange character n I don't know what it is.

Any body can help me?

I want to change the ascii to hex n show the result on lcd.

please,,,please...

lolyc:
please,please…

I don’t want to seem unhelpful, but can you please post your code that actually compiles? This won’t:

/ / Initialize the library with the numbers of the interface pins

And all the other places where you have a space between / and /.

And this:

# Include <LiquidCrystal.h>

It’s #include not #Include.

And this:

#Define rxPin 24

It’s #define not #Define.

(My apologies if the moderator did all this).


I like to encourage youngsters to get involved, but you aren’t helping by posting code that doesn’t compile.

This is probably the “funny character”:

// Display each character to the LCD
lcd.write (serial.read ());

Some RFID devices send “control” characters like STX and ETX. They may well print strangely.

Mind you, it’s Serial.read not serial.read. I’m having trouble believing you copied and pasted the code you are using. You can do that you know. You don’t have to retype it all.

(My apologies if the moderator did all this).

No, all I did is put it inside code tags. Once you do that, the italics and the smileys go away.

@Nick - The OP has called their SoftwareSerial instance serial.

SoftwareSerial serial = SoftwareSerial (rxPin, txPin);

Oh, sorry. Got confused by that one.

@lolyc: There is a built-in hardware serial class, where on the Arduino an instance of that is called Serial. Now you can use SoftwareSerial and call an instance of that serial (rather than Serial) but it is kinda confusing.

As a general rule try to avoid calling variables by things that are merely different in capitalisation (like Dog, DOG, dOG, DoG and so on.). It works, but is likely to get very confusing.

Once all the extra spaces have been removed, it almost compiles. I can't work out what this line is meant to do:

                val val = 10 + - 'A';

Thx for the correction… :slight_smile:
Now i’ve tried my new codes. It’s no different from the old codes.

But, I want to show the hex result on the 16x2 LCD. But, I always get strange data that shown on the LCD.

This is my new code:

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

#define rxPin 24
#define txPin 25

SoftwareSerial serial =  SoftwareSerial(rxPin, txPin);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(16, 17,18, 20, 21, 22, 23);

void setup() 
{
  lcd.begin(16, 2);
  Serial.begin(9600);                                 // connect to the serial port

  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  serial.begin(9600);
}

void loop () 
{
  char c = serial.read();
  serial.print(c);
  
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(Serial.available() > 0) {
    if((val = serial.read()) == 2) {                  // check for header 
      bytesread = 0; 
      while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
        if( Serial.available() > 0) { 
          val = serial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }

          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));

            if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
              checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        } 
      } 

      // Output to Serial:

      if (bytesread == 12) {                          // if 12 digit read is complete
        serial.print("5-byte code: ");
        for (i=0; i<5; i++) {
          if (code[i] < 16) serial.print("0");
          serial.print(code[i], HEX);
          serial.print(" ");
        }
        serial.println();

        serial.print("Checksum: ");
        serial.print(code[5], HEX);
        serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        serial.println();
      }

      bytesread = 0;
    }
  }
  lcd.write(c);
}

Moderator edit CODE TAGS again.

is there anybody can show the fault of the reading?

As far as I can see, The only lines that control what you see on your lcd are:

...
  char c = serial.read();
  serial.print(c);
...
  lcd.write(c);
...

So, you read a byte from your SoftwareSerial instance, without checking to see if there is actually anything to read, then print it back to the SoftwareSerial port. A bit later, you then write (not print) that value to the lcd.

In between the two, you do a whole load of other stuff that you print back to SoftwareSerial, that might be better sent to Hardware Serial, assuming that is the one connected to your PC.

You also have a slight issue here:

  if(Serial.available() > 0) {
    if((val = serial.read()) == 2) {                  // check for header

You check if there is anything to read in the Hardware Serial queue, then try and read it from SoftwareSerial. This is where Nick was going with his confusing names comment. You do this a couple of times.