Buffer

Hi,

Pls very new to all these;

I am reading and writing a value to serial com output; wherein buffer2 is a byte

Serial.write(buffer2[1]);

For example, I read a value of "3" on the serial com output, however I want to display this value on a 16x2 LCD but I am getting weird characters.

I would like to print this value to the LCD and also use the value as an integer.

Any help will be highly appreciated,

Thanls

In C/C++, the start index of an array is 0.

olamuse:
Any help will be highly appreciated,

Post your complete program.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Basically, this code is the modification of the an example "rfid read personal info" in the MFRC522 library. I am reading an integer/float stored on RFID tag. I am really concerned/I want to manipulate the value retrieved value stored in the RFID.

#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


constexpr uint8_t RST_PIN = 9;     // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10;     // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

#define BACKLIGHT_PIN     13
#define SS_PIN 10
#define RST_PIN 9
#define SP_PIN 8
#define LED_GREEN A0
#define LED_RED A1
const int Btn =  A2; 


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

//*****************************************************************************************//
void setup() {
 Serial.begin(9600);                                           // Initialize serial communications with the PC
 SPI.begin();                                                  // Init SPI bus
 mfrc522.PCD_Init();                                              // Init MFRC522 card
 Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read
 lcd.begin(16,2);               // initialize the lcd 
 lcd.clear ();                   // go home
}

//*****************************************************************************************//
void loop() {


 // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
 MFRC522::MIFARE_Key key;
 for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

 //some variables we need
 byte block;
 byte len;
 MFRC522::StatusCode status;

 //-------------------------------------------

 // Look for new cards
 if ( ! mfrc522.PICC_IsNewCardPresent()) {
   return;
 }

 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
   return;
 }

 Serial.println(F("**Card Detected:**"));

 //-------------------------------------------

 mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card

 //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex

 //-------------------------------------------

 Serial.print(F("Name: "));

 byte buffer1[18];

 block = 4;
 len = 18;

 //------------------------------------------- GET FIRST NAME
 lcd.clear();
 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Authentication failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }

 status = mfrc522.MIFARE_Read(block, buffer1, &len);
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Reading failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }
 lcd.setCursor(0, 0);
 //PRINT FIRST NAME
 for (uint8_t i = 0; i < 16; i++)
 {
   if (buffer1[i] != 32)
   {
     Serial.write(buffer1[i]);
     lcd.write((buffer1[i]));
   }
 }
 Serial.print(" ");

 //---------------------------------------- GET LAST NAME

 byte buffer2[18];
 block = 1;

 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
 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;
 }
 
  lcd.setCursor(0, 1);
 //PRINT VALUE
 for (uint8_t i = 0; i < 16; i++) { 
   Serial.write(buffer2[i]);
   lcd.write((buffer2[i]));          
 }


 //----------------------------------------

 Serial.println(F("\n**End Reading**\n"));
 delay(1000); //change value if you want to read cards faster

     
 mfrc522.PICC_HaltA();
 mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//

Why don't people read this before posting a programming question ?

olamuse:
Basically, this code is the modification of ...

Where in that program is the part you want help with?

...R

//PRINT VALUE
 for (uint8_t i = 0; i < 16; i++) { 
   Serial.write(buffer2[i]);
   lcd.write((buffer2[i]));          
 }

I want to be able to manipulate the value retrieved from RFID

I want to be able to manipulate the value retrieved from RFID

What do you want to do with it ?

I want the buffer value (I pre-stored the buffer value in the memory location of the RFID) as an integer so that I can add it (the retrieved value) to itself over a period time and then display total value.

olamuse:
I want the buffer value (I pre-stored the buffer value in the memory location of the RFID) as an integer so that I can add it (the retrieved value) to itself over a period time and then display total value.

OK. You are writing 16 values to the LCD and the screen. What do you see ?

Thanks, you probing has helped me realize the problem.

I was only storing the last value that was retrieved from the buffer. I kept writing over the values as they were being retrieved and displayed on the LCD.

So instead of having for example character 326 to convert to an integer to use, I have character 6 converted to an integer for use.

Big Brain Fart

instead of having for example character 326

I still don't understand what you are trying to do but whatever it is 326 is not a character. It could be a number of characters in an array, each in a different position (array induces 0, 1 and 2). If you inserted a zero into the next position then you would have a C style string and could easily convert it to an integer using the atoi() function.