I'm trying to do a weather station project that logs the soil humidity, pressure, and temperature in a SD card, it uses a Arduino nano (ATmega328P) using the old bootloader config, the SD card is this one:
formatted FAT32
the data written in the SD looks like this:
ET:(Millis since start),T:(temperature), M:(a relative moisture%),P:(relative Pressure%)
the problem is that passed some write cycles the SD card disconnects, and is not able to connect back, at least until i pop the SD into my pc and erase the txt file, the max amount of data it stores before the disconnect happens is about 5Kb and 8kb of data, or 130 and 190 lines of text in the txt file, it is very random and that is only the bottomline. I suspect some library shenanigans,
here is the horrible code i wrote:
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#define sensorPin A1
#define ONE_WIRE_BUS 2
const int chipSelect = 4;
const int PIn =7;
const int POut =5;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup()
{
pinMode(PIn, INPUT);
pinMode(POut, OUTPUT);
Serial.begin(9600);
sensors.begin();
lcd.init();
lcd.clear();
lcd.backlight();
Serial.print("Initializing SD card");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed to be detected");
while (!SD.begin(chipSelect));
}
Serial.println("Card initialized");
File dataFile = SD.open("data.txt",FILE_WRITE);
if (dataFile)
{
dataFile.println("**Data session separator**");
dataFile.close();
}
else {Serial.println("Error opening data.txt");}
lcd.setCursor(0, 0);
lcd.print("Weather<><><>");
lcd.setCursor(0, 1);
lcd.print("<>Station<><>");
delay(1000);
}
int ReadDelay = 20;
int LoopDelay = 100;
unsigned long StartTime = millis();
void loop()
{
lcd.clear();
lcd.noBacklight();
float Temp =0;
for(int i=1;i<7;i++)
{
sensors.requestTemperatures();
Temp =(Temp*(i-1)+sensors.getTempCByIndex(0))/i;
delay(10);
}
float Moist =0;
for(int i=1;i<7;i++)
{
Moist =(Moist*(i-1)+analogRead(A0))/i;
delay(10);
}
Moist =map(Moist,500,250,0,100);
float P =0;
for(int i=1;i<7;i++)
{
while (digitalRead(PIn));
long result =0;
for (int i=0;i<24;i++)
{
digitalWrite(POut,HIGH);
digitalWrite(POut,LOW);
result =result<<1;
if (digitalRead(PIn)) {result++;}
}
result =result^0x800000;
for (char i=0;i<3;i++)
{
digitalWrite(POut,HIGH);
digitalWrite(POut,LOW);
}
P =(P*(i-1)+result)/i;
delay(10);
}
P =map(P,7755716,12582911,0,100);
lcd.backlight();
Serial.print("0:");
Serial.print(0);
Serial.print(",");
Serial.print("100:");
Serial.print(100);
Serial.print(",");
Serial.print("25:");
Serial.println(25);
unsigned long ElapsedTime =millis();-StartTime;
String dataString ="";
dataString +="ET:";
dataString +=String(ElapsedTime);
dataString +=",";
dataString +="T:";
dataString +=String(Temp);
dataString +=",";
dataString +="M:";
dataString +=String(Moist);
dataString +=",";
dataString +="P:";
dataString +=String(P);
dataString +=",";
File dataFile =SD.open("data.txt",FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
Serial.println("error opening data.txt");
lcd.setCursor(1,0);
lcd.print("sd error");
SD.end();
while(!SD.begin(chipSelect));
}
lcd.clear();
lcd.setCursor(1,0);
lcd.print("temperature C");
lcd.setCursor(2,1);
lcd.print(Temp);
delay(ReadDelay);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Moisture R%");
lcd.setCursor(2,1);
lcd.print(Moist);
delay(ReadDelay);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Pressure %");
lcd.setCursor(2,1);
lcd.print(P);
delay(ReadDelay);
lcd.clear();
lcd.noBacklight();
delay(LoopDelay);
}
kinda new to it all so not as neat as it could be, i think most of the code describes itself, please help me solve this since it is for a school project and it needs to work for like 10 days straight of datalogging. thanks in advance
