Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h> // Include libraries
- #include <SD.h> // ,,
- #include <Wire.h> // ,,
- #include <TimeLib.h> // ,,
- #include <DS1307RTC.h> // ,,
- #include <LiquidCrystal_I2C.h> // ,,
- #define aref_voltage 4.99 // Uncomment the used analog reference voltage
- //#define aref_voltage 1.111 // ,,
- //#define aref_voltage 3.35 // ,,
- LiquidCrystal_I2C lcd(0x27, 16, 2); // Set up lcd display
- int tempPin = A0; // TMP36 middle pin
- int illPin = A1; // Photoresistor
- double illuminanceInput; // Raw illuminance output variable
- int buttonPin = 9; // Toggle button (record/standby)
- int buttonState = LOW; // Default low
- int tmpInput; // Raw TMP36 output variable
- double temp; // Calculated temperature variable
- double illuminance; // Calculated illuminance percentage
- unsigned long measureTime; // Time of measuring variable (millis)
- unsigned long currentTime; // Current time (millis)
- unsigned long deltaTime; // Difference between measureTime and currentTime
- unsigned long measureInterval = 2000; // Logging interval (5 mins when actually logging data)
- File Data; // SD data file
- void setup() {
- Serial.begin(9600); // Start serial comms
- //analogReference(INTERNAL); // Uncomment if using internal 1.111 v reference voltage
- //analogReference(EXTERNAL); // Uncomment if using external 3.35 v reference voltage
- pinMode(buttonPin, INPUT); // Set toggle button pin to input
- lcd.init(); // Start lcd
- lcd.backlight(); // ,,
- lcd.clear(); // Clear lcd
- lcd.setCursor(0, 0); // Set lcd cursor (column, row)
- lcd.print("Initializing SD");
- delay(500); // Check whether SD card is present/working correctly
- if (!SD.begin(10)) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Failed! Check SD");
- lcd.setCursor(0, 1);
- lcd.print("and reset");
- while (1); // If not, wait for reset
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Done!");
- delay(500);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Press S to log"); // Print standby instructions
- lcd.setCursor(0, 1);
- lcd.print("temperature");
- }
- void loop() {
- standby: // Standby goto point
- buttonState = digitalRead(buttonPin); // Check state of button
- if (buttonState == LOW) { // Return to start of standby loop if button was not pressed
- goto standby; // ,,
- } else { // Go to measure loop if button was pressed
- delay(250); // Wait 250 ms to prevent bouncing
- lcd.clear(); // Clear lcd
- Data = SD.open("data.txt", FILE_WRITE);
- if (Data) {
- goto measure; // Go to measure loop
- } else {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Failed! Check SD");
- lcd.setCursor(0, 1);
- lcd.print("and reset");
- while (1);
- }
- }
- measure: // Measure goto point
- illuminanceInput = analogRead(illPin); // Read raw photoresistor value
- illuminance = (double) (illuminanceInput / 1023 * 100); // Calculate illuminance percentage
- tmpInput = analogRead(tempPin); // Read raw TMP36 value
- temp = (double) ((tmpInput * aref_voltage / 1024 - 0.5) * 100); // Calculate temperature in C
- lcd.setCursor(0, 0); // Set lcd cursor and print temperature, illuminance, and recording
- lcd.print("T="); // ,,
- lcd.setCursor(2, 0); // ,,
- lcd.print(temp); // ,,
- lcd.setCursor(8, 0); // ,,
- lcd.print("I="); // ,,
- lcd.setCursor(10, 0); // ,,
- lcd.print(illuminance); // ,,
- lcd.setCursor(14, 0); // ,,
- lcd.print("%"); // ,,
- lcd.setCursor(0, 1); // ,,
- lcd.print("Logging"); // ,,
- tmElements_t tm; // No idea why this has to be here, but doesn't work without it
- RTC.read(tm); // Read DS3231 RTC
- Data.print(tm.Day); // Print date, time, temperature, and illuminance in SD card file
- Data.print("/"); // ,,
- Data.print(tm.Month); // ,,
- Data.print("/"); // ,,
- Data.print(tmYearToCalendar(tm.Year)); // ,,
- Data.print(","); // ,,
- Data.print(tm.Hour); // ,,
- Data.print(":"); // ,,
- Data.print(tm.Minute); // ,,
- Data.print(":"); // ,,
- Data.print(tm.Second); // ,,
- Data.print(","); // ,,
- Data.print(temp); // ,,
- Data.print(","); // ,,
- Data.print(illuminance); // ,,
- Data.println(); // ,,
- measureTime = millis(); // Store time of measuring
- goto wait; // Go to wait loop after taking measurement
- wait: // Wait goto point
- buttonState = digitalRead(buttonPin); // Check state of button
- if (buttonState == LOW) { // Check whether set logging interval has passed if button was not pressed
- currentTime = millis(); // Store current time
- deltaTime = (currentTime - measureTime); // Calculate time between last measurement and current time
- if (deltaTime > measureInterval) { // Go to measure loop to take next measurement if set logging interval has passed
- lcd.clear(); // Clear lcd
- goto measure; // Go to measure loop
- } else { // Go back to start of wait loop if set logging interval has not passed
- goto wait; // Go to wait loop
- }
- } else { // Go back to standby loop if button was pressed
- delay(250); // Wait 250 ms to prevent bouncing
- Data.close();
- lcd.clear(); // Clear lcd
- lcd.setCursor(0, 0); // Set lcd cursor
- lcd.print("Press S to log"); // Print standby loop instructions
- lcd.setCursor(0, 1);
- lcd.print("temperature");
- goto standby; // Go to standby loop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement