Help us understand the problem. What is going on with this article?

arduinoからSD cardへの書き込み, 読み込み

More than 1 year has passed since last update.

参考にさせていただいたのは
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");
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away