Advertisement
Guest User

Erratic sampleNumber variable

a guest
Nov 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.49 KB | None | 0 0
  1. /**
  2.  * The circuit:
  3.  * Arduino UNO R3
  4.  * SPI SD card shield attached (pins 10, 11, 12, 13)
  5.  * - DS1307 RTC on SD card shield via I2C
  6.  * Prototype shield attached
  7.  * - LCD attached (pins 2, 3, 4, 5, 6, 7)
  8.  * - BMP180 sensor via I2C (pins A4, A5)
  9.  */
  10.  
  11. // include libraries
  12. #include "Wire.h"
  13. #include "RTClib.h"
  14. #include "SparkFunBME280.h"
  15. #include <SPI.h>
  16. #include <SD.h>
  17. #include <LiquidCrystal.h>
  18.  
  19. // DS1307 real time clock
  20. RTC_DS1307 rtc;
  21.  
  22. // SD card
  23. unsigned int cardPin = 10;
  24. File dataFile;
  25. char filename[16];
  26. char timestamp[16];
  27.  
  28. // LCD display
  29. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  30.  
  31. // degree C symbol data
  32. byte degreeCSymbol[8] = {
  33.   0b01000,
  34.   0b10100,
  35.   0b01000,
  36.   0b00011,
  37.   0b00100,
  38.   0b00100,
  39.   0b00011,
  40.   0b00000
  41. };
  42.  
  43. // degree F symbol data
  44. byte degreeFSymbol[8] = {
  45.   0b01000,
  46.   0b10100,
  47.   0b01000,
  48.   0b00011,
  49.   0b00100,
  50.   0b00111,
  51.   0b00100,
  52.   0b00000
  53. };
  54.  
  55. // BMP280 sensor object
  56. BME280 bmp280sensor;
  57.  
  58. // sensor data
  59. float tempC;
  60. float tempF;
  61. float pressPa;
  62. float presskPa;
  63. float altM;
  64. float altFt;
  65.  
  66. // CSV row counter
  67. unsigned int sampleNumber = 0;
  68.  
  69. //  function to reset arduino at address 0
  70. void (* resetFunc) (void) = 0;
  71.  
  72. // function to initialize serial connection
  73. void initSerial()
  74. {
  75.   Serial.begin(115200);
  76.   delay(2000);
  77.  
  78.   // program started
  79.   Serial.println(F("Program Started\n"));
  80. }
  81.  
  82. // function to initialize RTC on SD card shield
  83. void initRTC()
  84. {
  85.   Serial.print(F("Initializing RTC module..."));
  86.   if (!rtc.begin()) {
  87.     // no RTC found
  88.     Serial.println(F("RTC not found!"));
  89.     // tell the user
  90.     lcd.clear();
  91.     lcd.print(F("   CHECK RTC!   "));
  92.     delay(5000);
  93.     // reset the arduino
  94.     resetFunc();
  95.   }
  96.   // rtc was found but isn't running
  97.   if (!rtc.isrunning()) {
  98.     Serial.println(F("RTC found, but not running!"));
  99.     // set the RTC date/time to when sketch complied
  100.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  101.     Serial.println(F("RTC Date/Time adjusted to sketch compile time!"));
  102.   }
  103.   // uncomment to force RTC time update with each compile
  104.   // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  105.   Serial.println(F("RTC initalized! \n"));
  106.  
  107.  
  108. }
  109.  
  110. // function to initalize SD card shield
  111. void initSD()
  112. {
  113.   Serial.print(F("Initializing SD card..."));
  114.   // see if the card is present and can be initialized:
  115.   if (!SD.begin(cardPin)) {
  116.     // card failed
  117.     Serial.println(F("Card failed, or not present"));
  118.     // tell the user
  119.     lcd.clear();
  120.     lcd.print(F(" CHECK SD CARD! "));
  121.     delay(5000);
  122.     // reset the arduino
  123.     resetFunc();
  124.   }
  125.   Serial.println(F("card initialized! \n"));
  126. }
  127.  
  128. // function to initalize LCD
  129. void initLCD()
  130. {
  131.   Serial.print(F("Initializing LCD..."));
  132.   lcd.begin(16, 2);
  133.   Serial.println(F("16x2 display initialized!"));
  134.   // initalize custom degree c symbol
  135.   lcd.createChar(0, degreeCSymbol);
  136.   Serial.println(F("- degree C character created."));
  137.   // initalize custom degree f symbol
  138.   lcd.createChar(1, degreeFSymbol);
  139.   Serial.println(F("- degree F character created. \n"));
  140.   // clear LCD to begin
  141.   lcd.clear();
  142. }
  143.  
  144. // function to initalize BMP280 sensor
  145. void initBMP()
  146. {
  147.   // sensor settings
  148.   bmp280sensor.settings.commInterface = I2C_MODE;
  149.   bmp280sensor.settings.I2CAddress = 0x76; //0x77(default) or 0x76
  150.   bmp280sensor.settings.runMode = 3; //Normal mode
  151.   bmp280sensor.settings.tStandby = 0; // 0.5ms
  152.   bmp280sensor.settings.filter = 0; //filter off
  153.   bmp280sensor.settings.tempOverSample = 1; //oversampling 1
  154.   bmp280sensor.settings.pressOverSample = 1; // oversampling 1
  155.   bmp280sensor.settings.humidOverSample = 0; // skipped
  156.   // init sensor
  157.   Serial.print(F("Initalizing BME280... initialized at: 0x"));
  158.   // calling .begin() causes the settings to be loaded
  159.   delay(10); // make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
  160.   Serial.print(bmp280sensor.begin(), HEX);
  161.   Serial.println(F("! \n"));
  162. }
  163.  
  164. // setup the arduino
  165. void setup()
  166. {
  167.   // initialize serial connection
  168.   initSerial();
  169.  
  170.   // initalize on-board LED
  171.   pinMode(13, OUTPUT);
  172.  
  173.   // initialize real time clock
  174.   initRTC();
  175.  
  176.   // initialize SD card
  177.   initSD();
  178.  
  179.   // initialize lcd
  180.   initLCD();
  181.  
  182.   // initalize bmp280 sensor
  183.   initBMP();
  184.  
  185.   // outputting CSV to seial connection and mirroring to SD card and LCD
  186.   Serial.println(F("Generating CSV file on SD card and mirroring here: \n"));
  187.  
  188. }
  189.  
  190. // loop logic
  191. void loop()
  192. {
  193.  
  194.   // generate the filename
  195.   generateFilename();
  196.  
  197.   // open the file
  198.   dataFile = SD.open(filename, FILE_WRITE);
  199.  
  200.   // if the file is available, write to it:
  201.   if (dataFile)
  202.   {
  203.     // build CSV data
  204.     if (sampleNumber == 0)
  205.     {
  206.       // build a first-row of column headers
  207.       dataFile.print("Sample,");
  208.       dataFile.print("Timestamp,");
  209.       dataFile.print("T(deg C),");
  210.       dataFile.print("T(deg F),");
  211.       dataFile.print("P(Pa),");
  212.       dataFile.print("P(kPa),");
  213.       dataFile.print("Alt(m),");
  214.       dataFile.print("Alt(ft),");
  215.       dataFile.println("");
  216.      
  217.       Serial.print(F("Sample,"));
  218.       Serial.print(F("Timestamp,"));
  219.       Serial.print(F("T(deg C),"));
  220.       Serial.print(F("T(deg F),"));
  221.       Serial.print(F("P(Pa),"));
  222.       Serial.print(F("P(kPa),"));
  223.       Serial.print(F("Alt(m),"));
  224.       Serial.print(F("Alt(ft),"));
  225.       Serial.println(F(""));
  226.     }
  227.     else
  228.     {
  229.       // sample number
  230.       dataFile.print(sampleNumber);
  231.       dataFile.print(F(","));
  232.      
  233.       Serial.print(sampleNumber);
  234.       Serial.print(F(","));
  235.      
  236.       // timestamp
  237.       generateTimestamp();
  238.      
  239.       dataFile.print(timestamp);
  240.       dataFile.print(F(","));
  241.      
  242.       Serial.print(timestamp);
  243.       Serial.print(F(","));
  244.      
  245.       // read and display data
  246.       displayData(0, 0, 1, 6); // temp,   C,    1 of 6
  247.       displayData(0, 1, 2, 6); // temp,   F,    2 of 6
  248.       displayData(1, 0, 3, 6); // press,  Pa,   3 of 6
  249.       displayData(1, 1, 4, 6); // press,  kPa,  4 of 6
  250.       displayData(2, 0, 5, 6); // alt,    m,    5 of 6
  251.       displayData(2, 1, 6, 6); // alt,    ft,   6 of 6
  252.      
  253.       // print new line
  254.       dataFile.println("");
  255.      
  256.       Serial.println(F(""));
  257.     }
  258.    
  259.     // increase CSV counter
  260.     sampleNumber++;
  261.    
  262.     // close the file
  263.     dataFile.close();
  264.    
  265.   }
  266.   // if the file isn't open, pop up an error:
  267.   else
  268.   {
  269.     // couldn't open file!
  270.     Serial.println(F("ERROR: file can't be saved!"));
  271.     // tell the user
  272.     lcd.clear();
  273.     lcd.print(F(" CAN'T SAVE SD! "));
  274.     delay(5000);
  275.     // reset the arduino
  276.     resetFunc();
  277.   }
  278.  
  279. }
  280.  
  281. // generate a non-duplicate filename
  282. void generateFilename()
  283. {
  284.   // get the time
  285.   DateTime now = rtc.now();
  286.  
  287.   // format the filename with MMDDYYYY.CSV format
  288.   sprintf(filename,"%02u%02u%04u.CSV",now.month(),now.day(),now.year());
  289.  
  290.   // if this is a new loop and file already exists
  291.   if (sampleNumber == 0 && SD.exists(filename))
  292.   {
  293.     // counter
  294.     int n = 0;
  295.    
  296.     // check for next file on SD card
  297.     while(SD.exists(filename))
  298.     {
  299.       // increase counter
  300.       n++;
  301.      
  302.       // format filename with trailing number
  303.       sprintf(filename,"%02u%02u-%02u.CSV",now.month(),now.day(), n);
  304.     }
  305.     // display filename
  306.     Serial.print(F("Filename is: "));
  307.     Serial.println(filename);
  308.   }
  309.   // this is a new file and new loop, output filename
  310.   else if (sampleNumber == 0)
  311.   {
  312.     // display filename
  313.     Serial.print(F("Filename is: "));
  314.     Serial.println(filename);
  315.   }
  316. }
  317.  
  318. // generate timestamp for CSV
  319. void generateTimestamp()
  320. {
  321.   // create DateTime object from RTC.now()
  322.   DateTime now = rtc.now();
  323.  
  324.   // format the date MM-DD-YYYY HH:MM:SS
  325.   sprintf(timestamp,"%02u-%02u-%04u %02u:%02u:%02u",now.month(),now.day(),now.year(),now.hour(),now.minute(),now.second());
  326.  
  327. }
  328.  
  329. // read, format, and display requested measurments
  330. void displayData(unsigned int measurment, unsigned int unit, unsigned int posX, unsigned int posY)
  331. {
  332.   // measurment begins
  333.   digitalWrite(13, HIGH );
  334.  
  335.   // what data are we displaying?
  336.   switch (measurment)
  337.   {
  338.     // requesting Temperature
  339.     case 0:
  340.     if (unit == 0)
  341.     {
  342.       // requesting celsius
  343.       lcd.print(F("Temperature: "));
  344.       // update progress on LCD
  345.       lcd.setCursor(13, 0);
  346.       lcd.print(posX);
  347.       lcd.print(F("/"));
  348.       lcd.print(posY);
  349.       // read sensor
  350.       tempC = bmp280sensor.readTempC();
  351.       // move cursor to new line
  352.       lcd.setCursor(0, 1);
  353.       // print measurement to LCD
  354.       lcd.print(tempC,2);
  355.       // print measurement to SD
  356.       dataFile.print(tempC);
  357.       dataFile.print(",");
  358.       // print measurement to Serial
  359.       Serial.print(tempC);
  360.       Serial.print(F(","));
  361.       // print degree character
  362.       lcd.write((uint8_t)0);
  363.      
  364.     }
  365.     else if (unit == 1)
  366.     {
  367.       // requesting ferinheight
  368.       lcd.print(F("Temperature: "));
  369.       // update progress on LCD
  370.       lcd.setCursor(13, 0);
  371.       lcd.print(posX);
  372.       lcd.print(F("/"));
  373.       lcd.print(posY);
  374.       // read sensor
  375.       tempF = bmp280sensor.readTempF();
  376.       // move cursor to new line
  377.       lcd.setCursor(0, 1);
  378.       // print measurement to LCD
  379.       lcd.print(tempF,2);
  380.       // print measurement to SD
  381.       dataFile.print(tempF);
  382.       dataFile.print(",");
  383.       // print measurement to Serial
  384.       Serial.print(tempF);
  385.       Serial.print(F(","));
  386.       // print degree character
  387.       lcd.write((uint8_t)1);
  388.     }
  389.     break;
  390.    
  391.     // requesting Pressure
  392.     case 1:
  393.     if (unit == 0)
  394.     {
  395.       // requesting Pascals
  396.       lcd.print(F("Pressure: "));
  397.       // update progress on LCD
  398.       lcd.setCursor(13, 0);
  399.       lcd.print(posX);
  400.       lcd.print(F("/"));
  401.       lcd.print(posY);
  402.       // read sensor
  403.       pressPa = bmp280sensor.readFloatPressure();
  404.       // move cursor to new line
  405.       lcd.setCursor(0, 1);
  406.       // print measurement to LCD
  407.       lcd.print(pressPa,2);
  408.       // print measurement to SD
  409.       dataFile.print(pressPa);
  410.       dataFile.print(",");
  411.       // print measurement to Serial
  412.       Serial.print(pressPa);
  413.       Serial.print(F(","));
  414.       // print unit of measure
  415.       lcd.print(F(" Pa"));
  416.     }
  417.     else if (unit == 1)
  418.     {
  419.       // requesting Kilo-Pascals
  420.       lcd.print(F("Pressure: "));
  421.       // update progress on LCD
  422.       lcd.setCursor(13, 0);
  423.       lcd.print(posX);
  424.       lcd.print(F("/"));
  425.       lcd.print(posY);
  426.       // read sensor
  427.       presskPa = (bmp280sensor.readFloatPressure() / 1000);
  428.       // move cursor to new line
  429.       lcd.setCursor(0, 1);
  430.       // print measurement to LCD
  431.       lcd.print(presskPa,2);
  432.       // print measurement to SD
  433.       dataFile.print(presskPa);
  434.       dataFile.print(",");
  435.       // print measurement to Serial
  436.       Serial.print(presskPa);
  437.       Serial.print(F(","));
  438.       // print unit of measure
  439.       lcd.print(F(" kPa"));
  440.     }
  441.     break;
  442.    
  443.     // requesting Altitude
  444.     case 2:
  445.     if (unit == 0)
  446.     {
  447.       // requesting Meters
  448.       lcd.print(F("Altitude: "));
  449.       // update progress on LCD
  450.       lcd.setCursor(13, 0);
  451.       lcd.print(posX);
  452.       lcd.print(F("/"));
  453.       lcd.print(posY);
  454.       // read sensor
  455.       altM = bmp280sensor.readFloatAltitudeMeters();
  456.       // move cursor to new line
  457.       lcd.setCursor(0, 1);
  458.       // print measurement to LCD
  459.       lcd.print(altM,2);
  460.       // print measurement to SD
  461.       dataFile.print(altM);
  462.       dataFile.print(",");
  463.       // print measurement to Serial
  464.       Serial.print(altM);
  465.       Serial.print(F(","));
  466.       // print unit of measure
  467.       lcd.print(F(" m"));
  468.      
  469.     }
  470.     else if (unit == 1)
  471.     {
  472.       // requesting Feet
  473.       lcd.print(F("Altitude: "));
  474.       // update progress on LCD
  475.       lcd.setCursor(13, 0);
  476.       lcd.print(posX);
  477.       lcd.print(F("/"));
  478.       lcd.print(posY);
  479.       // read sensor
  480.       altFt = bmp280sensor.readFloatAltitudeFeet();
  481.       // move cursor to new line
  482.       lcd.setCursor(0, 1);
  483.       // print measurement to LCD
  484.       lcd.print(altFt,2);
  485.       // print measurement to SD
  486.       dataFile.print(altFt);
  487.       dataFile.print(",");
  488.       // print measurement to Serial
  489.       Serial.print(altFt);
  490.       Serial.print(F(","));
  491.       // print unit of measure
  492.       lcd.print(F(" ft"));
  493.     }
  494.     break;
  495.   }
  496.  
  497.   // measurment ends
  498.   digitalWrite(13, LOW );
  499.  
  500.   // delay for displaying on LCD
  501.   delay(5000);
  502.  
  503.   // clear LCD for next measurment
  504.   lcd.clear();
  505. }
  506.  
  507. /*
  508. SERIAL OUTPUT:
  509.  
  510. Program Started
  511.  
  512. Initializing RTC module...RTC initalized!
  513.  
  514. Initializing SD card...card initialized!
  515.  
  516. Initializing LCD...16x2 display initialized!
  517. - degree C character created.
  518. - degree F character created.
  519.  
  520. Initalizing BME280... initialized at: 0x58!
  521.  
  522. Generating CSV file on SD card and mirroring here:
  523.  
  524. Filename is: 1120-13.CSV
  525. Sample,Timestamp,T(deg C),T(deg F),P(Pa),P(kPa),Alt(m),Alt(ft),
  526. 1,11-20-2017 16:36:14,24.43,75.97,94446.63,94.45,607.64,1993.56,
  527. 12603,11-20-2017 16:36:44,24.43,75.97,94446.63,94.45,608.13,1998.42,
  528. 13371,11-20-2017 16:37:14,24.43,75.96,94453.24,94.45,608.27,1995.63,
  529. 12603,11-20-2017 16:37:45,24.43,75.97,94450.84,94.45,609.23,1996.36,
  530. 13371,11-20-2017 16:38:15,24.44,75.97,94445.41,94.45,608.49,1997.16,
  531. 12603,11-20-2017 16:38:45,24.42,75.99,94459.75,94.45,608.42,1996.94,
  532. 13371,11-20-2017 16:39:15,24.42,75.97,94450.84,94.46,608.49,1997.98,
  533. 12603,11-20-2017 16:39:45,24.42,75.99,94446.17,94.46,608.42,1996.94,
  534. 13371,11-20-2017 16:40:15,24.43,75.99,94451.60,94.45,608.42,1996.12,
  535.  
  536. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement