How do I combine these codes? (altimeter MS5607)

Hello
Me and a friend of mine are trying to measure height, pressure and temperature with the altimeter MS5607. We also want it to write the measured data to an SD card. So far, we managed to measure height and to write the measurements to the SD card. Now we have found out how to measure temperature and pressure, but we aren’t able to combine the two codes, so that the height, pressure and temperature are all written to the SD card. It would be really awesome if someone could help us, because we’re just a bunch of newbies. Thank you very much.

Here is the code that works (height + SD card)

#include <SD.h> // to use Adafruit SD breakout board
#include <Wire.h> // to use I2C
#include "IntersemaBaro.h" // to use Altimeter Module MS5607
#define BUTTON 7 // Button wordt uitgelezen aan poort 7
File dataFile; // declaration of dataFile

const int chipSelect = 10; //Arduino Nano uses CS = 10
unsigned long time = 0; // declaration of time variable
int val = LOW; // variabele waarde button, initieel low
//int old_val = LOW; // variabele waarde button na cyclus in void loop
//int bezig = HIGH; // variabele om na te gaan of button actief is
Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
  Serial.begin(9600); // setting the bit rate per second
  baro.init(); // initialising the Altimeter Module MS5607

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT); // needed for the SD card, making SPI connection
  pinMode (BUTTON,INPUT); // Button is input voor poort 7
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  
  // adding a header to the file data.txt
  dataFile = SD.open("data.txt", FILE_WRITE); // open the file data.txt
  dataFile.print("time (s)"); 
  dataFile.print("\t"); // add tab
  dataFile.print("height (cm)"); 
  dataFile.println(); // carriage return
  dataFile.close(); //close file data.txt
} 

void loop() {
  val = digitalRead (BUTTON); // lees waarde button
  if ((val == HIGH)) { // als button aanstaat en eerste cyclus van de void loop sinds het aanzetten nog niet voltooid is
  
  
  //bezig = digitalRead(BUTTON); //nagegaan of button nog steeds aanstaat
  time = millis();
  
  int alt = baro.getHeightCentiMeters(); //measure height in centimeter
   
  dataFile = SD.open("data.txt", FILE_WRITE); // open the file data.txt
  dataFile.print(time); // print time
  dataFile.print("\t"); // add tab
  dataFile.print((float)(alt)); // print altitude
  dataFile.println(); // add empty line
  dataFile.close(); // close file data.txt
  
    
  // print to the serial port too:
  Serial.print((time)); // print time
  Serial.print("\t"); // add tab
  Serial.print("height (cm): ");
  Serial.print((float)(alt)); // print altitude
  Serial.println(); // carriage return
  delay(1000);

  }
}

and here is the code for measuring the pressure and temperature, but we just don’t find out how to combine them.

#include <Wire.h>
#define ADDRESS 0x77 //0x76

uint32_t D1 = 0;
uint32_t D2 = 0;
int32_t dT = 0;
int32_t TEMP = 0;
int64_t OFF = 0; 
int64_t SENS = 0; 
int32_t P = 0;
uint16_t C[7];

void setup() {
// Disable internal pullups, 10Kohms are on the breakout
 PORTC |= (1 << 4);
 PORTC |= (1 << 5);

  Wire.begin();
  Serial.begin(9600); //9600 changed 'cos of timing?
  delay(100);
  initial(ADDRESS);

}

void loop()
{
  D1 = getVal(ADDRESS, 0x48); // Pressure raw
  D2 = getVal(ADDRESS, 0x58);// Temperature raw

  dT = D2 - (C[4]*256);
  TEMP = 2000 + ((dT * C[5])/8388608);
  OFF  = ((C[1])<<16) + ((C[3] * dT) >> 7);
  SENS = ((C[0])<<15) + ((C[2] * dT) >> 8);
  P = ((D1 * SENS/2097152 - OFF)/32768)/100;


  Serial.print("     Actual TEMP= ");
  Serial.print(TEMP);
  Serial.print("      Actual PRESSURE= ");
  Serial.print(P);

  Serial.println();  
  Serial.print(" RAW Temp D2=  ");
  Serial.print(D2);
  Serial.print(" RAW Pressure D1=  ");
  Serial.println(D1);
  Serial.println();

  Serial.print(" dT=  ");
  Serial.println(dT);
  Serial.println();
  Serial.print(" C0/1=  ");
  Serial.println(C[0]);
  Serial.print(" C1/2=  ");
  Serial.println(C[1]);
  Serial.print(" C2/3=  ");
  Serial.println(C[2]); 
  Serial.print(" C3/4=  ");
  Serial.println(C[3]); 
  Serial.print(" C4/5=  ");
  Serial.println(C[4]); 
  Serial.print(" C5/6=  ");
  Serial.println(C[5]); 
  Serial.print(" C6/7=  ");
  Serial.println(C[6]); 
  Serial.print(" C7/8=  ");
  Serial.println(C[7]);
  Serial.println();

  delay(1000

    );
}

