i want to write a program for reward point system . each time a user scan the card , they get 1 point and i can display the total points in the card into serial monitor. I try the use the code below. Is sit possible to add a mathematical function to blockcontent to adding points to the card
cyberjupiter:
Every time a card is scanned, increment the point by 1 and write it to the card.
First, create a variable name "point" or whatever that identifies point.
Then using the function writeBlock, write the point to the card.
That should be:-
First, create a variable name "point" or whatever that identifies point.
Set that variable to the scan count stored in two bytes of some block somewhere on your card.
Increment that variable by 1
Write it back to the same two bytes of the same block.
Use Serial.print to display that variable on the serial monitor.
I just remembered that function writeBlock and readBlock expects a byte array as argument so you can't create a variable of "char point = 0;" and expect it to work. You need to create a byte array(safest way) or modify the writeBlock function(hard way).
I was bored so I figure out I would give some solution. It might not be the best, but folks here should have better idea.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan a MIFARE Classic card");
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
int block=2;
byte point[16]; //this is where out point will be stored first. we will use this variable to write the point into the card
byte read_point[1]; //this is for checking purpose, to check if we correctly increased the point by reading the card
void loop()
{
if ( !mfrc522.PICC_IsNewCardPresent()){
return;
}
if ( !mfrc522.PICC_ReadCardSerial()){
return;
}
Serial.println("card selected"); //to indicate that we scanned a card. no read/write function is started yet
/* Here is the important part. Before we increase the particular point of a card, we should first know the CURRENT point in the card.
If we don't do that, then how we gonna increment the point by 1? We should have a base value(the CURRENT point in the card).
So here's the procedure to increase the point by 1 everytime the card is scanned.
1. Read the current point in the card and store it in the point[16] array. At this point, I am assuming you are using Mifare 1KB card which has 16 bytes of data in a single block. We read every byte in the block, in this case block '2'. To do this we use the readBlock function like this */
readBlock(block, point);
/* Now, let's first check what we have in the block. This step can be done without the Serial.print(); We use Serial.print so we can view what we got in the serial monitor from reading the block. Assuming you are using a virgin card, block 2 should contain nothing. If you are getting some random values, change to a new card so you get all zeroes. */
Serial.println("Before increasing the point");
for (int i = 0; i < 16; i++)
{
Serial.println(point[i]);
}
/*
2. So now we have a base value of the point which is zero(0) stored in point array from point[0] to point[15].
0 <- point[0]
0 <- point[1]
0 <- point[2]
0 <- point[3]
0 <- point[4]
0 <- point[5]
0 <- point[6]
0 <- point[7]
0 <- point[8]
0 <- point[9]
0 <- point[10]
0 <- point[11]
0 <- point[12]
0 <- point[13]
0 <- point[14]
0 <- point[15]
Each point[] can store a maximum of 255, so total point that can be stored in a block is 255*16 = 4080. I don't know how much you really gonna need since you didn't specify your actual purpose, but I don't think you would be using more than 255 either.
Here's where Arduino folks might have better idea, but assuming you are not going to use more than 255, we can increment the point easily. To increment out current point by 1, we do this:
*/
point[0]++;
Serial.println("After increasing the point");
for (int i = 0; i < 16; i++)
{
Serial.println(point[i]);
}
/* Now our point array look like this. You can serial.print() it using for loop to check.
1 <- point[0]
0 <- point[1]
0 <- point[2]
0 <- point[3]
0 <- point[4]
0 <- point[5]
0 <- point[6]
0 <- point[7]
0 <- point[8]
0 <- point[9]
0 <- point[10]
0 <- point[11]
0 <- point[12]
0 <- point[13]
0 <- point[14]
0 <- point[15]
3. Our point now has increased by 1. Now, we can write the increased point to the card. */
writeBlock(block, point);
/* We can end our program here, but let's be a little confident that we really are writing the increased point. Just read the block again. */
readBlock(block, read_point);
//print the what we just read
Serial.print("read block: ");
Serial.print(read_point[0]);
/* we are done. I ended up doing your homework, but it was fun */
}
I explained most of the things in the code, but hit the thread if there's mistake or confusion. Much of the code is written under assumptions since OP doesn't specify the maximum point.