Go Down

Topic: Datalogging: Sound --- Array Storage Issues (Read 859 times) previous topic - next topic


Thanks in advance to those who reply.

I am very new to Arduino. I have set the clock with a prescalar of 32 to allow an interrupt to occur at ~38.5 kHz. I would like to store the data gathered by the ADCH in the interrupt into an array that can be read later to an SD card. Essentially, there is a short burst of data that must be written/stored and then a long time in between in which the SD card can write to file.


--- The array size will not go over 500. This is the primary issue I would like to fix and have been thus far, unable.
--- The time stamp fails to synchronize with the serial port (the computer).

Thank you for any help.

//communication for the RTC
#define DS1307_I2C_ADDRESS 0x68
#include <SD.h>
#include <RTClib.h>
#include <Wire.h>
//defining the variables
const int chipSelect = 10;
int z=0, dg = 1, s_s = 100, arr_in[100], input;
RTC_DS1307 RTC; // initializing the library to be called by RTC

/* z functions as index for the data array (arr_in [z]), dg is set as flag for datagathering status, s_s is the sample size which is the largest storage in the data array, input is the middleman variable in between the ADCH and the array */
void setup() {
  // initialize serial communication at 115200 bits per second:
  pinMode(10, OUTPUT);
  //intializing SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("no card");
  Serial.println("yes card");
  //initializing time stamp
  RTC.begin ();
  if (!RTC.begin()) {
    Serial.println ("Time not set");
  RTC.adjust(DateTime(__DATE__, __TIME__));

  Serial.println ("Time set");
  delay (100);

  cli (); //disables interrupts
  //set up continuous sampling of analog pin 0
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger or free running mode
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
  sei();//sets interrupts


ISR(ADC_vect) {//when new ADC value ready
if (z == s_s) { //checking for full buffer arry
    dg = 0;
    cli ();

if (z < s_s) {
    dg = 1;
    input = ADCH;
    arr_in[z] = input;


void loop () {

DateTime now = RTC.now ();
//data gathering switched off
if (dg == 0){
for (z=0;z<s_s;z++) {

      File logFile = SD.open ("logFile.csv", FILE_WRITE);
      if (!logFile) {
        Serial.println("false file");
      if (logFile){
        logFile.print(now.month(), DEC);
        logFile.print(now.day(), DEC);
        logFile.print(now.year(), DEC);
        logFile.print(",");//move to next Excel cell for time stamp
        //time stamp
        logFile.print(now.hour(), DEC);
        logFile.print(now.minute(), DEC);
        logFile.println(arr_in [z]);
        logFile.close ();
  dg = 1;
  z = 0;
  sei ();




The Arduino only has 2K of SRAM. 

SD.h, Wire, and Serial use 800-900 bytes.  500 ints uses another 1000 bytes.  That's uses all of your RAM..

You should store ADCH in a uint8_t array.  Then you could have almost 1000 entries.

Also put print strings in flash memory by using the F() macro like this:
Code: [Select]

  Serial.println(F("yes card"));


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131