Arduino Polska Forum
RTC + SD + MPU6050 - Błąd Zapisu - Wersja do druku

+- Arduino Polska Forum (https://forum.arduinopolska.pl)
+-- Dział: Korzystanie z Arduino (https://forum.arduinopolska.pl/dzial-korzystanie-z-arduino)
+--- Dział: Magazynowanie (https://forum.arduinopolska.pl/dzial-magazynowanie)
+--- Wątek: RTC + SD + MPU6050 - Błąd Zapisu (/watek-rtc-sd-mpu6050-b%C5%82%C4%85d-zapisu)



RTC + SD + MPU6050 - Błąd Zapisu - jayoz - 15-03-2018

Cześć, mam pewien problem z układem składającym się z:
- SD/RTC shielda 
[Obrazek: a7ab9b504fe9b5ec9149a2b79044]

- MPU6050
[Obrazek: d3054fb14d2db71c41f79e174e1e]

Kiedy zapisuję dane tylko z MPU6050 wszystko jest ok, logi tworzą się jeden pod drugim. Natomiast kiedy wstawiłem bibliotekę RTClib.h i kod odpowiadający za wstawianie daty i godziny do danych, po odczytaniu karty w pliku znajduje się tylko jeden log... Co może być przyczyną tak dziwnego zachowania?
[Obrazek: Bez_tytu_u.png]
Kod:
Kod:
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <MPU6050.h>
#include "RTClib.h"

MPU6050 mpu;

const int chipSelect = 10;
float accPitch = 0;
float accRoll = 0;
float totalG = 0;

RTC_Millis rtc;

void setup() {
 mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_8G);
 delay(1000);
 mpu.calibrateGyro();
 mpu.setThreshold(3);
 delay(1000);

 rtc.begin(DateTime(F(__DATE__), F(__TIME__)));

 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port 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.");




}

void loop() {

 Vector normAccel = mpu.readNormalizeAccel();
 Vector normGyro = mpu.readNormalizeGyro();

 accPitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis)) * 180.0) / M_PI;
 accRoll = (atan2(normAccel.YAxis, normAccel.ZAxis) * 180.0) / M_PI;

 totalG = sqrt(sq(normAccel.ZAxis) + (sqrt(sq(normAccel.XAxis) + sq(normAccel.YAxis)))) - 9, 5;

 // make a string for assembling the data to log:
 String dataString = "";

 DateTime now = rtc.now();

 //**************************YEAR*********************************
 dataString += String(now.year(), DEC);
 dataString += "/";

 //**************************MONTH*********************************

 if (now.month() < 10)
 {
   dataString += "0";
 }
 dataString += String(now.month(), DEC);
 dataString += "/";
 //**************************DAY*********************************

 if (now.day() < 10)
 {
   dataString += "0";
 }
 dataString += String(now.day(), DEC);
 dataString += "          ";

 //**************************HOUR*********************************
 if (now.hour() < 10)
 {
   dataString += "0";
 }
 dataString += String(now.hour(), DEC);
 dataString += ":";

 //**************************MINUTE*********************************
 if (now.minute() < 10)
 {
   dataString += "0";
 }
 dataString += String(now.minute(), DEC);
 dataString += ":";
 //**************************SECONDS*********************************

 if (now.second() < 10)
 {
   dataString += "0";
 }
 dataString += String(now.second(), DEC);
 dataString += "          ";

 //**************************DATA*********************************

 dataString += String(accPitch);
 dataString += "          ";
 dataString += String(accRoll);
 dataString += "          ";
 dataString += String(normAccel.XAxis);
 dataString += "          ";
 dataString += String(normAccel.YAxis);
 dataString += "          ";
 dataString += String(normAccel.ZAxis);
 dataString += "          ";
 dataString += String(totalG);


 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile = SD.open("dane.txt", FILE_WRITE);

 // if the file is available, write to it:
 if (dataFile) {

   dataFile.println(dataString);
   dataFile.close();

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