Hallo ik heb een probleem met het schrijven naar een SD kaartje.

Hallo Allemaal,

ik ben nieuw hier maar kom gelijk met een vraag.( dit is ook de reden van het aan melden op het forum)
Ben al een tijdje aan het prutsen met arduino.
en ben met Google al vaak uit de problemen gekomen.

Maar gelukkig zijn jullie er.

Het lukt mij dus niet om een log naar een SD te schrijven.
Het bestand word wel gemaakt en dan een keer bij geschreven.
Daarna lukt het niet meer om er bij te schrijven.

Hier mijn Code

#include <SD.h>
#include <SPI.h>
#include <INA219.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


INA219 ina219;

#define R_SHUNT 0.1
#define V_SHUNT_MAX 0.2
#define V_BUS_MAX 32
#define I_MAX_EXPECTED 3

File myFile ;
const int chipSelect = 10;
const int analogInPin = A0;  
const int relayPin = 2;
boolean led=true;
int sensorValue = 0;        
int cell1 = 0; 
int intervalReadData = 2000;
int intervalDisplay = 2000;      

float shuntvoltage = 0;
float busvoltage = 0;
float current_A = 0;
float batvoltage = 0;
float power = 0;
float Ah = 0;
float batvoltage1 = 0;

unsigned long lastread = 0; 
unsigned long tick;      
unsigned long previousMillisReadData = 0;
unsigned long previousMillisDisplay = 0;


void setup() {

  pinMode(relayPin, OUTPUT);
  pinMode(chipSelect, OUTPUT);
  pinMode(3, OUTPUT);
  
  Serial.begin(57600);
  Serial.println(F("Hello - Solar control system is booting up !!!!!"));
  delay(1000);
  
  ina219.begin();
  ina219.configure(INA219::RANGE_32V, INA219::GAIN_8_320MV, INA219::ADC_12BIT, INA219::ADC_12BIT, INA219::CONT_SH_BUS);
  lastread = millis();
  ina219.calibrate(R_SHUNT, V_SHUNT_MAX, V_BUS_MAX, I_MAX_EXPECTED);

  Serial.print(F("Initializing SD card..."));

  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    return;
  }
  Serial.println(F("initialization done."));

  lcd.begin(16,2);
  

  delay(964);
}

void loop() {

  myFile = SD.open("datalog.txt",FILE_WRITE);
  
  sensorValue = analogRead(analogInPin);
  float cell1= sensorValue * (5.000 / 1023.0);
  float cell2= batvoltage - cell1 ;
  unsigned long currentMillis = millis();  

  if ((unsigned long)(currentMillis - previousMillisReadData) >= intervalReadData) {
    previousMillisReadData = millis();
    readCurrent();
    Serial.print("tick:   "); Serial.print(tick); Serial.println(" ms");
    }
  
  if ((unsigned long)(currentMillis - previousMillisDisplay) >= intervalDisplay) {
    previousMillisDisplay = millis();
    // displays data
    
    
    Serial.print(F("Solar control systemparameters: "));
    Serial.println(F(""));
    Serial.print(F("Time:           "));Serial.print(tick);Serial.println(F(" ms"));
    Serial.print(F("Bat Voltage:    "));Serial.print(batvoltage,3); Serial.println(F(" V"));
    Serial.print(F("Voltage Cell 1: "));Serial.print(cell1,3); Serial.println(F(" V"));
    Serial.print(F("Voltage Cell 2: "));Serial.print(cell2,3); Serial.println(F(" V"));
    Serial.print(F("Current:        "));Serial.print(current_A,3); Serial.println(F(" A"));
    Serial.print(F("Watts:          "));Serial.print(power,3); Serial.println(F(" W"));
    Serial.print(F("Ah:             "));Serial.print(Ah,3); Serial.println(F(" Ah"));
    Serial.println(F(""));
    lcd.setCursor(0,0);
    lcd.print(F("C1:"));lcd.print(cell1,2);lcd.print(F(" C2:"));lcd.print(cell2,1);
    lcd.setCursor(0,1);
    lcd.print(F("Amp:"));lcd.print(current_A,2);lcd.print(F("A "));lcd.print(Ah,2); lcd.print("Ah");
    
    

    if (myFile) {
    Serial.print(F("Writing to log...."));
    myFile.print(F("Time:           ")); myFile.print(tick);myFile.print(": ms:");
    myFile.print(F("Bat Voltage:    ")); myFile.print(batvoltage,3); myFile.print(": V:");
    myFile.print(F("Voltage Cell 1: ")); myFile.print(cell1,3); myFile.print(": V:");
    myFile.print(F("Voltage Cell 2: ")); myFile.print(cell2,3); myFile.print(": V:");
    myFile.print(F("Current:        ")); myFile.print(current_A,3); myFile.print(": A:");
    myFile.print(F("Watts:          ")); myFile.print(power,3); myFile.print(": W:");
    myFile.print(F("Ah:             ")); myFile.print(Ah,3); myFile.println(": Ah:");
    myFile.close();
    Serial.println(F("done."));
     } 
    else {
    Serial.println(F("error writing to log...."));
     }
  
  }

  if (cell1 > 3.500){
    digitalWrite(relayPin, HIGH);    
  }
  if (cell1 < 2.600){
    digitalWrite(relayPin, LOW);
  }

  digitalWrite(3, led);
  led = !led;
  
  delay(10);
}

void readCurrent() {
  uint32_t count = 0;
  unsigned long newtime;

  
  busvoltage = ina219.busVoltage();
  
  while(!ina219.ready() && count < 500) {
    count++;
    delay(1);
    busvoltage = ina219.busVoltage();  
  }
  
  
  shuntvoltage = ina219.shuntVoltage() * 1000;
  current_A = ina219.shuntCurrent();
  batvoltage1 = busvoltage + (shuntvoltage / 1000);
  batvoltage = batvoltage1 + 0.421 ;
  power = ina219.busPower();
  newtime = millis();
  tick = newtime - lastread;
  Ah += (current_A * tick)/3600000.0;
  lastread = newtime;

 
  ina219.recalibrate();
  ina219.reconfig();
}

Hier ook wat info uit de seriële monitor.

Hello - Solar control system is booting up !!!
Initializing SD card…initialization done.
tick: 1197 ms
Solar control systemparameters:
Time: 1197 ms
Bat Voltage: 11.109 V
Voltage Cell 1: 4.995 V
Voltage Cell 2: -4.995 V
Current: 0.001 A
Watts: 0.004 W
Ah: 0.000 Ah

Writing to log…done.
tick: 2009 ms
Solar control systemparameters:
Time: 2009 ms
Bat Voltage: 9.715 V
Voltage Cell 1: 4.990 V
Voltage Cell 2: 4.725 V
Current: 0.663 A
Watts: 6.108 W
Ah: 0.000 Ah

error writing to log…
tick: 2003 ms
Solar control systemparameters:
Time: 2003 ms
Bat Voltage: 10.011 V
Voltage Cell 1: 4.995 V
Voltage Cell 2: 5.016 V
Current: 0.663 A
Watts: 6.312 W
Ah: 0.001 Ah

error writing to log…
tick: 2003 ms
Solar control systemparameters:
Time: 2003 ms
Bat Voltage: 9.727 V
Voltage Cell 1: 4.990 V
Voltage Cell 2: 4.737 V
Current: 0.662 A
Watts: 6.108 W
Ah: 0.001 Ah

Hulp word zeer gewaardeerd.

Gr. Ferdinand.