DataLogger for 4 analog Inputs: code optimization


g'day all,

i looking utilize teensy 2.0 act controller control system required take analog inputs , drive mirror depending on values of these inputs.

in order first stage working , results trying implement code samples 4 analog inputs fast possible (ideally 20khz satisfy minimum desired nyquist rate 10 khz signal). want write sd card have attached through sd adapter in order able analyse results. initial attempt runs @ embarrassingly slow rate of approx 30 hz.

after writing code test speed of various functions obvious file open procedure took ages trying implement code without in every loop provided in datalogger sample comes arduino library.

my code below:

code: [select]
#include <sd.h>
//  included match sd shield or module: teensy 2.0 utilised therefore pin 0
const int chipselect = 0;
const int ledpin = 11;
//  boolean allow 1 iteration
boolean done = false;
char filename[] = "qc_out.txt";
//  time variables
long timestart;
long timetotal;

void setup()
{
  //  setup led
  pinmode(ledpin, output);
  digitalwrite(ledpin, high);
 
  //  code taken provided datalogger example
  // open serial communications , wait port open:
  serial.begin(9600);
  serial.print("initializing sd card...");
  // make sure default chip select pin set to
  // output, if don't use it:
  pinmode(10, output);
  if (!sd.begin(chipselect))
  {
    serial.println("card failed, or not present");
    return;
  }
  serial.println("card initialized.");
 
  //  checking pre existing file , deleting if found
  serial.println("checking if file exists");
  if (sd.exists(filename)) {
    sd.remove(filename);
    serial.println("file found , deleted");
  }
  else {
    serial.println("no file found");
  }
 
  //  write informaiton top lines of file reference
  file datafile = sd.open(filename, file_write);
  if (datafile) {
    datafile.println("results testing");
    datafile.println("author: guy");
    datafile.println("");
    datafile.close();
    serial.println("header written file");
  }
  else {
    serial.println("error in opening file");
  } 
  timestart = micros();
}

void loop() {
  //  open file write info to
  file datafile = sd.open(filename, o_creat|o_append|o_write);
  if(timetotal<10000000) {
    string data = "";
   
    //  sequence reads values on analog input pins and
    //  adds them straight string
    (int analogpin = 21; analogpin >17; analogpin--) {
      int sensor = analogread(analogpin);
      data += string(sensor);
      if (analogpin>18) {
        data +=",";
      } 
    }
    //  write results file
    if (datafile) {
      datafile.println(data);
      datafile.flush();
    }
    else {
      serial.println("error in opening file");
    }
  //  timing loop 
  timetotal=micros()-timestart;
  serial.println(timetotal);
  } 
  else {
    if(!done) {
      datafile.close();
      serial.println("data acquisition complete");
      done = true;
      digitalwrite(ledpin, low);
    }
  }
}


the problem occuring stops being able access file after 1 second resulting in serial out showing the  ("error in opening file") message. have inkling might memory issue unsure...

quote
my initial attempt runs @ embarrassingly slow rate of approx 30 hz.

i'm not surprised. doing many things wrong.

first, way write sd card fast possible open file once, write data needed, , close file once.

second, file class derives print, knows how convert ints , floats strings, writes file. converting values strings first, , appending them string, , unpacking string write file way slow. knock crap off.

quote
after writing code test speed of various functions obvious file open procedure took ages trying implement code without in every loop provided in datalogger sample comes arduino library.

but, posted other code. well, fine. forget tried help.


Arduino Forum > Using Arduino > Programming Questions > DataLogger for 4 analog Inputs: code optimization


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support