sd card module dose not crate file

hello,
so I want to make an Arduino project to help keep track of how much coffee we sold in ower school caffé.
it should work like this:

  1. you press one of seven buttons depending on which type of coffee someone orders.

  2. the sd card module writes on to a .txt file what kind of coffee was order at witch price and ant what time.

  3. if you flip a switch the prices change for events.

here is the code that I wrote:

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


#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;



int led = 9;  

int sw = 10;

int preis = 0;

int l = 2;
int c = 3;
int e = 4;
int sh = 5;
int k = 6;
int g = 7;
int s = 8;


int lstate = 0;  
int cstate = 0;
int estate = 0; 
int shstate = 0; 
int kstate = 0; 
int gstate = 0; 
int sstate = 0; 



File myFile;

const int chipSelect = 10;
void setup() {

pinMode(l, INPUT);
pinMode(c, INPUT);
pinMode(e, INPUT);
pinMode(sh, INPUT);
pinMode(k, INPUT);
pinMode(g, INPUT);
pinMode(s, INPUT);

pinMode(sw, INPUT);
pinMode(led, OUTPUT);




  

Serial.begin(9600);

  clock.begin();
  
  clock.setDateTime(__DATE__, __TIME__);
  



Serial.print("Initializing SD card...");
if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
Serial.println("initialization done.");



// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:

digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);

}


void loop() {
if (myFile){
 dt = clock.getDateTime();
  
lstate = digitalRead(l);
cstate = digitalRead(c);
estate = digitalRead(e);
shstate = digitalRead(sh);
kstate = digitalRead(k);
gstate = digitalRead(g);
sstate = digitalRead(s);

//___________________________________________________________

if (sw == HIGH){
  preis = 0;
}else {
  preis = 1;
}


//*Veranstaltungspreise ändern !!!!!
//___________________________________________________________
 if (lstate == HIGH && (preis = 1)) {
   Serial.println("test latte");
    myFile.println("Latte Macchiato , 1.80 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (lstate == HIGH && (preis = 0)){

    myFile.println("Latte Macchiato , 2.50 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
    
 }

//___________________________________________________________
 if (cstate == HIGH && (preis = 1)) {
    Serial.println("test cap");
    myFile.println("Capuchino , 1.50 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (cstate == HIGH && (preis = 0)){

    myFile.println("Capuchino , 2.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//___________________________________________________________ 
 if (estate == HIGH && (preis = 1)) {
   
    myFile.println("Espresso , 1.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (estate == HIGH && (preis = 0)){

    myFile.println("Espresso , 1.50 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//___________________________________________________________
 if (shstate == HIGH && (preis = 1)) {
   
    myFile.println("Heiße Schokolade , 1.80 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (shstate == HIGH && (preis = 0)){

    myFile.println("Heiße Schokolade , 2.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//___________________________________________________________ 
 if (kstate == HIGH && (preis = 1)) {
   
    myFile.println("Kaffe , 1.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (kstate == HIGH && (preis = 0)){

    myFile.println("Kaffe , 1.50 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//___________________________________________________________ 
 if (gstate == HIGH && (preis = 1)) {
   
    myFile.println("Galao , 1.50 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (gstate == HIGH && (preis = 0)){

    myFile.println("Galao , 1.80 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//___________________________________________________________ 
 if (sstate == HIGH && (preis = 1)) {
   
    myFile.println("Spezial , 1.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
  
  } else if (sstate == HIGH && (preis = 0)){

    myFile.println("Spezial , 1.00 , ");
    myFile.print(dt.day);   myFile.print(".");
    myFile.print(dt.month);   myFile.print(".");
    myFile.print(dt.year);
    myFile.println("");
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
 }

//____________________________________________________________
}else {
    
    Serial.println("error opening test.txt");
  }
   myFile.close();
}

the problem that I have is, that the Arduino does not create a .txt file.
it just goes right into an error loop. I tried other examples and they work just fine.

I would really appreciate your help.

best regards, Lorenz

You open the file in your setup() function:

  myFile = SD.open("test.txt", FILE_WRITE);

setup() only runs one, right after you power on or reset your Arduino board.

You close the file at the end of your loop() function:

  myFile.close();

the loop() function runs over and over again. So after the first time through loop(), the file is closed and so this line:

  if (myFile) {

will always be false from then on, thus the "error loop".

thanks for your quick reply :smiley:

so if I move the myFile = SD.open("test.txt", FILE_WRITE); command in to the loop()
and remove the myFile.close(); it should work ?
or am I missing something else. because I still get a errormessage.
Sorry for the questions I'm new to the sd module :wink:

No

If you close a file, you need to open it if you write again.
2)
Somewhere along the line you will have to close the file if you want to remove the card.

The usual approach (suitable for most applications)

void loop()
{
    open file
    check if opening succeeded
    if so
        write to file
        close file
}

thank you very much :smiley:
I will test this immediately