Go Down

Topic: Seeeduino Stalker Datalogger (Read 1 time) previous topic - next topic

davidb-uk

Hi Everyone,

I'm using a seeeduino stalker v1.0 to make a datalogger. What I am trying to do is take 16 analog readings with averaging using a multiplexer, then write these 16 readings to a .csv line with a time and datestamp on the same line and then repeat after a set delay.

The problem I'm having is the data is not writing properly to the .csv file. Either it will write the first few readings and no more, or it will take readings at random points in time and not necessarily write them to new lines. I've attached a bit of the output .csv below to make it clearer.

This is how the output should be:
Time,Date,reading1,reading2....reading16
Time,Date,reading1,reading2....reading16

The program works fine over the serial and also if I just take the 16 readings without the time/datestamp so I think the buffer may be running out of memory?

I hope I've made the problem clear. I've been trying to fix this for days but really have run out of ideas, I would be eternally grateful if anyone has any suggestions on how to fix this problem!

Thanks

Example output:
Code: [Select]
08:46:46 08/08/2011 73 73 08:56:46 08/08/2011 45 45 08:57:10 08/08/2011 358 360


Code:
Code: [Select]
/*
* Datalogger:- Uses CD4051BE Multiplexer to record 16 analog readings with a time stamp to SD card.
*
*
* codeexample for useing a 4051 * analog multiplexer / demultiplexer
* by david c. and tomek
*
* edited by Ross R.
*/

#include <Fat16.h>  // the SD Card library - http://code.google.com/p/fat16lib/
#include <Wire.h> 
#include <Time.h> 
#include <DS1307RTC.h> // a basic DS1307 library - http://www.arduino.cc/playground/Code/Time

const char *fileName = "TEST.csv";  // the name of the log file

int r0 = 0;      //value of select pin at the 4051 (s0)
int r1 = 0;      //value of select pin at the 4051 (s1)
int r2 = 0;      //value of select pin at the 4051 (s2)
int count = 0;   //which y pin we are selecting

// I/O Pins
int s0 = 2;
int s1 = 3;
int s2 = 4;
int ledPin = 13; //LED for error reading

//Holders
int readingA0 = 0; //Analog A0 reading
int readingA1 = 0; //Analog A1 reading

//Averaging Variables
int MreadingA0 = 0; //Analog A0 Average reading
int MreadingA1 = 0; //Analog A1 Average reading
int n1 = 50; // Number of samples taken in sampling average
int t1 = 10; // Total milliseconds between samples
int logInterval = 100; // The interval in seconds between each log


//SD CARD
SdCard card;
Fat16 LogFile;


void setup(){

  pinMode(s0, OUTPUT);    // s0
  pinMode(s1, OUTPUT);    // s1
  pinMode(s2, OUTPUT);    // s2
  pinMode(ledPin, OUTPUT);

  // RTC
  setSyncProvider(RTC.get);   // the function to get the time from the RTC

  // initialize the SD card
  if (!card.init())
    error(1);

  // initialize a FAT16 volume
  if (!Fat16::init(&card))
    error(2);

  // open file for append, create if it doesn't exist 
  if (!LogFile.open(fileName, O_CREAT | O_APPEND | O_WRITE))
    error(3);

  // clear write error
  LogFile.writeError = false;

}

void loop () {

  // Record the time
  time_t timeStamp = now();
  printDateTime(timeStamp);

  for (count=0; count<=7; count++) {

    // select the bit 
    r0 = bitRead(count,0);       
    r1 = bitRead(count,1);         
    r2 = bitRead(count,2);       

    //Write bits to multiplexer
    digitalWrite(s0, r0);
    digitalWrite(s1, r1);
    digitalWrite(s2, r2);

    //Reset Averaging
    MreadingA0 = 0;
    MreadingA1 = 0;

    //Reading Averaging
    for(int i = 1; i <= n1; i++) {

      readingA0 = analogRead(0);
      readingA1 = analogRead(1);

      MreadingA0 = MreadingA0 + readingA0; //Add readings to average
      MreadingA1 = MreadingA1 + readingA1;

      delay(t1);

    }

    readingA0 = MreadingA0 / n1; // Take mean average
    readingA1 = MreadingA1 / n1;

    LogFile.print(readingA0);
    LogFile.print(",");

    if(count == 7){
      LogFile.println(readingA1); //If on final multiplexer reading, start new line
    }
    else{
      LogFile.print(readingA1); // Else write to same line
      LogFile.print(",");     
    }

    // write the data to the card at the end of every line
    if (!LogFile.sync());
    error(4);   
  }

  // Delay next reading by logInterval
  while(now() < (timeStamp +  logInterval )) {
    delay(5);
  } 
}

/*
END OF LOOP
*/


// Routine handle file errors
void error(int err) {
  while(1)
  {
    digitalWrite(ledPin, HIGH);
    delay(err * 200);
    digitalWrite(ledPin, LOW);
    delay(200); 
  }
}


// Conversion of time_now to values and printing on the buffer
void printDateTime(time_t t) {
  printDigits(hour(t),':' );
  printDigits(minute(t),':' );
  printDigits(second(t),',' );
  printDigits(day(t),'//' ); 
  printDigits(month(t),'//' );   
  LogFile.print(year(t)-2000);  // prints year using 2 digits
  LogFile.print(',');
}

// Conversion of time digits to normal display
void printDigits(int digits, char seperator) {
  if(digits < 10)
    LogFile.print('0');
  LogFile.print(digits);
  LogFile.print(seperator); 
}

Go Up