more speed SD card

hi everyone,

I have been working to write on SD card to my datas (Accelerometer and GPS). There are people who recommend the SDFat library, but I couldn’t do that. I’m writing my code below. I expect your help. Thanks in advance…

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <TinyGPS.h>

File dosya;
TinyGPS gps;
LiquidCrystal_I2C lcd(0x27, 16, 2);

#ifndef GPSport_h
#define GPSport_h
#define gpsPort Serial1
#define GPS_PORT_NAME “Serial”
#define DEBUG_PORT Serial1
#endif

const int MPU_addr = 0x68;
unsigned long previousMillis = 0;
const long interval = 30;

float XAxisFinal;
float YAxisFinal;
float ZAxisFinal;
float ang_x;
float ang_y;
float ang_z;
int X_offset = 0;
int Y_offset = 0;
int Z_offset = 1700;

unsigned long simdikizaman;
const int chipSelect = 10;

void setup()
{

Serial.begin(9600);
Serial.print(“Initializing SD card…”);
Serial1.begin(9600);
lcd.begin();
lcd.clear();
lcd.print(“Hiziniz:”);
lcd.setCursor(8, 1);
lcd.print(“km/saat”);

Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission(true);
}

void loop() {

simdikizaman = millis();
int sensorState = analogRead(A0);

smartdelay(0);

uint8_t sat = gps.satellites();
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
int spd = gps.f_speed_kmph();
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age2;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age2);

lcd.setCursor(0, 1);
lcd.print(spd);

///////accelerometer

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 6, true);

int16_t XAxisFull = (Wire.read() << 8 | Wire.read()) + X_offset;
int16_t YAxisFull = (Wire.read() << 8 | Wire.read()) + Y_offset;
int16_t ZAxisFull = (Wire.read() << 8 | Wire.read()) + Z_offset;
XAxisFinal = (float) XAxisFull / 16384.0;
YAxisFinal = (float) YAxisFull / 16384.0;
ZAxisFinal = (float) ZAxisFull / 16384.0;

ang_x = atan(YAxisFull / (sqrt(pow(XAxisFull, 2) + pow(ZAxisFull, 2)))) * 57296 / 1000;
ang_y = atan(XAxisFull / (sqrt(pow(YAxisFull, 2) + pow(ZAxisFull, 2)))) * 57296 / 1000;

Serial.print(“sat: “); Serial.print(sat);
Serial.print(”\t”);
Serial.print(“spd: “); Serial.print(spd);
Serial.print(”\t”);
Serial.print(“lat: “); Serial.print(flat, 6);
Serial.print(”\t”);
Serial.print("\t");
Serial.print(“lon: “); Serial.print(flon, 6);
Serial.print(”\t”);
Serial.print("\t");
Serial.print(“hour: “); Serial.print(hour + 3);
Serial.print(”\t”);
Serial.print(“minute: “); Serial.print(minute);
Serial.print(”\t”);
Serial.print(“second: “); Serial.print(second);
Serial.print(”\t”);
Serial.print(Rfinal);
Serial.print("\t");
Serial.println(sensorState);

//////////////// SD CARD

dosya = SD.open(“myfile.txt”, FILE_WRITE);

//dosya.print(saat.dayofmonth);
//dosya.print(".");
//dosya.print(saat.month);
//dosya.print(".");
//dosya.print(saat.year);
//dosya.print("–");
//dosya.print(saat.hours);
//dosya.print(":");
dosya.print(“sat: “); dosya.print(sat);
dosya.print(”\t”);
dosya.print(“spd: “); dosya.print(spd);
dosya.print(”\t”);
dosya.print(“lat: “); dosya.print(flat, 6);
dosya.print(” “);
dosya.print(“lon: “); dosya.print(flon, 6);
dosya.print(”\t”);
dosya.print(“minute: “); dosya.print(minute);
dosya.print(”\t”);
dosya.print(“second: “); dosya.print(second);
dosya.print(”\t”);
dosya.print(simdikizaman);
dosya.print(”\t”);
//dosya.print(ZAxisFinal);
//dosya.print("\t");
dosya.print(Rfinal);
dosya.print("\t");
dosya.println(sensorState);
dosya.close();
}
}

static void smartdelay(unsigned long ms) {
unsigned long start = millis();
do {
while (Serial1.available())
gps.encode(Serial1.read());
} while (millis() - start < ms);
}

Topic moved

What made you think that the Website and Forum section was the correct place to post this ?

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

rizelioglu:
i dont understand. Isn’t this a good place to ask my question? If not, how and where can I ask questions?

This, the “Programming Questions” section of the forum, is a good place to ask your question, but you originally posted it in the “Website and Forum” section which was the wrong place for a programming question

Please edit your post to add code tags.

Your code is very, very slow because:

  1. you print a lot to the serial monitor at the very slow speed of 9600 Baud
  2. you open the SD file, write a tiny bit of data, close the file and repeat

The fixes:

  1. increase the serial Baud rate to 115200 or faster
  2. open the SD file once in setup(), and close it once, when you are all finished collecting data.

mushy91:
Do you get any errors when running your code?

Also, have you tried adding:

SD.begin(chipSelect);

inside of setup()?

When i run my code, no error occurs, but as mentioned above, it is very slow...
also, yes i tried SD.begin(chipSelect) and no change in speed

jremington:
Please edit your post to add code tags.

Your code is very, very slow because:

  1. you print a lot to the serial monitor at the very slow speed of 9600 Baud
  2. you open the SD file, write a tiny bit of data, close the file and repeat

The fixes:

  1. increase the serial Baud rate to 115200 or faster
  2. open the SD file once in setup(), and close it once, when you are all finished collecting data.

thanks for your interest, The important thing for me is only the increase of the SD card speed, I know the baud rate is only important for PC communication.

rizelioglu:
*open the SD file once in setup(), and close it once, when you are all finished collecting data. *
How can I do that?