15-03-2018, 14:23
Cześć, mam pewien problem z układem składającym się z:
- SD/RTC shielda
- MPU6050
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?
Kod:
- SD/RTC shielda
- MPU6050
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?
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");
}
}