1

when reading RFID(MIFARE) information using Arduino mega and display in serial monitor it works fine. But, when printing on 2004LCD only one information(Value) displays correctly. The other information Item Name display a decimal number. Could someone help on this?

 void Price(){
      block = 4;
      status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); 
      if (status != MFRC522::STATUS_OK) {
        Serial.print(F("Authentication failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      status = mfrc522.MIFARE_Read(block, buffer1, &len);  // ***Note down buffer1 within the bracket
      if (status != MFRC522::STATUS_OK) {
        Serial.print(F("Reading failedl: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
             //Display price information...
      for (uint8_t i = 0; i < 16; i++)
      {
        if (buffer1[i] != 32) // if buffer1[i] not equal to 32
        {
          Serial.write(buffer1[i]);  //Price
         //lcd.write(buffer1[i]); 
          Value = buffer1[i];
          }
       } 
      Serial.print("/=");
      Serial.println(" ");
      sscanf(buffer1,"%d",&Value);
      lcd.setCursor(0,1);
      lcd.println(Value);
     
      
     }
    //==========================================================
    void Item(){ //----- Get & Display Item information
        block = 1;
     status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid));
      if (status != MFRC522::STATUS_OK) {
        Serial.print(F("Authentication failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      status = mfrc522.MIFARE_Read(block, buffer2, &len);
      if (status != MFRC522::STATUS_OK) {
        Serial.print(F("Reading failed: "));
        Serial.println(mfrc522.GetStatusCodeName(status));
        return;
      }
      for (uint8_t i = 0; i < 16; i++) { //
         Serial.write(buffer2[i]);       // Display Item
         lcd.write(buffer2[i]); 
         Item_Name=buffer2[i];
        } 
        sscanf(buffer2,"%s",&Item_Name);
       //sprintf(buffer2,"%d",&Item_Name);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print(Item_Name);// displays numbers instead of "Fresh Milk 1Lt 
     }

lcd Serial op

3
  • 1
    What is the data type of Item_Name? – Edgar Bonet Mar 18 at 8:40
  • data type is int – Ak Rikas Mar 18 at 9:03
  • sscanf(buffer2,"%s",&Item_Name); works however return a number instead an string. – Ak Rikas Mar 18 at 9:08
1

Consider the following:

char buffer2[16];  // array of 16 chars
for (uint8_t i = 0; i < 16; i++) {
    do_something_with(buffer2[i]);  // handle one char at a time
}

Here we have an array of 16 cells, each having the char type (which I assume is what you have). The loop goes through the array, handling one cell a time. Within the loop, buffer2[i] represents the char being currently handled.

Keep in mind that a char is just an 8-bit integer. It is somehow implied that it is meant to represent a character, where 32 = ' ' (space), 33 = '!', 48 = '0', 65 = 'A' and so on (see ASCII). The Arduino print() methods do assume a char (unlike an int) is intended to be displayed as a character.

Now your actual loop:

int Item_Name;
for (uint8_t i = 0; i < 16; i++) {
    Serial.write(buffer2[i]);  // write the char on the serial port
    lcd.write(buffer2[i]);     // write the char on the LCD
    Item_Name=buffer2[i];      // store the char in a variable
}

This will print the contents of the buffer to both the serial port and the LCD. You would have seen the expected message on the LCD if you did not have lcd.clear() right after the loop.

sscanf(buffer2, "%s", &Item_Name);

This means “extract from buffer2 a sequence of contiguous non-white-space characters, and store it in memory at the address of the variable ItemName”. If the buffer contains “Fresh Milk 1Lt”, this will write “Fresh” to that memory location. That is 6 bytes (5 letters and a terminating NUL). Unfortunately, the compiler has only allocated 2 or 4 bytes of RAM for storing ItemName (depending on the Arduino model), so this will write past the allocated storage, which is a type of bug known as buffer overflow. Now your Arduino's memory is corrupted and anything can happen.

sprintf(buffer2, "%d", &Item_Name);

This tries to find within byffer2 a textual representation of a number, and store it into Item_Name. This is safe, but only makes sense if the buffer contains a number, written as text, which is not the case here.

lcd.print(Item_Name);

This is asking the LCD to print an integer (namely Item_Name). It will convert the integer to decimal text and print the corresponding digits on the LCD.

Let's assume for example that Item_Name contains the numeric value 32 (which is the ASCII representation of a space). lcd.print() will convert that into a sequence of two characters: '3' and '2' (the decimal representation of 32). It will then send those characters to the LCD (ASCII values 51 and 50), and you will see the text “32”.

If you want to see the text contained within the buffer, the you can either send the characters one by one (like you already did right before lcd.clear()), or provide to lcd.print() a char* pointer with the whole message:

lcd.print(buffer2);

This assumes that the buffer is terminated with a NUL character. If it is not, then you can either use the char-at-a-time method, or change your code in order to put a NUL terminator in the buffer.

1
  • Thank you so much! it's working when i change the data type of buffer2 from byte array to character array – Ak Rikas Mar 21 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.