long getVal(int address, byte code)
{
  unsigned long ret = 0;
  Wire.beginTransmission(address);
  Wire.write(code);
  Wire.endTransmission();
  delay(10);
  // start read sequence
  Wire.beginTransmission(address);
  Wire.write((byte) 0x00);
  Wire.endTransmission();
  Wire.beginTransmission(address);
  Wire.requestFrom(address, (int)3);
  if(Wire.available()) {
    //    ret = ((uint32_t)Wire.read() << 16) | ((uint32_t)Wire.read() << 8) | Wire.read();
    ret = Wire.read() * 65536 + Wire.read() * 256 + Wire.read();
  }
  else {
    ret = -1;
  }
  Wire.endTransmission();
  return ret;
}

void initial(uint8_t address)
{

  Serial.println();
  Serial.println("PROM COEFFICIENTS ivan");

  Wire.beginTransmission(address);
  Wire.write(0x1E); // reset
  Wire.endTransmission();
  delay(10);


  for (int i=0; i<7  ; i++) {

    Wire.beginTransmission(address);
    Wire.write(0xA2 + (i * 2));
    Wire.endTransmission();

    Wire.beginTransmission(address);
    Wire.requestFrom(address, (uint8_t) 6);
    delay(1);
    if(Wire.available()) {

      C[i] = Wire.read() *256 + Wire.read();
    }
    else {
      Serial.println("Error reading PROM 1"); // error reading the PROM or communicating with the device
    }
    Serial.println(C[i]);
  }
  Serial.println();
}

combine the stuff that goes before setup. combine the stuff in setup combine the stuff in loop keep any other functions. resolve any duplicate use of pins or variable names. make sure the flow in loop does what youwant.

// first pass at combining codes, no cleanup done outside of duplicate Serial.begin & Wire.h 
#include <SD.h> // to use Adafruit SD breakout board
#include <Wire.h> // to use I2C
#include "IntersemaBaro.h" // to use Altimeter Module MS5607
#define BUTTON 7 // Button wordt uitgelezen aan poort 7

// #include <Wire.h>
#define ADDRESS 0x77 //0x76

File dataFile; // declaration of dataFile

uint32_t D1 = 0;
uint32_t D2 = 0;
int32_t dT = 0;
int32_t TEMP = 0;
int64_t OFF = 0; 
int64_t SENS = 0; 
int32_t P = 0;
uint16_t C[7];
const int chipSelect = 10; //Arduino Nano uses CS = 10
unsigned long time = 0; // declaration of time variable
int val = LOW; // variabele waarde button, initieel low
//int old_val = LOW; // variabele waarde button na cyclus in void loop
//int bezig = HIGH; // variabele om na te gaan of button actief is
Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
  Serial.begin(9600); // setting the bit rate per second
  baro.init(); // initialising the Altimeter Module MS5607

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT); // needed for the SD card, making SPI connection
  pinMode (BUTTON,INPUT); // Button is input voor poort 7
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  
  // adding a header to the file data.txt
  dataFile = SD.open("data.txt", FILE_WRITE); // open the file data.txt
  dataFile.print("time (s)"); 
  dataFile.print("\t"); // add tab
  dataFile.print("height (cm)"); 
  dataFile.println(); // carriage return
  dataFile.close(); //close file data.txt

// Disable internal pullups, 10Kohms are on the breakout
 PORTC |= (1 << 4);
 PORTC |= (1 << 5);

  Wire.begin();
  // Serial.begin(9600); //9600 changed 'cos of timing?
  delay(100);
  initial(ADDRESS);
} 

