본문 바로가기
아두이노

아두이노를 이용한 데이터 저장 -#6

by 오징어땅콩2 2020. 9. 5.
반응형

 

오늘은 조금 더 실질적인 값을 기록해 보자.

딱히 저번것하고 다른것은 없지만 그래도 측정한 데이터 값을 기록하는데 있어서 다르다.

TM35온도계 5개의 온도를 측정해서 기록하는것으로 하자.

연결하는것은 A0 ~ A4번까지 물론 6개 열결 할 수도 있지만, 가진 TM35온도계가 5개 뿐이다.

한개 연결한것과 5개 연결한것은 크게 차이가 없으니 하나로 테스트해도 무방하다.

 

 
온도를 읽는 추가된 코드
void read_temp(Info& _info)
{
 
    for(int i =0; i<5;i++)
    {
      _info.Temp[i]  = analogRead (A0+i); // A0의 값을 불러옵니다.
      _info.Temp[i] *= 5.0 / 10.24//온도값을 계산하는 공식입니다.
    }
    return  ;
}
 
 
void print_temp(Info& _info, char* p)
{
  
    p+= sprintf(p, "Temperature: ");
    for(int i =0; i<5;i++)
    {
        p+= sprintf(p, "%s", String(_info.Temp[i],2).c_str());
        p+= sprintf(p,  "C,  ");
    } 
    p+= sprintf(p,  "\n");
    
    return ; 
}

 

전체코드

 
#include <SPI.h>
#include <SD.h>
 
 
#define version 0x01
#define check 0xFF
 
typedef struct _Info
  char ver;
  float Temp[5];
  char chk;
    
  _Info()
  {
    ver = version;
    chk = check;
    memset(Temp, 0x00sizeof(float)*5);
  }
}Info;
void read_temp(Info& _info);
void print_temp(Info& _info);
File myFile;
Info adcinfo;
volatile unsigned long previousMillis;
 
 
void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
 
  Serial.print("Initializing SD card...");
 
  if (!SD.begin(10)) 
  {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  previousMillis = millis(); 
  
}
 
void loop() 
{
    unsigned long currentMillis = millis();
 
    if ((currentMillis-previousMillis)> 1000*1)
    {
        read_temp(adcinfo); 
        myFile = SD.open("test.txt", FILE_WRITE);
        // if the file opened okay, write to it:
        if (myFile) 
        {   
              Serial.print("Writing to test.txt...");
                           
              char buf[100];        
              print_temp(adcinfo, buf);       
              Serial.print(buf);
              Serial.println("done.");
              myFile.println(buf);            
              myFile.close();            
        } 
        else 
        {
              // if the file didn't open, print an error:
              Serial.println("error opening test.txt");
        }
     previousMillis = currentMillis;   
    }
    delay(100);
}
 
void read_temp(Info& _info)
{
 
    for(int i =0; i<5;i++)
    {
      _info.Temp[i]  = analogRead (A0+i); // A0의 값을 불러옵니다.
      _info.Temp[i] *= 5.0 / 10.24//온도값을 계산하는 공식입니다.
    }
    return  ;
}
 
 
void print_temp(Info& _info, char* p)
{
  
    p+= sprintf(p, "Temperature: ");
    for(int i =0; i<5;i++)
    {
        p+= sprintf(p, "%s", String(_info.Temp[i],2).c_str());
        p+= sprintf(p,  "C,  ");
    } 
    p+= sprintf(p,  "\n");
    
    return ; 
}

 

 

 

댓글