12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Arduino+SDcardで起動するたびに新しいLogFileを生成する

Posted at

#Arduino + SDcard
今回はワイヤレスSDシールドを用いてArduinoからSDカード内にLogFileを作ります。

#形式
LogFileはArduinoを起動するごとに「LOG000.TXT」、「LOG001.TXT」、「LOG002.TXT」...と数字の部分が増えてゆく形式で作られていきます。

#ソース

LogFile.ino
#include <SD.h>
#include <SPI.h>

File logFile;
char fileName[16];
int fileNum = 0;

void setup() {

  Serial.begin(9600);

  if(!SD.begin(4)){
    // SDカードがないとき
    Serial.println("SD card initialization failed!");
    while(1){
      ;
    }
  }

  // ファイル名決定
  String s;

  while(1){
    s = "LOG";
    if (fileNum < 10) {
      s += "00";
    } else if(fileNum < 100) {
      s += "0";
    }
    s += fileNum;
    s += ".TXT";
    s.toCharArray(fileName, 16);
    if(!SD.exists(fileName)) break;
    fileNum++;
  }

  // ログファイルに書き込む
  logFile = SD.open(fileName, FILE_WRITE);
  logFile.print("This file is No.");
  logFile.println(fileNum);
  logFile.close();

  Serial.print("logFile number is ");
  Serial.println(fileNum);
}

void loop() {
  ;
}
12
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?