SD카드의 데이터를 읽고 쓰기가 가능한 모듈입니다.
SPI통신을 사용합니다.
연결법
SD 라이브러리는 Adafruit 웹사이트에서 다운로드 받을 수 있습니다.
https://github.com/adafruit/SD
소스 코드
/*
Requires the SD library from the Adafruit website
* SD card attached MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 10
*/
#include
const int chipSelect = 10;
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SS, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
// create a new file
char filename[] = "LOGGER00.TXT";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
dataFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! dataFile) {
Serial.println("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
}
int i = 0; //Use this for the counter
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
dataFile.println(dataString);
// print to the serial port too:
//Serial.println(' ');
// The line dataFile.flush() will 'save' the file to the SD card after every
// line of data - this will use more power and slow down how much data
// you can read but it's safer!
// If you want to speed up the system, call flush() less often and it
// will save the file only when called and every 512 bytes - every time a sector on the
// SD card is filled with data. However, don't depend on the automatic flush!
i++;
if (i > 50) { //Call every 50 times
dataFile.flush();
i = 0;
}
// Take 1 measurement every 10 milliseconds
delay(10);
}
상품이 장바구니에 담겼습니다.
바로 확인하시겠습니까?
상품이 찜 리스트에 담겼습니다.
바로 확인하시겠습니까?
Copyright © Mechasolution. All rights reserved.