void loop() {
  val = digitalRead (BUTTON); // lees waarde button
  if ((val == HIGH)) { // als button aanstaat en eerste cyclus van de void loop sinds het aanzetten nog niet voltooid is
  
  
  //bezig = digitalRead(BUTTON); //nagegaan of button nog steeds aanstaat
  time = millis();
  
  int alt = baro.getHeightCentiMeters(); //measure height in centimeter
   
  dataFile = SD.open("data.txt", FILE_WRITE); // open the file data.txt
  dataFile.print(time); // print time
  dataFile.print("\t"); // add tab
  dataFile.print((float)(alt)); // print altitude
  dataFile.println(); // add empty line
  dataFile.close(); // close file data.txt
  
    
  // print to the serial port too:
  Serial.print((time)); // print time
  Serial.print("\t"); // add tab
  Serial.print("height (cm): ");
  Serial.print((float)(alt)); // print altitude
  Serial.println(); // carriage return
  delay(1000);

  }

  D1 = getVal(ADDRESS, 0x48); // Pressure raw
  D2 = getVal(ADDRESS, 0x58);// Temperature raw

  dT = D2 - (C[4]*256);
  TEMP = 2000 + ((dT * C[5])/8388608);
  OFF  = ((C[1])<<16) + ((C[3] * dT) >> 7);
  SENS = ((C[0])<<15) + ((C[2] * dT) >> 8);
  P = ((D1 * SENS/2097152 - OFF)/32768)/100;


  Serial.print("     Actual TEMP= ");
  Serial.print(TEMP);
  Serial.print("      Actual PRESSURE= ");
  Serial.print(P);

  Serial.println();  
  Serial.print(" RAW Temp D2=  ");
  Serial.print(D2);
  Serial.print(" RAW Pressure D1=  ");
  Serial.println(D1);
  Serial.println();

  Serial.print(" dT=  ");
  Serial.println(dT);
  Serial.println();
  Serial.print(" C0/1=  ");
  Serial.println(C[0]);
  Serial.print(" C1/2=  ");
  Serial.println(C[1]);
  Serial.print(" C2/3=  ");
  Serial.println(C[2]); 
  Serial.print(" C3/4=  ");
  Serial.println(C[3]); 
  Serial.print(" C4/5=  ");
  Serial.println(C[4]); 
  Serial.print(" C5/6=  ");
  Serial.println(C[5]); 
  Serial.print(" C6/7=  ");
  Serial.println(C[6]); 
  Serial.print(" C7/8=  ");
  Serial.println(C[7]);
  Serial.println();

  delay(1000

    );
}
long getVal(int address, byte code)
{
  unsigned long ret = 0;
  Wire.beginTransmission(address);
  Wire.write(code);
  Wire.endTransmission();
  delay(10);
  // start read sequence
  Wire.beginTransmission(address);
  Wire.write((byte) 0x00);
  Wire.endTransmission();
  Wire.beginTransmission(address);
  Wire.requestFrom(address, (int)3);
  if(Wire.available()) {
    //    ret = ((uint32_t)Wire.read() << 16) | ((uint32_t)Wire.read() << 8) | Wire.read();
    ret = Wire.read() * 65536 + Wire.read() * 256 + Wire.read();
  }
  else {
    ret = -1;
  }
  Wire.endTransmission();
  return ret;
}

void initial(uint8_t address)
{

  Serial.println();
  Serial.println("PROM COEFFICIENTS ivan");

  Wire.beginTransmission(address);
  Wire.write(0x1E); // reset
  Wire.endTransmission();
  delay(10);


  for (int i=0; i<7  ; i++) {

    Wire.beginTransmission(address);
    Wire.write(0xA2 + (i * 2));
    Wire.endTransmission();

    Wire.beginTransmission(address);
    Wire.requestFrom(address, (uint8_t) 6);
    delay(1);
    if(Wire.available()) {

      C[i] = Wire.read() *256 + Wire.read();
    }
    else {
      Serial.println("Error reading PROM 1"); // error reading the PROM or communicating with the device
    }
    Serial.println(C[i]);
  }
  Serial.println();
}

Thank you very much, but somehow we don't see anything on the serial monitor anymore.

Add some more print statements, see where its stopping at, such as:

void setup() { Serial.begin(9600); // setting the bit rate per second

Serial.println("Initializing Altimeter Module...");

baro.init(); // initialising the Altimeter Module MS5607

Serial.print("Initializing SD card...");

Work your way thru the code, see where it gets hung or stops working or whatever.

There's something i forgot to add in my last reply. It doesn't say that there's an error, like it usually does, so I guess there aren't any mistakes in the code itself. Thank you very much for the help!

Yes, could be the barometer is not acknowledging the I2C request, so the code looks hung up. Maybe its just a problem of missing SCL/SDA pullup resistors.