BASIC4MCU | 질문게시판 | 센싱 데이터 sd카드 저장 관련 코드 질문 드립니다.
페이지 정보
작성자 우끼우기 작성일2021-01-13 17:22 조회21회 댓글1건첨부파일
본문
아래 코드에서 볼수 있듯이 센싱된 데이터를 전부 sample.txt에 저장한후 조건에 맞는 데이터만을 sample3.txt에 써 넣는 코드 입니다. 조건에 부합하는 데이터를 sample.txt에 넣을수 있는것 까지 확인 하였으나 sample.txt 마지막에 존재하는 %를 만난다 하더라도 while문을 탈출 하지 못합니다. 또한 while1(1)을 while(myFile.available()) 로 바꾼다 하더라도 탈출 하지 못하고 이상한 곳에서 끊기게 됩니다. 혹시 탈출할수 있는 좋은 방법 있을까요?? 아니면 아두이노 코드 또한 비주얼 스튜디오 처럼 시간에 따라서 어디서 오류가 생기고 어디서 정확하게 멈추는지를 확인할수 있나요??
File myFile;
File myFile2;
int pinCS = 53; // Pin 10 on Arduino Uno
int j=1;
int i;
TSYS01 sensor; // temperature
MS5837 sensor2; // pressure
void loop() {
initializeSD();
createFile("Sample.txt");
createFile("Sample3.txt");
j=1;
while(1){
measure_td();
if(j==70){
myFile = SD.open("Sample.txt", FILE_WRITE);
if (myFile) {
myFile.print("%");
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening Sample.txt");
}
break;}
}
Serial.println("data 70 ok");
char m1[10];
char m2[10];
char m3[10];
int data1;
double data2;
double data3;
char k;
int i =0;
int p = 990; //압력 테스트 위한 변수
int count;
Serial.println("malloc ok");
myFile = SD.open("Sample.txt");
if (myFile) {
while(myFile.available()){
i=0;
char a = (char)myFile.read() ;
if(a=='/'){
while(1){
k = (char)myFile.read() ;
if(k=='%'){break;}
if(k != '*') {
Serial.println("q");
m1[i]=k;
i++;
}
else {
Serial.println(m1);
data1 = atoi(m1);
Serial.println(data1);
Serial.println("data1 ok");
if(data3 >= p){
sd_logfinal(data1,data2,data3);
p = p+10;
Serial.println("sample3 ok");
break;
} //p의 값만 변환 시키면 됨
else break;
}
}
}
if(a=='*'){
while(1){
k= (char)myFile.read() ;
if(k != ',') {
m2[i]=k;
i++;
}
else {
Serial.println(m2);
data2=atof(m2);
Serial.println(data2);
Serial.println("data2 ok");
break;
}
}
}
if(a==','){
while(1){
k= (char)myFile.read() ;
if(k != '/') {
m3[i]=k;
i++;
}
else {
Serial.println(m3);
data3=atof(m3);
Serial.println(data3);
Serial.println("data3 ok");
break;}
}
}
}
myFile.close();
}
Serial.println("Finish");
exit(0);
}
int initializeSD() {
Serial.println("Initializing SD card...");
pinMode(pinCS, OUTPUT);
if (SD.begin()) {
Serial.println("SD card is ready to use.");
return 1;
} else {
Serial.println("SD card initialization failed");
return 0;
}
}
int createFile(char filename[]) {
myFile = SD.open(filename, FILE_WRITE);
if (myFile) {
Serial.println("File created successfully.");
return 1;
} else {
Serial.println("Error while creating file.");
return 0;
}
}
void measure_td(){
sensor.read(); // tem sensor data read
sensor2.read(); // pres sensor data read
Serial.print("Temperature: ");
Serial.print(sensor.temperature());
Serial.println(" deg C");
Serial.print("Pressure: ");
Serial.print(sensor2.pressure());
Serial.println(" mbar");
myFile = SD.open("Sample.txt", FILE_WRITE);
if (myFile) {
//myFile.print(",");
myFile.print(j);
myFile.print("**");
myFile.print(float(sensor.temperature()));
myFile.print(",,");
myFile.print(float(sensor2.pressure()));
myFile.print("//");
myFile.close(); // close the file
j++;
}
// if the file didn't open, print an error:
else {
Serial.println("error opening Sample.txt");
}
//delay(3000);
}
/*void sd_log(){
myFile = SD.open("Sample.txt", FILE_WRITE);
if (myFile) {
//myFile.print(",");
myFile.print(j);
myFile.print("**");
myFile.print(float(sensor.temperature()));
myFile.print(",,");
myFile.print(float(sensor2.pressure()));
myFile.print("//");
myFile.close(); // close the file
j++;
}
// if the file didn't open, print an error:
else {
Serial.println("error opening Sample.txt");
}
//delay(3000);
}*/
void sd_logfinal(int a,double b,double c){
int q = a-1;
myFile2 = SD.open("Sample3.txt", FILE_WRITE);
if (myFile2) {
//myFile.print(",");
myFile2.print(q);
myFile2.print(",");
myFile2.print(b);
myFile2.print(",");
myFile2.print(c);
myFile2.print("/");
myFile2.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening Sample.txt");
}
}
float measure_p(){
sensor2.read(); // pres sensor data read
Serial.print("Pressure: ");
Serial.print(sensor2.pressure());
Serial.println(" mbar");
return sensor2.pressure();
}
int Read_Data() {
myFile = SD.open("Sample.txt");
if (myFile) {
while (myFile.available()) {
char Data = (char)myFile.read() ;
inputString += Data;
count++;
// read file until no more
}
Serial.println("save OK!");
myFile.close(); // close the file:
return 1;
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
return 0;
}
} // =================
댓글 1
조회수 21master님의 댓글
master 작성일
while문을 아주 좋아하나 봅니다.
제가 도와드릴 수 있는 수준이 아닌 듯 싶습니다.
c언어 문법을 공부하셔서 완성시켜보세요