Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // before 1679
- // after 1331
- // include libraries
- #include "SparkFunBME280.h"
- #include "Wire.h"
- #include "RTClib.h"
- #include <SPI.h>
- #include <SD.h>
- #include <LiquidCrystal.h>
- // DS1307 Real-time Clock
- RTC_DS1307 rtc;
- // SD card
- const unsigned int cardPin = 10;
- String dataString;
- String fileName = "data.csv";
- // 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;
- // reset function at address 0
- void(* resetFunc) (void) = 0;
- // function to initalize SD card
- 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("card initialized. \n");
- }
- // function to initalize RTC
- void initRTC() {
- if (! rtc.begin()) {
- // no RTC found
- Serial.println(F("RTC not found!"));
- while(1);
- }
- // 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 initalized!"));
- }
- // funtion to initalize LCD
- void initLCD() {
- lcd.begin(16, 2);
- Serial.println(F("LCD 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() {
- // initalize BMP280 Driver with 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
- Serial.print(F("Starting BME280... result of .begin(): 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.println(bmp280sensor.begin(), HEX);
- Serial.print(F("\n"));
- }
- // setup the arduino
- void setup()
- {
- // initialize serial connection
- Serial.begin(57600);
- Serial.println(F("Program Started\n"));
- // initalize on-board LED
- pinMode(13, OUTPUT);
- // init sd
- initSD();
- // init RTC
- initRTC();
- // init lcd
- initLCD();
- // init sensor
- initBMP();
- // displaying remainder of program on LCD
- Serial.print(F("please allow 30 seconds for the first reading to appear."));
- Serial.print(F("\n\n"));
- }
- // loop logic
- void loop()
- {
- // create DateTime object from RTC.now()
- DateTime now = rtc.now();
- // build CSV data
- if (sampleNumber == 0) {
- // build a first-row of column headers
- dataString = "";
- dataString +="Sample,";
- dataString +="Timestamp,";
- dataString +="T(deg C),";
- dataString +="T(deg F),";
- dataString +="P(Pa),";
- dataString +="P(kPa),";
- dataString +="Alt(m),";
- dataString +="Alt(ft),";
- dataString +=("");
- }
- else {
- // collect and display data
- dataString = "";
- dataString += sampleNumber;
- dataString += ",";
- dataString += now.year();
- dataString += '/';
- dataString += now.month();
- dataString += '/';
- dataString += now.day();
- dataString += '-';
- dataString += now.hour();
- dataString += ':';
- dataString += now.minute();
- dataString += ':';
- dataString += now.second();
- dataString += ",";
- // read and display data
- displayData(0, " C", 1, 6);
- displayData(0, " F", 2, 6);
- displayData(1, " Pa", 3, 6);
- displayData(1, " kPa", 4, 6);
- displayData(2, " M", 5, 6);
- displayData(2, " Ft", 6, 6);
- }
- // save to SD card
- saveSD();
- // increase CSV counter
- sampleNumber++;
- }
- // read, format, display, requested measurments
- void displayData(unsigned int measurment, String 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 == " C") {
- // requesting celsius
- lcd.print(F("Temperature: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print("/");
- 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 Serial/CSV/SD
- dataString += tempC;
- dataString += ",";
- // print degree character
- lcd.write((uint8_t)0);
- }
- else if (unit == " F") {
- // requesting ferinheight
- lcd.print(F("Temperature: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print("/");
- 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 Serial/CSV/SD
- dataString += tempF;
- dataString += ",";
- // print degree character
- lcd.write((uint8_t)1);
- }
- break;
- // requesting Pressure
- case 1:
- if (unit == " Pa") {
- // requesting Pascals
- lcd.print(F("Pressure: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print("/");
- 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 Serial/CSV/SD
- dataString += pressPa;
- dataString += ",";
- // print unit of measure
- lcd.print(unit);
- }
- else if (unit == " kPa") {
- // requesting Kilo-Pascals
- lcd.print(F("Pressure: "));
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print("/");
- 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 Serial/CSV/SD
- dataString += presskPa;
- dataString += ",";
- // print unit of measure
- lcd.print(unit);
- }
- break;
- // requesting Altitude
- case 2:
- if (unit == " M") {
- // 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 Serial/CSV/SD
- dataString += altM;
- dataString += ",";
- // print unit of measure
- lcd.print(unit);
- }
- else if (unit == " Ft") {
- // requesting Feet
- lcd.print("Altitude: ");
- // update progress on LCD
- lcd.setCursor(13, 0);
- lcd.print(posX);
- lcd.print("/");
- 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 Serial/CSV/SD
- dataString += altFt;
- dataString += ",";
- // print unit of measure
- lcd.print(unit);
- }
- break;
- }
- // measurment ends
- digitalWrite(13, LOW );
- // delay for displaying on LCD
- delay(5000);
- // clear LCD for next measurment
- lcd.clear();
- }
- // save dataString to SD card
- void saveSD() {
- // open the file
- File dataFile = SD.open(fileName, FILE_WRITE);
- // if the file is available, write to it:
- if (dataFile) {
- // print datastring to file
- dataFile.println(dataString);
- // close the file
- dataFile.close();
- // print to the serial port too:
- Serial.println(dataString);
- }
- // if the file isn't open, pop up an error:
- else {
- // couldn't open file!
- Serial.print(F("error opening "));
- Serial.println(fileName);
- // tell the user
- lcd.clear();
- lcd.print(F(" CHECK SD CARD! "));
- delay(5000);
- // reset the arduino
- resetFunc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement