Arduino:
텍스트 파일 읽기

How to: (방법)

아두이노에선 SD 카드 모듈을 사용해 텍스트 파일을 읽을 수 있다. 간단한 예제를 살펴보자.

#include <SD.h>
#include <SPI.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }

  if (!SD.begin(4)) {
    Serial.println("SD card initialization failed!");
    return;
  }
  
  File dataFile = SD.open("example.txt");
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  } else {
    Serial.println("File open failed!");
  }
}

void loop() {
  // Nothing here
}

파일에 있는 내용이 시리얼 모니터에 표시된다.

Deep Dive (심층 분석)

텍스트 파일 읽기 기능은 아두이노에서 다양한 프로젝트에 활용되며, 역사적으로는 punched tape에서 시작하여 디지털 저장 매체로 발전했다. SD 카드 모듈 사용은 가장 일반적인 방법이지만, EEPROM이나 인터넷을 통한 데이터 접근 방식도 있다. 데이터 형식에 따라 read(), readBytes(), readString() 등 다양한 함수를 사용하여 구현할 수 있다.

See Also (참고 자료)