Hi,
I am working on a way to log data from a pressure sensor onto a micro SD card but it is proving difficult. My problem is that after I run the sketch and open up the micro SD card the file is there but there is no data on the file.
I have been able to identify the micro SD card using the "Cardinfo" Arduino library. I have been able to log example data to it using the "Datalogger" Arduino library. And I have tried printing out the values that would be logged and the results are perfect proving that the sensor works. I am clueless as to what is going on and need help.
Thanks for your time.
I am using an Adafruit Micro SD card slot
Here is my code
#include <Wire.h>
#include <LPS.h>
#include <SPI.h>
#include <SD.h>
float altitude_difference = 0;
const int chipSelect = 10;
float TimeKeeper=0;
float alt_present = 0;
float startingPressure=0;
float Velocity = 0;
float Altitude=0;
float Altitude1 = 0;
int launch = 0;
int trigger=0;
float Apogee=1;
float Maxvel = 0;
LPS ps;
void setup()
{
Serial.begin(9600);
Wire.begin();
if (!ps.init())
{
Serial.println("Failed to autodetect pressure sensor!");
while (1);
}
pinMode(10, OUTPUT);
SD.begin(chipSelect);
ps.enableDefault();
float pressure = ps.readPressureMillibars();
float altitude = ps.pressureToAltitudeMeters(pressure);
Altitude1 = altitude;
}
void loop()
{
File flight1 = SD.open("Fli.txt", FILE_WRITE);
float pressure = ps.readPressureMillibars();
float altitude = ps.pressureToAltitudeMeters(pressure);
Altitude= altitude - Altitude1;
float alt_past = altitude;
float Velocity_past = Velocity;
{
delay(30);
float pressure = ps.readPressureMillibars();
float altitude = ps.pressureToAltitudeMeters(pressure);
float alt_present = altitude;
float altitude_difference= alt_present - alt_past;
Velocity = altitude_difference+ 0.05 /0.094;
float Velocity_past = Velocity;
delay(30);
}
if (flight1) {
flight1.println(Altitude);
if (launch <= 1) {
if(Altitude>1.5) { Serial.print("p: ");
flight1.print(pressure);
flight1.print(" mbar\ta: ");
flight1.print(altitude);
flight1.print(" m\tt: ");
// Serial.print( temp.temperature );
flight1.print(" deg C");
flight1.print(Altitude);
flight1.print(" ");
flight1.println(Velocity);
launch = 1;
}
}
pressure = ps.readPressureMillibars();
altitude = ps.pressureToAltitudeMeters(pressure);
alt_present = altitude;
altitude_difference= alt_present - alt_past;
Velocity = altitude_difference + 0.05/0.094;
float Velocity_present = Velocity;
if (Maxvel <= 0) {
if (Velocity_past > Velocity_present) {
float Max_velocity = Velocity;
flight1.println(Max_velocity);
Maxvel = 1;
}
}
delay(30);
//unsigned long currentTime = millis();
// if (currentTime - previousTime >= eventInterval) {
//previousTime = currentTime;
if (launch>=1) {
if (Apogee <= 1) {
if (Velocity < 1.5) {
launch = 2;
flight1.println ("Apogee");
//Serial.print (" ");
//Serial.print (Altitude);
}
}
}
if(launch>=2) {
if ( Velocity <= -1) {
flight1.print(" ");
flight1.print ("descent");
flight1.print(pressure);
flight1.print(" mbar\ta: ");
flight1.print(altitude);
flight1.print(" m\tt: ");
// Serial.print( temp.temperature );
flight1.print(" deg C");
flight1.print(Altitude);
flight1.print(" ");
flight1.println(Velocity);
Apogee = 2;
flight1.close();
}
}
}
}