Last active
October 15, 2020 12:16
-
-
Save EnsekiTT/6315684 to your computer and use it in GitHub Desktop.
SDカードのファイル名が被って消されないようにする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// SD File Manager /// | |
| /// auther: EnsekiTT /// | |
| #include <SD.h> | |
| const int CS = 4;//イーサネットシールドなので | |
| char filename[12] = "data000.log"; | |
| byte filenum_1 = 0; | |
| byte filenum_10 = 0; | |
| byte filenum_100 = 0; | |
| File dataFile; | |
| void setup() | |
| { | |
| // シリアルポート初期化なので | |
| Serial.begin(9600); | |
| SD_init(); | |
| } | |
| void loop() | |
| { | |
| int data = analogRead(0); | |
| SD_write_int(data); | |
| // すごく回っても困惑するなので | |
| delay(100); | |
| } | |
| //SDカードの初期化とファイル選択 | |
| void SD_init(){ | |
| pinMode(SS, OUTPUT); | |
| if (!SD.begin(CS)) { | |
| while(1); | |
| } | |
| Serial.println(F("SD ok.")); | |
| while(1){ | |
| if(SD.exists(filename)){ | |
| filenum_1++; | |
| if(filenum_1 > 9){ | |
| filenum_10++; | |
| filenum_1 = 0; | |
| if(filenum_10 > 9){ | |
| filenum_100++; | |
| filenum_10 = 0; | |
| } | |
| } | |
| filename[4] = filenum_100+48; | |
| filename[5] = filenum_10+48; | |
| filename[6] = filenum_1+48; | |
| }else{ | |
| break; | |
| } | |
| } | |
| dataFile = SD.open(filename, FILE_WRITE); | |
| Serial.print(F("ok.")); | |
| Serial.println(filename); | |
| SdFile::dateTimeCallback( &dateTime ); | |
| } | |
| //受け取ったデータを書き込む | |
| void SD_write_int(int data){ | |
| dataFile = SD.open(filename, FILE_WRITE); | |
| if (dataFile) { | |
| dataFile.println(data); | |
| dataFile.close(); | |
| Serial.println(data); | |
| } | |
| else { | |
| Serial.print(F("error opening ")); | |
| Serial.println(filename); | |
| } | |
| } | |
| //とりあえず日付がひつようなので | |
| void dateTime(uint16_t* date, uint16_t* time) | |
| { | |
| uint16_t year = 2013; | |
| uint8_t month = 2, day = 3, hour = 9, minute = 0, second = 0; | |
| *date = FAT_DATE(year, month, day); | |
| *time = FAT_TIME(hour, minute, second); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment