/* * Datalogger for Condensing Engine Project */ #include // uses SD libray files via Sketch/Include_library manager #include // uses SPI library files via Sketch/Include_library manager #define PCylIn_PIN A5 // Set the pressure transducer for the cylinder inlet to pin A0 #define PCylOut_PIN A1 // Set the pressure transducer for the cylinder outlet to pin A1 #define PCondOut_PIN A2 // Set the pressure transducer for the condenser outlet to pin A2 #define TCondIn_PIN A10 // Set the thermocouple for the condenser inlet water to pin A10 #define TCondOut_PIN A15 // Set the thermocouple for the condenser outlet water to pin A15 #define AREF 5.0 // set to AREF, typically board voltage like 3.3 or 5.0 #define ADC_RESOLUTION 1024 // set to ADC bit resolution, 10 bits is default which is 1024 values const int chipSelect = 4; // value to set SD card unsigned long TimeStamp; // set the time stamp value as an unsigned long variable String dataString = ""; // make a string for assembling the data to log float reading, voltage, temperature, pressure; // Set value types to floating numbers for these variables float CylinderInletPressure, CylinderOutletPressure, CondenserOutletPressure, CondenserTempIn, CondenserTempOut; // Set value types to floating numbers for these variables // Set up equations to be used in data reading float get_voltage(int raw_adc) { return raw_adc * (AREF / ADC_RESOLUTION); } float get_temperature(float voltage) { return (voltage - 1.25) / 0.005; } float get_pressure(float voltage) { return ((2 * voltage) - 1); } void setup() { // put your setup code here, to run once: // Open serial communications and wait for port to open: Serial.begin(115200); // Set up SD card while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("Initializing SD card..."); // 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."); // Write header information at start of data log. Open the file. note that only one file can be open at a time, // so you have to close this one before opening another. // Note, the datafile must already exist on the SD card. Creat a .txt file with desired name and type that into the quotations below to open. File dataFile = SD.open("datalog.txt", FILE_WRITE); // // if the file is available, write to it: if (dataFile) { // write header information to SD card dataFile.println("Date and Test Type"); // Type here what you would like to be printed at the top of the file to distinguish it from others dataFile.println("TimeStamp(ms), CylinderInletPressure, CylinderOutletPressure, CondenserOutletPressure, CondenserTempIn, CondenserTempOut"); // Print the data headings dataFile.close(); // print to the serial port too: // write header information to serial monitor Serial.println("TimeStamp(ms), CylinderInletPressure, CylinderOutletPressure, CondenserOutletPressure, CondenserTempIn, CondenserTempOut"); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } } void loop() { // put your main code here, to run repeatedly: // Call for current time stamp to print: TimeStamp = millis(); // read the five sensors and convert values to desire units: reading = analogRead(PCylIn_PIN); voltage = get_voltage(reading); CylinderInletPressure = get_pressure(voltage); reading = analogRead(PCylOut_PIN); voltage = get_voltage(reading); CylinderOutletPressure = get_pressure(voltage); reading = analogRead(PCondOut_PIN); voltage = get_voltage(reading); CondenserOutletPressure = get_pressure(voltage); reading = analogRead(TCondIn_PIN); voltage = get_voltage(reading); CondenserTempIn = get_temperature(voltage); reading = analogRead(TCondOut_PIN); voltage = get_voltage(reading); CondenserTempOut = get_temperature(voltage); // append time and sensor data to a data string: dataString = String(TimeStamp) + "," + String(CylinderInletPressure, 2) + "," + String(CylinderOutletPressure, 2) + "," + String(CondenserOutletPressure, 2) + "," + String(CondenserTempIn, 2) + "," + String(CondenserTempOut, 2); // write data to the SD card // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. File dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } delay(1000); // delay for 50ms before repeating data reading/writing }