Hello, i have problem with sd card writing... I use DS1302 RTC, sd card reader and a light sensor.
I get good light readings on serial monitor, but on sd card i get 4-5 black dots... Also time is displayed like this:
12:00:00
aaaaa
12:00:05
aaaaa
00:00:00
aaaaa
12:00:15
00:00:00
12:00:25
00:00:00
aaaaa
12:00:35
Please help, this is my code:
#include <SD.h>
#include <SPI.h>
#include <DS1302.h>
// Init the DS1302
DS1302 rtc(4, 3, 2);
int CS_PIN = 10;
File file;
const int svetlostPin = A0;
int stanje = 0;
void setup()
{
Serial.begin(9600);
rtc.halt(false);
rtc.writeProtect(false);
// The following lines can be commented out to use the values already stored in the DS1302
rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY
rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(6, 8, 2010); // Set the date to August 6th, 2010
initializeSD();
}
void loop()
{
stanje = analogRead(svetlostPin);
createFile("svetlost.txt");
writeToFile(rtc.getTimeStr());
writeToFile(analogRead(A0));
closeFile();
Serial.println(stanje);
delay(5000);
}
void initializeSD()
{
Serial.println("Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
int createFile(char filename[])
{
file = SD.open(filename, FILE_WRITE);
if (file)
{
Serial.println("Tekst dokument uspesno kreiran");
return 1;
} else
{
Serial.println("Tekst dokument neuspesno kreiran");
return 0;
}
}
int writeToFile(char text[])
{
if (file)
{
file.println(text);
Serial.println("Writing to file: ");
Serial.println(text);
return 1;
} else
{
Serial.println("Couldn't write to file");
return 0;
}
}
void closeFile()
{
if (file)
{
file.close();
Serial.println("File closed");
}
}
int openFile(char filename[])
{
file = SD.open(filename);
if (file)
{
Serial.println("File opened with success!");
return 1;
} else
{
Serial.println("Error opening file...");
return 0;
}
}
String readLine()
{
String received = "";
char ch;
while (file.available())
{
ch = file.read();
if (ch == '\n')
{
return String(received);
}
else
{
received += ch;
}
}
return "";
}
aaaaa