12
18

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からSD cardへの書き込み, 読み込み

Last updated at Posted at 2017-09-18

参考にさせていただいたのは
ArduinoでSDメモリカードを読み書きする
です.
というかこれしか試してません.

SDカードはシールド
HiLetgo Micro SD/ TF カードモジュール シールド モジュール 6 ピン SPI [並行輸入品]
を使いました.
この方の方法だと, ちゃんと回路組めばシールドなしでもいけるみたいですね. 今度やってみよう.

セッティング

接続は
PIN11 - DI (MOSI)
PIN12 - DO (MISO)
PIN13 - CLK (SCK)
PIN 4 - CS
GND - GND
5V - VCC

codes

以下のcodeは, 上のURLの方が書いてくださったコメントを外して動作に必要そうな部分だけ抜き出したもの.

A0の値をSDに書き込み(電源が入るたびに書き込みを追加)

sd_write_simple.ino
#include <SD.h>
const int chipSelect = 4;

void setup(){
  pinMode(SS, OUTPUT);
  SD.begin(chipSelect);
}

void loop(){
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    int value = analogRead(0);
    dataFile.println(value);
    dataFile.close();
  }
  delay(1000);
}

A0の値をSDに書き込み(電源が入るたびに過去ファイルを消して新しく作成)

sd_write_simple_remove.ino
#include <SD.h>
const int chipSelect = 4;

void setup(){
  pinMode(SS, OUTPUT);
  SD.begin(chipSelect);
  SD.remove("datalog.txt");
}

void loop(){
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    int value = analogRead(0);
    dataFile.println(value);
    dataFile.close();
  }
  delay(1000);
}

SDの値を読み込み

sd_read_simple.ino
#include <SD.h>
const int chipSelect = 4;

void setup(){
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  SD.begin(chipSelect);
}

void loop(){
  File dataFile = SD.open("datalog.txt");
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  delay(2000);
}

ファイル位置

ファイル位置はseekを使うと決められるらしいですが, いまいち使い方をつかめず.
removeは
SD (File Operations)
を参考にしました.

スイッチと組み合わせる

control_read-write_sd.ino
#include <SD.h>
const int chipSelect = 4;
const int pinWriteSW = 2;
const int pinReadSW  = 3;
int stateWriteSW;
int countWriteSW;
int stateReadSW;
int countReadSW;
boolean isWritingMode;
boolean isReadingMode;
File dataFile;

void setup(){
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  SD.begin(chipSelect);

  //pinMode(pinWriteSW,  INPUT_PULLUP);
  pinMode(pinWriteSW,  INPUT);
  pinMode(pinReadSW,  INPUT);
  stateWriteSW = 0;
  countWriteSW = 0;
  isWritingMode = false;
  stateReadSW = 0;
  countReadSW = 0;
  isReadingMode = false;
}

void loop(){
  readPIN();
  
  controlWritingMode();
  if (isWritingMode){
    writeValue();
    //delay(1000);
  }

  controlReadingMode();
  if (isReadingMode){
    readValue();
  }  
}


void controlWritingMode(){
  if (stateWriteSW == 1){
    countWriteSW++;
    if (countWriteSW == 10){

      if (!isWritingMode){
        if (isReadingMode){
          isReadingMode = false;
          closeSD();
        }
        isWritingMode = true;
        openSDToWrite();
        if (!dataFile) {
          openSDToWrite();
        }
      } else if (isWritingMode){
        isWritingMode = false;
        closeSD();
      }
      
      delay(1000);
    }
  } else {
    countWriteSW = 0;
  }
}

void controlReadingMode(){
  if (stateReadSW == 1){
    countReadSW++;
    if (countReadSW == 10){

      if (!isReadingMode){
        if (isWritingMode){
          isWritingMode = false;
          closeSD();
        }
        isReadingMode = true;
        openSDToRead();
        if (!dataFile) {
          openSDToRead();
        }
      } else if (isReadingMode){
        isReadingMode = false;
        closeSD();
      }
      
      delay(1000);
    }
  } else {
    countReadSW = 0;
  }
}

void readPIN(){
  stateWriteSW = digitalRead(pinWriteSW);
  stateReadSW  = digitalRead(pinReadSW);
}

void openSDToWrite(){
  SD.remove("data.txt");
  dataFile = SD.open("data.txt", FILE_WRITE);
  Serial.println("SD was opened to write");
}

void openSDToRead(){
  dataFile = SD.open("data.txt");
  Serial.println("SD was opened to read");
}

void writeValue(){
  int value = analogRead(0);
  dataFile.println(value);
  Serial.print("write:");
  Serial.println(value);
}

void readValue(){
  while (dataFile.available()) {
    Serial.write(dataFile.read());
  }
}

void closeSD(){
  if (dataFile) {
    dataFile.close();
    Serial.println("SD was closed");
  }
}
12
18
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
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?