Just got a PM about this but I'll answer it here.
The code he/she send me after a heavy cleanup (ctrl+t...) because it was completely unreadable
#include <Bounce2.h>
#include <SPI.h>
#include <SD.h>
#define FILE_BASE_NAME "Data"
#include <Wire.h>
#include <RTClib.h>
#define DS3231_Address 0x68
#define HSCDDRD005PD3A3_I2C 0x38
#define OUTPUT_MIN 1638.4 // 1638 counts (10% of 2^14 counts or 0x0666)
#define OUTPUT_MAX 14745.6 // 14745 counts (90% of 2^14 counts or 0x3999)
#define PRESSURE_MIN -5
#define PRESSURE_MAX +5
unsigned long time; //declaring to display milliseconds
RTC_DS3231 rtc;
int chipSelect = 10;
File file;
int BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char filename[] = FILE_BASE_NAME "00.csv";
const byte LedPin = 9;
const byte ButtonPin = 7;
Bounce button;
bool logFlag = false;
void buttonCheck() {
button.update();
if (button.fell())
{
logFlag = !logFlag;
digitalWrite(9, logFlag);
}
}
void setup()
{
Wire.begin(); // wake up I2C bus
delay (500);
Serial.begin(9600);
Serial.println ("Sketch has started");
button.attach(7, INPUT_PULLUP);
pinMode ( 9, OUTPUT);
if (rtc.lostPower())
{
Serial.println("RTC lost power,set the time");
//rtc.adjust(DateTime(2016,4,20,02,16,00));
}
delay(10);
if (logFlag)
{
if (!SD.begin(10))
{
Serial.println(F("begin failed"));
return;
}
while (SD.exists(filename)) {
if (filename[BASE_NAME_SIZE + 1] != '9') {
filename[BASE_NAME_SIZE + 1]++;
}
else if (filename[BASE_NAME_SIZE] != '9') {
filename[BASE_NAME_SIZE + 1] = '0';
filename[BASE_NAME_SIZE]++;
}
else {
Serial.println(F("Can't create file name"));
return;
}
File file = SD.open(filename, FILE_WRITE);
DateTime now = rtc.now();
file.print(now.year(), DEC);
file.print('/');
file.print(now.month(), DEC);
file.print('/');
file.print(now.day(), DEC);
file.print(',');
file.print(now.hour(), DEC);
file.print(':');
file.print(now.minute(), DEC);
file.print(':');
file.print(now.second(), DEC);
file.println();
file.print("Time( ms)");
file.print(',');
file.print("Presuure (mmhg)");
file.println();
file.close();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(',');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
}
}
void loop()
{
float pressure, temperature;
float pressure_mmhg;
//send a request
Wire.beginTransmission(HSCDDRD005PD3A3_I2C);
Wire.write(1); // send a bit asking for register one, (as specified by the pdf)
Wire.endTransmission();
// now get the data from the sensor
delay (20);
Wire.requestFrom(HSCDDRD005PD3A3_I2C, 4);
byte a = Wire.read(); // first received byte stored here ....Example bytes one: 00011001 10000000
byte b = Wire.read(); // second received byte stored here ....Example bytes two: 11100111 00000000
byte c = Wire.read(); // third received byte stored here
byte d = Wire.read(); // fourth received byte stored here
byte status1 = (a & 0xc0) >> 6;
int bridge_data = ((a & 0x3f) << 8) + b;
int temperature_data = ((c << 8) + (d & 0xe0)) >> 5;
pressure = 1.0 * (bridge_data - OUTPUT_MIN) * (PRESSURE_MAX - PRESSURE_MIN) / (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN;
pressure_mmhg = 51.71484 * pressure;
temperature = (temperature_data * 0.0977) - 50;
time = millis();
//buttonCheck();
if (logFlag)
{
File file = SD.open(filename, FILE_WRITE);
if (file)
{
Serial.print(time);
Serial.print(',');
Serial.println(pressure_mmhg);
file.print(time);
file.print(',');
file.print(pressure_mmhg);
file.println();
file.close();
}
else
{
Serial.print("Button not pressed");
}
}
}
A few thing a spot right away:
button.attach(7, INPUT_PULLUP);
pinMode ( 9, OUTPUT);
You give the pins nice and readable names but after that you think, fuck it, let's never use them? You tent to do that everywhere...
//buttonCheck();
if (logFlag)
{
So why never do a button check?
I'm not going to rewrite the whole code so I'll just make the framework for you. What to read and write is up to you. But you have three states. Not writing to sd, start writing to sd (aka, make the file etc) and writing to sd. And maybe even a 4th state, stopping to write to SD (but I'll not implement it here) To make that simple I changed the logFlag into a logState.
void buttonCheck() {
button.update();
if (button.fell())
{
if(logState)
{
logState = 0; //stop logging
}
else
{
logState = 1; //let's start
}
digitalWrite(LedPin, logState);
}
}
void loop()
{
buttonCheck();
//we need to start logging
if(logState == 1){
//so open the SD card and make the file
sdLogStart();
//if that's succesfull
mode = 2;
}
else if(mode == 2)
{
//do the SD card logging
sdLog();
}
//don't log when mode == 0
}
Now you have to fill in the stuff that needs to be done in each state.