problem with writing on sd card

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

you are doing this writeToFile([color=red]analogRead(A0)[/color]);

for a function you defined like this int writeToFile([color=red]char text[][/color])

do you see the pb?

didn't you get an alert when compiling?

Im new to programming.. Pls explain to me what does pb mean? Also i dont understand char text[]..
If i wrote stanje = analogRead(A0), does that mean i should write writeToFile(stanje)? Because i got same thing with that... :slight_smile:

well better start then with a good book or tutorial on the syntax of the language...

trying to explain a bit, consider this code

char myPhrase[] = "Hello World";
int value = 100;

void writePhrase(char text[])
{
  Serial.print("The text is: '");
  Serial.print(text);
  Serial.println("'");
}


void setup() {
  Serial.begin(115200);
  writePhrase(myPhrase); // kinda OK  passing indeed an array of characters 
  writePhrase(value); // Bad - not passing an array of characters 
}

void loop() {}

if you compile this, you'll see that the compiler is not happy and throws warnings

warning: invalid conversion from 'int' to 'char*' [-fpermissive]
   writePhrase(value);
 note: initializing argument 1 of 'void writePhrase(char*)'
 void writePhrase(char text[])

so that tells you your code is not perfect and you need to fix this...

if you run it anyway, you'll see in the console

The text is: 'Hello World'
The text is: ''

so the first phrase worked but clearly it did not work for the second because I passed an integer to a function which was expecting a pointer to a string of character

hope that helps a bit