1

我需要使用 Adafruit Feather M0 Adalogger 并使其以每秒 100 个样本的速度记录 8 个传感器的数据。问题是:有些数据需要更长的时间才能写入txt文件。每个样本之间的间隔是 10 毫秒(左右),但是突然(大约 30 秒,i = 2700,和 37 秒,i = 3150,开始后),需要 30-50 毫秒才能获取数据,然后,回来到 10 毫秒。这对应用程序不利,因为我们正在尝试比较这种数据收集的一些方法,而这个问题会妨碍同步。我需要知道为什么会发生这种情况,以及如何解决它。

#include <BlockDriver.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SysCall.h>

#include <SPI.h>
//#include <SD.h>

SdFat sd;

const int chipSelect = 4;
const int cardDetect = 7;
const int ledPin = 3;
String fileName = "DATA.TXT";
bool alreadyBegan = false;

int sensorPin[] = {A0,A1,A2,A3,A4,A5,0,A7};
uint8_t sensorVal[] = {0,0,0,0,0,0,0,0};
const int sensorNo = 8;

boolean interrupt = false;
boolean displayed = false;
int i = 0;
unsigned long loopCount = 0;
unsigned long beforeTime = 0;
unsigned long afterTime = 0;
unsigned long t1 = 0;
unsigned long t2 = 0;
unsigned long dif = 0;
int sampleSize = 6000;

File dataFile;

void setup(void)
{
  pinMode(ledPin, OUTPUT);
  pinMode(cardDetect, INPUT_PULLUP);
  for (int k = 0; k < sensorNo; k++) {
    pinMode(sensorPin[k], INPUT);
  }
  Serial.begin(9600);
  while(!Serial){
    delay(1000);
  }
  Serial.println("");Serial.println("INITIALIZING.....");
  iniCard();
  Serial.println("");Serial.println("INITIALIZED!!!");
  Serial.println("");Serial.println("========================");
  Serial.println("");Serial.print("File Name: ");Serial.println(fileName);
  Serial.println("");Serial.println("========================");
  delay(2000);
}

void loop(void)
{
    loopCount++;
    t2 = micros();
    dif = t2-t1;
    if (dif >= 9999){
      if (i==0){
        dataFile = sd.open(fileName, FILE_WRITE);
        beforeTime = micros();
        digitalWrite(ledPin, HIGH);
      }
      else if (i == sampleSize){
        afterTime = micros();
        digitalWrite(ledPin, LOW);
      }
      t1 = t2;
      if (i < sampleSize){
        String dataString = "";
        dataString += micros();
        dataString += ",";
        for (int j = 0; j < sensorNo; j++) {
          sensorVal[j] = analogRead(sensorPin[j]);
          dataString += String(sensorVal[j]);
          if (j < sensorNo-1) {
            dataString += ",";
          }
        }
        Serial.println(dataString);
        dataFile.println(dataString);
        i++;
      }
      else{
        digitalWrite(ledPin, LOW);
        if (!displayed){
          dataFile.close();
          displayed = true;
          Serial.println("");Serial.println("========================");
          Serial.print("Frames: ");Serial.println(i);
          Serial.print("Loops: ");Serial.println(loopCount);
          Serial.print("Start Time: ");Serial.println(beforeTime);
          Serial.print("End Time: ");Serial.println(afterTime);
          float period = (afterTime - beforeTime)/i;
          Serial.print("Period: ");Serial.println(period);
          float freq = 1000000 / period;
          Serial.print("Freq: ");Serial.println(freq);
        }
      }
    }
    else{
    }
}

void erro() {
  digitalWrite(ledPin, LOW);
  delay(250);
  digitalWrite(ledPin, HIGH);
  delay(250);
}

void iniCard() {
  digitalWrite(ledPin, HIGH);

  // see if the card is present and can be initialized:
  while (!digitalRead(cardDetect)) {
    erro();
  }

  // Card seems to exist.  begin() returns failure
  // even if it worked if it's not the first call.
  if (!sd.begin(chipSelect) && !alreadyBegan) { // begin uses half-speed...
    iniCard();  // Possible infinite retry loop is as valid as anything
  }
  else {
    alreadyBegan = true;
  }

  // card initialized:
  delay(2000);
  digitalWrite(ledPin, LOW);
}
4

0 回答 0