I am using an Arduino to read the rtc, GPS and sensor values. and then i try to save data in my sd card. but when I try to run this microcontroller for +- 10 minutes, but there is only a few data stored. is there any mistake i made in the code? because every time I run the microcontroller is always like that?
this is the code
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
#define MQ2pin (A0)
#define MQ4pin (A1)
#define sensorIn 3
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define FILE_BASE_NAME "data"
File myFile;
DS3231 rtc(SDA, SCL);
const uint8_t CS_PIN = 10;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.txt";
float sensorValue; //variable to store sensor value
float sensor2Value;
float sensor3Value; //H2S Sensor2
static const int RXPin = 5, TXPin = 4;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup() {
Serial.begin(9600);
pinMode(CS_PIN, OUTPUT);
ss.begin(GPSBaud);
// SD Card Initialization
if (SD.begin())
{
Serial.println("Gas sensor warming up!");
delay(2000); // allow the MQ-6 to warm up
Serial.println("SD card is ready to use.");
Serial.println("Time,Latitude, Longitude, Altitude, H2s Value, CH4, CO2 Value");
}
while (SD.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '99') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
Serial.print(F("opened: "));
Serial.println(fileName);
myFile.close();
}
}
rtc.begin();
}
void loop() {
sensorValue = analogRead(MQ2pin); // read analog input pin 0
sensor2Value = analogRead(MQ4pin); // read analog input pin 1
sensor3Value = analogRead(sensorIn); // read analog input pin 1
Serial.print(rtc.getDateStr()); //prosedur pembacaan tanggal
Serial.print(",");
Serial.print(rtc.getTimeStr()); //prosedur pembacaan waktu
Serial.print(",");
Serial.print(gps.location.lat());
Serial.print(",");
Serial.print(gps.location.lng());
Serial.print(",");
Serial.print(gps.altitude.meters());
Serial.print(",");
Serial.print(sensorValue);
Serial.print(",");
Serial.print(sensor2Value);
Serial.print(",");
Serial.print(sensor3Value);
Serial.println(" ");
myFile = SD.open(fileName, FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
myFile.print(sensorValue);
myFile.print(",");
myFile.print(sensor2Value);
myFile.print(",");
myFile.print(sensor3Value);
myFile.println(" ");
myFile.close(); // close the file
}
// if the file didn't open, print an error:
//else {
//Serial.println("error opening file");
//}
myFile = SD.open(fileName, FILE_WRITE);
if (myFile) {
Serial.println(F("open failed"));
return;
}
Serial.print(F("opened: "));
Serial.println(fileName);
myFile.close();
smartDelay(200);
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
and this is the recorded data
