Hey guys. I am working on a project, where i wanna read a NFC tag, and then store the value of this tag for 5 minutes, then it has to be cleared.
My problem is though, is it possible to allow the arduino to create, and delete integers? I mean, when it scans a tag, can i then make it create a new int, and then, after 5 minutes delete that said int, to save space in the sram?
I wanna do this as i have to store a lot of integers at a time. like maybe 40-50 at the same time, for 5 minutes.
I know how to read the tags, so it is "just" the saving part i need..
I have been thinking about using a sd data logging shield, but i don't know if it will be compatible, with my adafruit nfc shield.
i have to store a lot of integers at a time. like maybe 40-50 at the same time, for 5 minutes.
This sounds like you should be using an array to store the ints. True that will use a fixed amount of memory but it will not expand once declared and you can store 50 ints in 100 bytes of memory using an array. You cannot delete the entries in the array but you could set them to a value indicating that the entry is not currently valid or create a second array of bytes to hold a list of currently valid array entries.
It would be easier to provide advice if you were to expand further on what you are doing.
UKHeliBob:
It would be easier to provide advice if you were to expand further on what you are doing.
Okay, so it won't be a problem for me, to store 50 ints in arduino, regarding the ram?
is it better to use bytes, instead of ints? i ONLY wanna store numbers. it will be random numbers from 1-1000.
what i wanna do, is to read from a nfc tag. every nfc tag has a random number, from 1-1000 encoded into them.
i then read this value, lets say i read a tag with 875 encoded onto it.
The arduino then has to store 875 for 5 minutes. so that if 875 is scanned again, it can go through the array, and know that exactly this tag, has already been scanned before, within 5 minutes.
Does this make sense?
Thank you alot for your time!
an int is 16bits .. so 32,000 some odd possibilities versus 256 in your byte.
is 256 posibilities enough?
Unfortunately, i am not very good with bytes, bits and such. i am currently taking lessons, to improve on this.
Can you elaborate?
a byte is 8 bits, and an int is 16 bits. right?
So when you say that a byte has 256 possibilities, you mean that i can hold values from 1-256 right?
Where an int can hold from 1-32000 right?
but using bytes saves me 50% ram ?
kasperhangard:
Ah close! if i only wanna store positive numbers, uint will be the most logical choice right?
Uint is the same size, as a normal int right?
yup, unsigned you get 216 choices of positive integers instead of 215 using up that last bit for the sign!!!
Fantastic!
I am looking at an arduino Mega r3, i think getting this instead of the uno r3, will give me more Sram, which will allow me to store more integers, right?
Is there any difference, in using mega vs uno? Obviously there are more pins and such, but exept for this?
kasperhangard:
Fantastic!
I am looking at an arduino Mega r3, i think getting this instead of the uno r3, will give me more Sram, which will allow me to store more integers, right?
Is there any difference, in using mega vs uno? Obviously there are more pins and such, but exept for this?
they don't call him Mega for nothing... he boasts 8KB of SRAM and 256K of FLASH versus his puny cousin Uno weighing in at 2KB of SRAM and only 32KB of FLASH.
that's a lot of integers....
but... your programming will be clever and lean... hanging out here and learning all the tricks to save memory.
Look at PROGMEM to store constants in FLASH instead of using your SRAM and you even have EEPROM to play with!!!
The arduino then has to store 875 for 5 minutes. so that if 875 is scanned again, it can go through the array, and know that exactly this tag, has already been scanned before, within 5 minutes.
In order to know that the number has been stored for 5 minutes you will need to store the time at which it was stored. Unless you have an RTC you will need to store the value of millis() as the start time. millis() returns an unsigned long integer which takes 4 bytes to store which increases the storage requirement considerably,
If I was doing this then I would setup 2 arrays or a custom data type array .
One to store the millis time of the tag read. (If the tag is read again within 5 minutes does this value update or keep initial (first) read time).
The second to store the tag number.
The main program loop would scan the array looking for time expired tags and delete them. Then read new scanned tags and compare to existing reading in the arrays to determine if they have been read before (and how long ago) or storing them with a millis timestamp.
Millis uses an unsigned long (4 bytes) and your tag integer uses 2 bytes so to store 50 entries will need 300 bytes. Easily doable on and UNO if the NFC libraries (and other stuff) don't consume to much SRAM themselves.
Riva:
If I was doing this then I would setup 2 arrays or a custom data type array .
One to store the millis time of the tag read. (If the tag is read again within 5 minutes does this value update or keep initial (first) read time).
The second to store the tag number.
The main program loop would scan the array looking for time expired tags and delete them. Then read new scanned tags and compare to existing reading in the arrays to determine if they have been read before (and how long ago) or storing them with a millis timestamp.
Millis uses an unsigned long (4 bytes) and your tag integer uses 2 bytes so to store 50 entries will need 300 bytes. Easily doable on and UNO if the NFC libraries (and other stuff) don't consume to much SRAM themselves.
I hear you. that should be possible, seeing as the uno has 2,000 bytes to deal with.
So im just gonna do arrays like this?:
int value[50];
int time[50];
how do i then make it put the time and value together, so that it knows which value entry, has which time entry?
If the tag is read again, it should just ingnore that tag, until it doesn't exist on the list anymore.
Won't millis rollover, after some time?
This system is supposed to just run like forever, without being reset.
int time[50];No. You need unsigned long timeStored[50];because as I noted earlier millis() returns an unsigned long
how do i then make it put the time and value together, so that it knows which value entry, has which time entry?
By using the same array index for both.
Won't millis rollover, after some time?
No, because you will use a construct like if (millis() - timeStored[x] >= interval) to test whether the time has passed to delete entry x from the array of tag numbers. By using unsigned longs and subtracting them from millis() the rollover problem is avoide.
whether the time has passed to delete entry x from the array of tag numbers. By using unsigned longs and subtracting them from millis() the rollover problem is avoide.
Deleting entry x from the array implies moving the rest of the array up. Don't do that. Simply set the array entry to 0, presuming that 0 is not a valid tag value. You'd need to check all 50 array elements for the tag, each time, but that is faster than shuffling array contents around.
Paul - quite right of course. When I said delete I had in mind what I suggested in reply #1, although looking back I suggested that entries could not be deleted from the array which is actually not true.
@Bob, Paul: If he plans to take the samples at regular intervals (every 6 seconds), why not just use a single array and let the values "roll off the end". If he needs to know the sample time for a given sample, he can subtract 6 seconds for each sample from the last one taken. I don't know the granularity he needs for the time, but this would likely do for gov't work.
Perhaps something like:
#define UPDATESAMPLE 6000 // Update every 6 seconds for 50 samples in 5 minutes
#define SAMPLESTAKEN 50
unsigned long currentTime, previousTime;
int myTagValues[SAMPLESTAKEN];
void setup() {
previousTime = 0L;
currentTime = millis();
}
void loop() {
int tagValue;
currentTime = millis();
if (currentTime - previousTime > UPDATESAMPLE) { // Time to update?
memmove(&myTagValues[1], myTagValues, sizeof(int) * (SAMPLESTAKEN - 1)); // Bump all old values up 1
myTagValues[0] = ReadNFCTag(); // Get the new tag value
previousTime = millis();
}
}
int ReadNFCTag() {
// Code to read NFC value
}
@Bob, Paul: If he plans to take the samples at regular intervals (every 6 seconds), why not just use a single array and let the values "roll off the end".
The original post said that OP wanted to scan RFID tags, and have the data hang around for up to 5 minutes. I saw nothing about sampling anything at regular intervals.