Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The circuit:
- * Arduino UNO R3
- * SPI SD card shield attached (pins 10, 11, 12, 13)
- * - DS1307 RTC on SD card shield via I2C
- * Prototype shield attached
- * - LCD attached (pins 2, 3, 4, 5, 6, 7)
- * - BMP180 sensor via I2C (pins A4, A5)
- */
- // include libraries
- #include "Wire.h"
- #include "RTClib.h"
- #include "SparkFunBME280.h"
- #include <SPI.h>
- #include <SD.h>
- #include <LiquidCrystal.h>
- // DS1307 real time clock
- RTC_DS1307 rtc;
- // SD card
- unsigned int cardPin = 10;
- File dataFile;
- char filename[16];
- char timestamp[16];
- // LCD display
- LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
- // degree C symbol data
- byte degreeCSymbol[8] = {
- 0b01000,
- 0b10100,
- 0b01000,
- 0b00011,
- 0b00100,
- 0b00100,
- 0b00011,
- 0b00000
- };
- // degree F symbol data
- byte degreeFSymbol[8] = {
- 0b01000,
- 0b10100,
- 0b01000,
- 0b00011,
- 0b00100,
- 0b00111,
- 0b00100,
- 0b00000
- };
- // BMP280 sensor object
- BME280 bmp280sensor;
- // sensor data
- float tempC;
- float tempF;
- float pressPa;
- float presskPa;
- float altM;
- float altFt;
- // CSV row counter
- unsigned int sampleNumber = 0;
- // function to reset arduino at address 0
- void (* resetFunc) (void) = 0;
- // function to initialize serial connection
- void initSerial()
- {
- Serial.begin(115200);
- delay(2000);
- // program started
- Serial.println(F("Program Started\n"));
- }
- // function to initialize RTC on SD card shield
- void initRTC()
- {
- Serial.print(F("Initializing RTC module..."));
- if (!rtc.begin()) {
- // no RTC found
- Serial.println(F("RTC not found!"));
- // tell the user
- lcd.clear();
- lcd.print(F(" CHECK RTC! "));
- delay(5000);
- // reset the arduino
- resetFunc();
- }
- // rtc was found but isn't running
- if (!rtc.isrunning()) {
- Serial.println(F("RTC found, but not running!"));
- // set the RTC date/time to when sketch complied
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- Serial.println(F("RTC Date/Time adjusted to sketch compile time!"));
- }
- // uncomment to force RTC time update with each compile
- // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- Serial.println(F("RTC initalized! \n"));
- }
- // function to initalize SD card shield
- void initSD()
- {
- Serial.print(F("Initializing SD card..."));
- // see if the card is present and can be initialized:
- if (!SD.begin(cardPin)) {
- // card failed
- Serial.println(F("Card failed, or not present"));
- // tell the user
- lcd.clear();
- lcd.print(F(" CHECK SD CARD! "));
- delay(5000);
- // reset the arduino
- resetFunc();
- }
- Serial.println(F("card initialized! \n"));
- }
- // function to initalize LCD
- void initLCD()
- {
- Serial.print(F("Initializing LCD..."));
- lcd.begin(16, 2);
- Serial.println(F("16x2 display initialized!"));
- // initalize custom degree c symbol
- lcd.createChar(0, degreeCSymbol);
- Serial.println(F("- degree C character created."));
- // initalize custom degree f symbol
- lcd.createChar(1, degreeFSymbol);
- Serial.println(F("- degree F character created. \n"));
- // clear LCD to begin
- lcd.clear();
- }
- // function to initalize BMP280 sensor
- void initBMP()
- {
- // sensor settings
- bmp280sensor.settings.commInterface = I2C_MODE;
- bmp280sensor.settings.I2CAddress = 0x76; //0x77(default) or 0x76
- bmp280sensor.settings.runMode = 3; //Normal mode
- bmp280sensor.settings.tStandby = 0; // 0.5ms
- bmp280sensor.settings.filter = 0; //filter off
- bmp280sensor.settings.tempOverSample = 1; //oversampling 1
- bmp280sensor.settings.pressOverSample = 1; // oversampling 1
- bmp280sensor.settings.humidOverSample = 0; // skipped
- // init sensor
- Serial.print(F("Initalizing BME280... initialized at: 0x"));
- // calling .begin() causes the settings to be loaded
- delay(10); // make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
- Serial.print(bmp280sensor.begin(), HEX);
- Serial.println(F("! \n"));
- }
- // setup the arduino
- void setup()
- {
- // initialize serial connection
- initSerial();
- // initalize on-board LED
- pinMode(13, OUTPUT);
- // initialize real time clock
- initRTC();
- // initialize SD card
- initSD();
- // initialize lcd
- initLCD();
- // initalize bmp280 sensor
- initBMP();
- // outputting CSV to seial connection and mirroring to SD card and LCD
- Serial.println(F("Generating CSV file on SD card and mirroring here: \n"));
- }
- // loop logic
- void loop()
- {
- // generate the filename
- generateFilename();
- // open the file
- dataFile = SD.open(filename, FILE_WRITE);
- // if the file is available, write to it:
- if (dataFile)
- {
- // build CSV data
- if (sampleNumber == 0)
- {
- // build a first-row of column headers
- dataFile.print("Sample,");
- dataFile.print("Timestamp,");
- dataFile.print("T(deg C),");
- dataFile.print("T(deg F),");
- dataFile.print("P(Pa),");
- dataFile.print("P(kPa),");
- dataFile.print("Alt(m),");
- dataFile.print("Alt(ft),");
- dataFile.println("");
- Serial.print(F("Sample,"));
- Serial.print(F("Timestamp,"));
- Serial.print(F("T(deg C),"));
- Serial.print(F("T(deg F),"));
- Serial.print(F("P(Pa),"));
- Serial.print(F("P(kPa),"));
- Serial.print(F("Alt(m),"));
- Serial.print(F("Alt(ft),"));
- Serial.println(F(""));
- }
- else
- {
- // sample number
- dataFile.print(sampleNumber);
- dataFile.print(F(","));
- Serial.print(sampleNumber);
- Serial.print(F(","));
- // timestamp
- generateTimestamp();
- dataFile.print(timestamp);
- dataFile.print(F(","));
- Serial.print(timestamp);
- Serial.print(F(","));
- // read and display data
- displayData(0, 0, 1, 6); // temp, C, 1 of 6
- displayData(0, 1, 2, 6); // temp, F, 2 of 6
- displayData(1, 0, 3, 6); // press, Pa, 3 of 6
- displayData(1, 1, 4, 6); // press, kPa, 4 of 6
- displayData(2, 0, 5, 6); // alt, m, 5 of 6
- displayData(2, 1, 6, 6); // alt, ft, 6 of 6
- // print new line
- dataFile.println("");
- Serial.println(F(""));
- }
- // increase CSV counter
- sampleNumber++;
- // close the file
- dataFile.close();
- }
- // if the file isn't open, pop up an error:
- else
- {
- // couldn't open file!
- Serial.println(F("ERROR: file can't be saved!"));
- // tell the user
- lcd.clear();
- lcd.print(F(" CAN'T SAVE SD! "));
- delay(5000);
- // reset the arduino
- resetFunc();
- }
- }
- // generate a non-duplicate filename
- void generateFilename()
- {
- // get the time
- DateTime now = rtc.now();
- // format the filename with MMDDYYYY.CSV format
- sprintf(filename,"%02u%02u%04u.CSV",now.month(),now.day(),now.year());
- // if this is a new loop and file already exists
- if (sampleNumber == 0 && SD.exists(filename))
- {
- // counter
- int n = 0;
- // check for next file on SD card
- while(SD.exists(filename))
- {
- // increase counter
- n++;
- // format filename with trailing number
- sprintf(filename,"%02u%02u-%02u.CSV",now.month(),now.day(), n);
- }
- // display filename
- Serial.print(F("Filename is: "));
- Serial.println(filename);
- }
- // this is a new file and new loop, output filename
- else if (sampleNumber == 0)
- {
- // display filename
- Serial.print(F("Filename is: "));
- Serial.println(filename);
- }
- }
- // generate timestamp for CSV
- void generateTimestamp()
- {
- // create DateTime object from RTC.now()
- DateTime now = rtc.now();
- // format the date MM-DD-YYYY HH:MM:SS
- sprintf(timestamp,"%02u-%02u-%04u %02u:%02u:%02u",now.month(),now.day(),now.year(),now.hour(),now.minute(),now.second());
- }
- // read, format, and display requested measurments
- void displayData(unsigned int measurment, unsigned int unit, unsigned int posX, unsigned int posY)
- {
- // measurment begins
- digitalWrite(13, HIGH );
- // what data are we displaying?
- switch (measurment)
- {
- // requesting Temperature
- case 0:
- if (unit == 0)
- {
- // requesting celsius
- lcd.print(F("Temperature: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- tempC = bmp280sensor.readTempC();
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(tempC,2);
- // print measurement to SD
- dataFile.print(tempC);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(tempC);
- Serial.print(F(","));
- // print degree character
- lcd.write((uint8_t)0);
- }
- else if (unit == 1)
- {
- // requesting ferinheight
- lcd.print(F("Temperature: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- tempF = bmp280sensor.readTempF();
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(tempF,2);
- // print measurement to SD
- dataFile.print(tempF);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(tempF);
- Serial.print(F(","));
- // print degree character
- lcd.write((uint8_t)1);
- }
- break;
- // requesting Pressure
- case 1:
- if (unit == 0)
- {
- // requesting Pascals
- lcd.print(F("Pressure: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- pressPa = bmp280sensor.readFloatPressure();
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(pressPa,2);
- // print measurement to SD
- dataFile.print(pressPa);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(pressPa);
- Serial.print(F(","));
- // print unit of measure
- lcd.print(F(" Pa"));
- }
- else if (unit == 1)
- {
- // requesting Kilo-Pascals
- lcd.print(F("Pressure: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- presskPa = (bmp280sensor.readFloatPressure() / 1000);
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(presskPa,2);
- // print measurement to SD
- dataFile.print(presskPa);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(presskPa);
- Serial.print(F(","));
- // print unit of measure
- lcd.print(F(" kPa"));
- }
- break;
- // requesting Altitude
- case 2:
- if (unit == 0)
- {
- // requesting Meters
- lcd.print(F("Altitude: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- altM = bmp280sensor.readFloatAltitudeMeters();
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(altM,2);
- // print measurement to SD
- dataFile.print(altM);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(altM);
- Serial.print(F(","));
- // print unit of measure
- lcd.print(F(" m"));
- }
- else if (unit == 1)
- {
- // requesting Feet
- lcd.print(F("Altitude: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print(F("/"));
- lcd.print(posY);
- // read sensor
- altFt = bmp280sensor.readFloatAltitudeFeet();
- // move cursor to new line
- lcd.setCursor(0, 1);
- // print measurement to LCD
- lcd.print(altFt,2);
- // print measurement to SD
- dataFile.print(altFt);
- dataFile.print(",");
- // print measurement to Serial
- Serial.print(altFt);
- Serial.print(F(","));
- // print unit of measure
- lcd.print(F(" ft"));
- }
- break;
- }
- // measurment ends
- digitalWrite(13, LOW );
- // delay for displaying on LCD
- delay(5000);
- // clear LCD for next measurment
- lcd.clear();
- }
- /*
- SERIAL OUTPUT:
- Program Started
- Initializing RTC module...RTC initalized!
- Initializing SD card...card initialized!
- Initializing LCD...16x2 display initialized!
- - degree C character created.
- - degree F character created.
- Initalizing BME280... initialized at: 0x58!
- Generating CSV file on SD card and mirroring here:
- Filename is: 1120-13.CSV
- Sample,Timestamp,T(deg C),T(deg F),P(Pa),P(kPa),Alt(m),Alt(ft),
- 1,11-20-2017 16:36:14,24.43,75.97,94446.63,94.45,607.64,1993.56,
- 12603,11-20-2017 16:36:44,24.43,75.97,94446.63,94.45,608.13,1998.42,
- 13371,11-20-2017 16:37:14,24.43,75.96,94453.24,94.45,608.27,1995.63,
- 12603,11-20-2017 16:37:45,24.43,75.97,94450.84,94.45,609.23,1996.36,
- 13371,11-20-2017 16:38:15,24.44,75.97,94445.41,94.45,608.49,1997.16,
- 12603,11-20-2017 16:38:45,24.42,75.99,94459.75,94.45,608.42,1996.94,
- 13371,11-20-2017 16:39:15,24.42,75.97,94450.84,94.46,608.49,1997.98,
- 12603,11-20-2017 16:39:45,24.42,75.99,94446.17,94.46,608.42,1996.94,
- 13371,11-20-2017 16:40:15,24.43,75.99,94451.60,94.45,608.42,1996.12,
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement