Hi folks. So I have been getting my head around this and could not find a solution.
So basically I have some barcodes coming from the Serial Barcode Scanner. When a barcode is scanned, my code searches through the if else to compare the scanned barcode with the existing one in the database. And this process is repeated until the user wishes to 'CHECKOUT'. I am doing fine until this stage. When the user wants to 'CHECKOUT', they just put a card on the reader and all the barcodes that have been scanned will be written on different block of the card. I am using MFRC522 RFID by the way.
The problem is that how I am suppose to write a code that only take the barcodes that have been scanned to be written on the card?
This is what I currently have
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 7
#define RST_PIN 6
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
struct ItemList {
char barcode[10];
char name[25];
unsigned int price;
};
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
ItemList item1 =
{
"452795615",
"SOME ITEM_1 $1.00",
100
};
ItemList item2 =
{
"562330250",
"SOME ITEM_2 $1.80",
180
};
unsigned int block = 2;
byte codeBuffer[10];
const byte numChars = 12;
char receivedChars[numChars];
boolean newData = false;
unsigned int total = 0;
void loop()
{
recvWithEndMarker();
processData();
writeDataToCard();
}
void writeDataToCard()
{
if ( !mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( !mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println("card selected");
strcat((char*)codeBuffer, item1.barcode);
writeBlock(2, codeBuffer); //this is not how it should be. this will only write the barcode of item1.
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void processData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
if (strcmp(receivedChars, item1.barcode) == 0)
{
total = total + item1.price;
Serial.print(item1.name);
}
else if (strcmp(receivedChars, item2.barcode) == 0)
{
total = total + item2.price;
Serial.print(item2.name);
}
newData = false;
}
}
int writeBlock(int blockNumber, byte arrayAddress[])
{
int largestModulo4Number=blockNumber/4*4;
int trailerBlock=largestModulo4Number+3;
if (blockNumber > 2 && (blockNumber+1)%4 == 0){
Serial.print(blockNumber);
Serial.println(" is a trailer block:");
return 2;
}
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
return 3;//
}
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
if (status != MFRC522::STATUS_OK) {
return 4;
}
}
Of course one method is to put the writeDataToCard() routine inside each if-else but that is not efficient. What I want to do is that the user only scan the card after he finishes shopping.
P/S: Pardon me if the title is not suitable. I couldn't find a better description for the subject.
Also thanks to @Robin2 for his Serial Input Basics - updated