Mini Pro RTC&HCSR04&SD

Hallo Leute,

Ich habe eine Bitte ich stehe irgendwie voll an
mein Problem. Das Programm (Code1) mach ja genau das was ich will aber nur im Serial Monitor am PC
nun möchte ich die Daten die ich am Serial Monitor bekomme auf ne SD schreiben aber ich bekomms einfach nciht hin (Code2)

Code 1:

#include <Wire.h>    // I2C-Bibliothek einbinden
#include "RTClib.h"  // RTC-Bibliothek einbinden
#include <SPI.h>
#include <SD.h>
#include <LowPower.h>

const int chipSelect = 4;
File myFile;

RTC_DS1307 RTC;      // RTC Modul


#define trigPin 9
#define echoPin 10



void setup() {

   // Initialisiere I2C  
  Wire.begin();
  
  // Initialisiere RTC
  RTC.begin();

  
  Serial.begin (9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  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:
    return;
  }
  Serial.println("card initialized.");

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  
}

int handda=0 ;

void loop() {
// make a string for assembling the data to log:

 String dataString = "";
  dataString += RTC.now;


 
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if ((distance < 9) && (handda==2)) {
    handda=0;// This is where the LED On/Off happens  
   
  DateTime now=RTC.now(); // aktuelle Zeit abrufen
  
  show_time_and_date(now);  // Datum und Uhrzeit ausgeben

                                      }
 if (distance > 15) {
  handda=2;
 delay(1000);
                   }
            } 



  // Wochentag ermitteln
String get_day_of_week(uint8_t dow){ 
  
  String dows="  ";
  switch(dow){
   case 0: dows="So"; break;
   case 1: dows="Mo"; break;
   case 2: dows="Di"; break;
   case 3: dows="Mi"; break;
   case 4: dows="Do"; break;
   case 5: dows="Fr"; break;
   case 6: dows="Sa"; break;
  }
  
  return dows;
}

// Datum und Uhrzeit ausgeben
void show_time_and_date(DateTime datetime){

  
  // Wochentag, Tag.Monat.Jahr
  Serial.print(get_day_of_week(datetime.dayOfWeek()));
  Serial.print(", ");
  if(datetime.day()<10)Serial.print(0);
  Serial.print(datetime.day(),DEC);
  Serial.print(".");
  if(datetime.month()<10)Serial.print(0);
  Serial.print(datetime.month(),DEC);
  Serial.print(".");
  Serial.print(datetime.year(),DEC);
  
  // Stunde:Minute:Sekunde
  if(datetime.hour()<10)Serial.print(0);
  Serial.print(datetime.hour(),DEC);
  Serial.print(":");
  if(datetime.minute()<10)Serial.print(0);
  Serial.print(datetime.minute(),DEC);
  Serial.print(":");
  if(datetime.second()<10)Serial.print(0);
  Serial.print(datetime.second(),DEC);

       }

Bitte um eure Hilfe vl kann mich ja jeman in die richtige Richtung Schieben :slight_smile:

Code2 fehlt.

Sry hab ich nicht bemerkt

Code 2:

#include <Wire.h>    // I2C-Bibliothek einbinden
#include "RTClib.h"  // RTC-Bibliothek einbinden
#include <SPI.h>
#include <SD.h>


RTC_DS1307 RTC;      // RTC Modul
const int chipSelect = 4;

#define trigPin 9
#define echoPin 10



void setup() {

   // Initialisiere I2C  
  Wire.begin();
  
  // Initialisiere RTC
  RTC.begin();

  
  Serial.begin (9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }




  

  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:
    return;
  }
  Serial.println("card initialized.");



  

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

int handda=0 ;

void loop() {
// make a string for assembling the data to log:
 
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if ((distance < 9) && (handda==2)) {
    handda=0;// This is where the LED On/Off happens  
   
  DateTime now=RTC.now(); // aktuelle Zeit abrufen
  
  show_time_and_date(now);  // Datum und Uhrzeit ausgeben



  File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (dataFile) {
    dataFile.println("test");
    // print to the serial port too:
    Serial.println("test");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  
                                      }
 if (distance > 15) {
  handda=2;
 delay(1000);
                   }
            } 



  // Wochentag ermitteln
String get_day_of_week(uint8_t dow){ 
  
  String dows="  ";
  switch(dow){
   case 0: dows="So"; break;
   case 1: dows="Mo"; break;
   case 2: dows="Di"; break;
   case 3: dows="Mi"; break;
   case 4: dows="Do"; break;
   case 5: dows="Fr"; break;
   case 6: dows="Sa"; break;
  }
  
  return dows;
}

// Datum und Uhrzeit ausgeben
void show_time_and_date(DateTime datetime){

  
  // Wochentag, Tag.Monat.Jahr
  dataFile.println(get_day_of_week(datetime.dayOfWeek()));
  dataFile.println(", ");
  if(datetime.day()<10)dataFile.println(0);
  dataFile.println(datetime.day(),DEC);
  dataFile.println(".");
  if(datetime.month()<10)dataFile.println(0);
  dataFile.println(datetime.month(),DEC);
  dataFile.println(".");
  dataFile.println(datetime.year(),DEC);
  
  // Stunde:Minute:Sekunde
  if(datetime.hour()<10)dataFile.println(0);
  dataFile.println(datetime.hour(),DEC);
  dataFile.println(":");
  if(datetime.minute()<10)dataFile.println(0);
  dataFile.println(datetime.minute(),DEC);
  dataFile.println(":");
  if(datetime.second()<10)dataFile.println(0);
  dataFile.println(datetime.second(),DEC);

       }