Advertisement
KillianMel

Untitled

Sep 13th, 2020 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>                                                    // Include libraries
  2. #include <SD.h>                                                     // ,,
  3. #include <Wire.h>                                                   // ,,
  4. #include <TimeLib.h>                                                // ,,
  5. #include <DS1307RTC.h>                                              // ,,
  6. #include <LiquidCrystal_I2C.h>                                      // ,,
  7. #define aref_voltage 4.99                                           // Uncomment the used analog reference voltage
  8. //#define aref_voltage 1.111                                        // ,,
  9. //#define aref_voltage 3.35                                         // ,,
  10. LiquidCrystal_I2C lcd(0x27, 16, 2);                                 // Set up lcd display
  11. int tempPin = A0;                                                   // TMP36 middle pin
  12. int illPin = A1;                                                    // Photoresistor
  13. double illuminanceInput;                                            // Raw illuminance output variable
  14. int buttonPin = 9;                                                  // Toggle button (record/standby)
  15. int buttonState = LOW;                                              // Default low
  16. int tmpInput;                                                       // Raw TMP36 output variable
  17. double temp;                                                        // Calculated temperature variable
  18. double illuminance;                                                 // Calculated illuminance percentage
  19. unsigned long measureTime;                                          // Time of measuring variable (millis)
  20. unsigned long currentTime;                                          // Current time (millis)
  21. unsigned long deltaTime;                                            // Difference between measureTime and currentTime
  22. unsigned long measureInterval = 2000;                               // Logging interval (5 mins when actually logging data)
  23. File Data;                                                          // SD data file
  24.  
  25. void setup() {
  26.   Serial.begin(9600);                                               // Start serial comms
  27.   //analogReference(INTERNAL);                                      // Uncomment if using internal 1.111 v reference voltage
  28.   //analogReference(EXTERNAL);                                      // Uncomment if using external 3.35 v reference voltage
  29.   pinMode(buttonPin, INPUT);                                        // Set toggle button pin to input
  30.   lcd.init();                                                       // Start lcd
  31.   lcd.backlight();                                                  // ,,
  32.   lcd.clear();                                                      // Clear lcd
  33.   lcd.setCursor(0, 0);                                              // Set lcd cursor (column, row)
  34.   lcd.print("Initializing SD");                                    
  35.   delay(500);                                                       // Check whether SD card is present/working correctly
  36.   if (!SD.begin(10)) {
  37.     lcd.clear();
  38.     lcd.setCursor(0, 0);
  39.     lcd.print("Failed! Check SD");
  40.     lcd.setCursor(0, 1);
  41.     lcd.print("and reset");
  42.     while (1);                                                      // If not, wait for reset
  43.   }
  44.   lcd.clear();
  45.   lcd.setCursor(0, 0);
  46.   lcd.print("Done!");
  47.   delay(500);
  48.   lcd.clear();
  49.   lcd.setCursor(0, 0);
  50.   lcd.print("Press S to log");                                      // Print standby instructions
  51.   lcd.setCursor(0, 1);
  52.   lcd.print("temperature");
  53. }
  54.  
  55. void loop() {
  56.   standby:                                                          // Standby goto point
  57.   buttonState = digitalRead(buttonPin);                             // Check state of button
  58.   if (buttonState == LOW) {                                         // Return to start of standby loop if button was not pressed
  59.     goto standby;                                                   // ,,
  60.   } else {                                                          // Go to measure loop if button was pressed
  61.     delay(250);                                                     // Wait 250 ms to prevent bouncing
  62.     lcd.clear();                                                    // Clear lcd
  63.     Data = SD.open("data.txt", FILE_WRITE);
  64.     if (Data) {
  65.       goto measure;                                                 // Go to measure loop
  66.     } else {
  67.       lcd.clear();
  68.       lcd.setCursor(0, 0);
  69.       lcd.print("Failed! Check SD");
  70.       lcd.setCursor(0, 1);
  71.       lcd.print("and reset");
  72.       while (1);
  73.     }
  74.   }
  75.   measure:                                                          // Measure goto point
  76.   illuminanceInput = analogRead(illPin);                            // Read raw photoresistor value
  77.   illuminance = (double) (illuminanceInput / 1023 * 100);           // Calculate illuminance percentage
  78.   tmpInput = analogRead(tempPin);                                   // Read raw TMP36 value
  79.   temp = (double) ((tmpInput * aref_voltage / 1024 - 0.5) * 100);   // Calculate temperature in C
  80.   lcd.setCursor(0, 0);                                              // Set lcd cursor and print temperature, illuminance, and recording
  81.   lcd.print("T=");                                                  // ,,
  82.   lcd.setCursor(2, 0);                                              // ,,
  83.   lcd.print(temp);                                                  // ,,
  84.   lcd.setCursor(8, 0);                                              // ,,
  85.   lcd.print("I=");                                                  // ,,
  86.   lcd.setCursor(10, 0);                                             // ,,
  87.   lcd.print(illuminance);                                           // ,,
  88.   lcd.setCursor(14, 0);                                             // ,,
  89.   lcd.print("%");                                                   // ,,
  90.   lcd.setCursor(0, 1);                                              // ,,
  91.   lcd.print("Logging");                                             // ,,
  92.   tmElements_t tm;                                                  // No idea why this has to be here, but doesn't work without it
  93.   RTC.read(tm);                                                     // Read DS3231 RTC
  94.   Data.print(tm.Day);                                               // Print date, time, temperature, and illuminance in SD card file
  95.   Data.print("/");                                                  // ,,
  96.   Data.print(tm.Month);                                             // ,,
  97.   Data.print("/");                                                  // ,,
  98.   Data.print(tmYearToCalendar(tm.Year));                            // ,,
  99.   Data.print(",");                                                  // ,,
  100.   Data.print(tm.Hour);                                              // ,,
  101.   Data.print(":");                                                  // ,,
  102.   Data.print(tm.Minute);                                            // ,,
  103.   Data.print(":");                                                  // ,,
  104.   Data.print(tm.Second);                                            // ,,
  105.   Data.print(",");                                                  // ,,
  106.   Data.print(temp);                                                 // ,,
  107.   Data.print(",");                                                  // ,,
  108.   Data.print(illuminance);                                          // ,,
  109.   Data.println();                                                   // ,,
  110.   measureTime = millis();                                           // Store time of measuring
  111.   goto wait;                                                        // Go to wait loop after taking measurement
  112.  
  113.   wait:                                                             // Wait goto point
  114.   buttonState = digitalRead(buttonPin);                             // Check state of button
  115.   if (buttonState == LOW) {                                         // Check whether set logging interval has passed if button was not pressed
  116.     currentTime = millis();                                         // Store current time
  117.     deltaTime = (currentTime - measureTime);                        // Calculate time between last measurement and current time
  118.     if (deltaTime > measureInterval) {                              // Go to measure loop to take next measurement if set logging interval has passed
  119.       lcd.clear();                                                  // Clear lcd
  120.       goto measure;                                                 // Go to measure loop
  121.     } else {                                                        // Go back to start of wait loop if set logging interval has not passed
  122.       goto wait;                                                    // Go to wait loop
  123.     }
  124.   } else {                                                          // Go back to standby loop if button was pressed
  125.     delay(250);                                                     // Wait 250 ms to prevent bouncing
  126.     Data.close();
  127.     lcd.clear();                                                    // Clear lcd
  128.     lcd.setCursor(0, 0);                                            // Set lcd cursor
  129.     lcd.print("Press S to log");                                    // Print standby loop instructions
  130.     lcd.setCursor(0, 1);
  131.     lcd.print("temperature");
  132.     goto standby;                                                   // Go to standby loop
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement