Ich betreibe meinen Arduino Uno mit DS1307 RTC über IDE 2.2.1 an meinem iMac. Der beigefügte Sketch soll die Entladungskurve eines NiMH-Akkus unter Belastung aufzeichnen. Die Daten sollen auf die SD-Karte geschrieben werden und auch auf dem Monitor erscheinen. Letzteres funktioniert richtig aber beim schreiben auf die Karte erhalte ich jedes Mal die Meldung "error opening Akkutest1.txt". Außerdem bleibt das Programm ohne Fehlermeldung öfter stehen. Was kann ich tun?
// Spannungsverlust eines NIMH-Akkus unter Last
#include "RTClib.h" // Lib für RTC DS 1307
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
long funfMin=300000; // 300 Sekunden
RTC_DS1307 rtc;
void setup () {
Serial.begin(9600);
// while (!Serial); // wait for serial port to connect. Needed for native USB
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.adjust(DateTime(2024, 1, 20, 13, 20, 0));
delay(1000);
Serial.print("Initializing SD card...");
// 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.");
}
int nc=0;
void loop () {
File datafile = SD.open("Akkutest1.txt", FILE_WRITE);
//loop über 96*5 Minuten = 8h
while (nc<96) {
DateTime now = rtc.now();
// read A0 pin
int sensor = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3V):
float voltage = sensor * (5.0 / 1023.0);
// if the file is available, write to it:
if (datafile) {
datafile.print(nc);
datafile.print(' ');
datafile.print(now.year(), DEC);
datafile.print(' ');
datafile.print(now.month(), DEC);
datafile.print(' ');
datafile.print(now.day(), DEC);
datafile.print(' ');
datafile.print(now.hour(), DEC);
datafile.print(' ');
datafile.print(now.minute(), DEC);
datafile.print(' ');
datafile.print(voltage);
datafile.println();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening Akkutest1.txt");
}
// print to the serial port too:
Serial.print(nc);
Serial.print(' ');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.month(), DEC);
Serial.print(' ');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(' ');
Serial.print(now.minute(), DEC);
Serial.print(' ');
Serial.print(voltage);
Serial.println();
delay(funfMin);
nc=nc+1;
}
datafile.close();
Serial.println("fertig");
}