Saving anolog data?

how can I save the analog readings (of say a 1 second recording) that the arduino reads from the sensor? I’m trying to save the sensor reading as a data file so i can use it again later as a part of the C code…So i guess I’m basically asking if there’s any arduino source code to save the analog data as a file in the arduino’s memory, to be later applied again in the c code…


My thoughts:

Make a timebased call to a log function
Log the data read in an array at the current index then increment index

Then send all content of the array back over serial if a certain message is recieved. Here a pin state is set according to data recieved across serial.

Then you will need a program running on your computer writing a file from the recieved bytes.

If you aim at a lot of readings/samples you may need to use the EEPROM.

The EEPROM can hold 512 bytes of data, so if you wish to store an int, that gives you enough space for 128 readings unless you get fancy and store 10 bit values (as used by the analog pins) in which case, you could squeeze in 409 readings.

I would not use an array but write directly to the EEPROM at a calculated position something like.

int recordnumber = 0, position, reading;
char *p;

reading = Analogread(myPin);

p = & (char) (void) reading;
position = recordnumber * sizeof(reading);
EEPROMWrite(position++, p++);
EEPROMWrite(position, p);

Maybe you could also save the record number in EEPROM so it persists on power off. and add an offset to your record position eg. :

offset = sizeof(recordnumber)
position = offset + recordnumber * sizeof(reading);

To use 10 bit values, the data would be split over two bytes and you would read both bytes and use some bit wise manipulation to fold the data in to the current values without overwiting bits that do not belong to this reading.

I noted that the stated life of the EEPROM is about 100,000 write cycles so this I doubt this technique is a good idea for continuous logging, you need to look at SD or USB shields.