I write the data recorded by an ADXL335 and a linear potentiometer to a SD-Card.
I need a sampling rate for both sensors around 1 khz.
Because of the low speed of the data writing command (it needs 25ms) I just write the date of both sensors every 500 ms to the SD-Card. This is fine for the ADXL335 because the arduino stores the unsaved 499 ms data before writing to the SD-Card somewere.
But what shall i do for the linear potentiometer? How and where can I store the data (data between 0 and 499 ms) between writing to the SD-card? The code below just saves the value of 500 ms and the 499 values before the data storage will not be saved.
Can the data be saved to an array or an other variable and how?
myFile = SD.open("logtest.csv", FILE_WRITE);
while (millis()-start<500L)
{
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
zeit = millis();
/* write action to sd card, */
myFile.print(zeit);
myFile.print(" , ");
myFile.print(event.acceleration.x);
myFile.print(" , ");
myFile.print(event.acceleration.y);
myFile.print(" , ");
myFile.print(event.acceleration.z);
myFile.print(" , ");
myFile.print(potiValue);
myFile.println(" ");
}
myFile.close();
So use an array of 500 integers to store the data, then write all 1000 bytes to the SD card in one step. That should work relatively quickly as opening&closing the file is usually the time-consuming step.
some of the upper part of my code
unsigned long zeit;
File myFile;
int poti[500];
int Arraynumber;
part of the main program
myFile = SD.open("logtest.csv", FILE_WRITE);
while (millis()-start<500L)
//Arraynumber = (millis()-start<500L);
{
Arraynumber = (millis()-start<500L);
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
zeit = millis();
/* write action to sd card, */
myFile.print(zeit);
myFile.print(" , ");
myFile.print(event.acceleration.x);
myFile.print(" , ");
myFile.print(event.acceleration.y);
myFile.print(" , ");
myFile.print(event.acceleration.z);
myFile.print(" , ");
poti[Arraynumber] = analogRead(A0);
myFile.print(poti[Arraynumber]);
myFile.println(" ");
}
myFile.close();
I changed the code in this way an it writes data to my SD Card with a refresh rate of about 1ms which is great. Thank you for your advice.
When does an array "overflow" the arduino memory? I store 500 integer (each 2bits on my due= 1kb) values in my array.
There is no simple way of detecting an overflow. The board will generally reset or do strange things when it runs out of memory.
There are utilities available that will let you read the free memory and thereby understand what your program is doing. You are right to be concerned about an array that size using up too much memory. On an UNO, this is half the available memory. On a Due, it's less than 2% of the available memory.
As you create these large arrays, you should be able to watch your RAM go downward during a compile. Remember also on the DUE, everything, even type INT takes 32 bits or four bytes. So you could make maybe 40 arrays of 500, leaving some for other variables. (just round number based on 96K).
Using SdFat will allow you to log analog data VERY FAST! you may have to select pin 4 instead of SS depending on the SD shield you use.
This is an awesome library. I logged 5kHz on 2 channels, and the measurements can go even faster!
I am trying to use SdFat (connected to pins 10-13) with the nRF8001 BLE (connected to pins 50-53 using Adafruit library) with my Arduino Mega, but having trouble. Seems like being located on 2 separate set of pins would alleviate the issue, but I don't know what is wrong.
Any suggestions? Seems versions of both library with SPI transactions exist, but I am not sure how to use it. Any pointers? I am concerned for the SDFat library called within a BLE loop, will result in dropped BLE connection.
while BLE.available(): //from the BLE library
case1:
logData() // from the SdFat library
case2:
dumpdata() //access BLE and SdFat simultaneously or looping til done :o
The Bluetooth library won't work with Sdfat. See your other post.
The bluetooth library does not initialize the SPI bus before access. It uses LSBFIRST, SdFat uses MSBFIRST. It uses slow SPI SPI_CLOCK_DIV8. The bluetooth library uses interrupts and has a long latency which will block the fast logger.