Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. [code]//The libraries we need for our terrarium kit
  2. //Download and install them all at this link:  https://drive.google.com/drive/folders/10XAFdYmnyz0dg9I6YYj80vJlHebFIZhN
  3. #include <OneWire.h> //Import OneWire library
  4. #include <DallasTemperature.h> //Import DallasTemperature library
  5. #include <Adafruit_Sensor.h> //Import Adafruit Sensor library
  6. #include <DHT.h> //Import first part of the DHT library
  7. #include <TimerOne.h> //Import TimerOne library
  8. #include <SPI.h>
  9. #include <SD.h>  //this refers to an old SD library needed specifically for
  10. #include "RTClib.h"  //this is from the rtc example folder code
  11.  
  12.  
  13. //SD Card & Realtime Clock: Initialization
  14. File logfile;
  15. RTC_DS1307 RTC;
  16.  
  17. //Humidity Sensor: Constants
  18. //This #define defines the pin that the DHT is plugged into
  19. #define DHT_PIN 2
  20. //This #define defines the type of the DHT
  21. #define DHT_TYPE DHT22
  22. //This tells the code that a DHT is plugged into port DHT_PIN and the DHT is of type DHT_TYPE
  23. DHT dht(DHT_PIN, DHT_TYPE);
  24.  
  25. //Moisture Sensor: Variables
  26. int MS_sigPin = A5; //Set the signal pin as analog pin 5
  27. int MS_digPin = 7; //Set the digital pin as 2
  28. float MS_upper = 0; //Declare highest value variable
  29.  
  30. //Other variables
  31. int measurementNumber = 1; //Number of measurements taken
  32.  
  33. //Filename:
  34.  DateTime now = RTC.now(); //Initialize the RTC
  35. //  String seconds = (String)(now.second());  //Stores the seconds from the RTC upon setup as a string
  36. const String minutes = (String)(now.minute()); //Stores the seconds from the RTC upon setup as a string
  37. const String hours = (String)(now.hour());  //Stores the seconds from the RTC upon setup as a string
  38. const String days = (String)(now.day());  //Stores the seconds from the RTC upon setup as a string
  39. const String months = (String)(now.month());  //Stores the seconds from the RTC upon setup as a string
  40. const String years = (String)(now.year());  //Stores the seconds from the RTC upon setup as a string
  41. const String filename = "D_" + minutes + hours + days + months + years; //Create the file name as a timestamp
  42.  
  43.  
  44. void setup() {
  45.  
  46.  int chipSelect = 10; //chipSelect pin for the SD card
  47.  
  48.  // put your setup code here, to run once:
  49.  Serial.begin(115200); //Set the baud rate at 56700
  50. //If RTC is already updated, the below is commented out
  51.  RTC.begin();
  52.  RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));//this is to force rtc update
  53. //  if (!RTC.isrunning()) {
  54. //    
  55. //    Serial.println("RTC is NOT running! Starting it now.");
  56. //    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  57. //  
  58. //  }// end rtc if
  59.  
  60. //{
  61. //The below is copied from wired, contains a second try, hoping for sd success herein.
  62. pinMode(chipSelect, OUTPUT);
  63. digitalWrite(chipSelect, HIGH);
  64. SD.begin(chipSelect);
  65. //  if (!SD.begin(10, 11, 12, 13)) {
  66. //    Serial.println("Card failed, or not present");
  67. //    delay(50);
  68. //    Serial.println("Trying again...");
  69. //    if (!SD.begin(50, 51, 52, 53)) {
  70. //      Serial.println("Card failed, or not present");
  71. //    }
  72. //  }
  73.  
  74.  
  75. /******************************** CSV FORMAT ******************************/
  76.  
  77. //  Serial.print("Current Position: ");
  78. //  Serial.println(logfile.position());
  79.  if(logfile.position() == 0)//Below print header line for datafile
  80.  logfile.println("Unix Time, Date, Time, Light, Humidity, Temperature");
  81.  //logfile.println("uni,date,time,Temperature");
  82.  
  83.  /**************************************************************************/
  84.  
  85. //  sensors.begin();
  86. //  sensors.setResolution(12);
  87.  
  88.  //Humidity Sensor:
  89.   dht.begin();
  90.    
  91.  //Moisture Sensor:
  92.  pinMode(MS_sigPin, OUTPUT); //Set the signal pin as an output
  93.  pinMode(MS_digPin, OUTPUT); //Set the digital pin as an output
  94.  digitalWrite(MS_digPin, HIGH); //Turn the digital pin on
  95.  
  96.  Serial.println("DIP THE MOISTURE SENSOR IN A CUP OF WATER MAKING SURE NOT TO SUBMERGE THE TOP OF IT INTO THE WATER, ENTER IN ANY KEY WHEN YOU ARE READY TO CALIBRATE"); //Print out useful information
  97.  while (Serial.available() == 0) //Hang the program until the user inputs a key to continue
  98.  {
  99.  
  100.  }
  101.  Serial.println("Starting Calibration"); //Print out useful information
  102.  Timer1.initialize(1000000); //Initialize an interrupt every 1 second
  103.  Timer1.attachInterrupt(Calibrate); //When the interrupt is called run the “Calibrate” function
  104.  
  105. }
  106. //Calibrate the moisture sensor
  107. void Calibrate() //Function to calibrate the sensor
  108. {
  109. if (analogRead(MS_sigPin) > MS_upper) //If the value read by the sensor is higher than the past highest value
  110. {
  111. MS_upper = analogRead(MS_sigPin); //Set the current value as the new highest value
  112.  }
  113. }
  114.  
  115. void loop() {
  116.  
  117. //Photocell: Variables
  118. int photocellPin = A0; // the cell and 10K pulldown are connected to a0
  119.  
  120.  char fileCharStorage[(filename.length()+5)]; //+4 Characters to include ".csv"
  121.  filename.toCharArray(fileCharStorage, filename.length()+5);
  122.  fileCharStorage[filename.length()] = 'V'; //Set the file name
  123.  fileCharStorage[filename.length()-1] = 'S'; //Set the file name
  124.  fileCharStorage[filename.length()-2] = 'C'; //Set the file name
  125.  fileCharStorage[filename.length()-3] = '.'; //Set the file name
  126.  Serial.println(filename); //Print out the filename
  127.  Serial.println(sizeof(fileCharStorage)); //Print the length of the array storing the file name
  128.  logfile = SD.open(fileCharStorage, FILE_WRITE);  //Creates (or opens) a file names "MMHHDDMMYYYY.CSV"
  129.  
  130.  if (logfile)
  131.  {
  132.    Serial.println("The SD Card is functioning properly");
  133.  }
  134.  else
  135.  {
  136.    Serial.println("A Critical Error occured while trying to initialize the SD Card!");
  137.  }
  138.  //Realtime Clock
  139.  DateTime now = RTC.now();
  140.  
  141. //Photocell:
  142. int photocellReading = analogRead(photocellPin); //Variable that stores the amount of light detected by the photocell as an integer
  143.  
  144.  // This code gets the humidity from the DHT
  145.  float dhtHumidity = dht.readHumidity();
  146.  /*
  147.  This code gets the temperature from the DHT
  148.  */
  149.  float dhtTemperature = dht.readTemperature();
  150.  
  151.  Serial.println(""); //Text Formatting
  152.  Serial.println("Starting New Measurement (" + (String)measurementNumber + ")");
  153.  Serial.println(""); //Text Formatting
  154.  delay(1000);
  155.  
  156.  //Print out data from the number of measurements taken
  157.  Serial.println("The Arduino has been active for " + (String)((measurementNumber*10)-10) + " seconds");
  158.  measurementNumber++;
  159.  
  160.  //Print out data from the RTC
  161.    Serial.print("Unix Time (Seconds since 1/1/1970): ");
  162.    Serial.print(now.unixtime()); // seconds since 1/1/1970
  163.    Serial.println(", ");
  164.    Serial.print("RTC time: ");
  165.    Serial.print(now.month(), DEC);
  166.    Serial.print("/");
  167.    Serial.print(now.day(), DEC);
  168.    Serial.print("/");
  169.    Serial.print(now.year(), DEC);
  170.    Serial.print(",");
  171.    Serial.print(now.hour(), DEC);
  172.    Serial.print(":");
  173.    Serial.print(now.minute(), DEC);
  174.    Serial.print(":");
  175.    Serial.print(now.second(), DEC);
  176.    Serial.println("");
  177.    Serial.println("");
  178.    
  179.  //Photocell:
  180.  Serial.println("Data from the photocell:");
  181.  Serial.print("Analog reading = ");
  182.  Serial.print(photocellReading); // the raw analog reading
  183.  
  184.  if (photocellReading < 10) {
  185.  Serial.println(" - Dark"); //says that if any number is lower than 10 then it prints out dark.
  186.  } else if (photocellReading < 200) {
  187.  Serial.println(" - Dim"); //says that if any number is lower than 200 then it prints out dim.
  188.  } else if (photocellReading < 500) {
  189.  Serial.println(" - Light"); //says that if any number is lower than 500 then it prints out light.
  190.  } else if (photocellReading < 800) {
  191.  Serial.println(" - Bright"); //says that if any number is lower than 800 then it prints out bright
  192.  } else {
  193.  Serial.println(" - Very bright"); //says that if any number higher than 800 then it prints out very bright
  194. }
  195.  Serial.println("");
  196.    
  197. // Humidity Sensor:
  198.  Serial.println("Data from the humidity sensor:");
  199.  Serial.print("Humidity: ");
  200.  Serial.print(dhtHumidity);
  201.  Serial.print(" %, Temp: ");
  202.  Serial.print(dhtTemperature);
  203.  Serial.println(" Celsius");
  204.  Serial.println("");
  205.    
  206.  
  207.  //Moisture Sensor:
  208.  Serial.println("Data from the moisture sensor:");
  209.  float percent = (analogRead(MS_sigPin)/MS_upper)*100; //Calculate moisture percentage
  210.  Serial.println("Moisture = " + (String)percent + "%"); //Print out the moisture percentage
  211.  
  212.  Serial.println("");
  213.  Serial.println("--------------------------------------------------");
  214.  
  215.    //Log everything onto the SD card
  216.    logfile.print(now.unixtime()); // seconds since 1/1/1970
  217.    logfile.print(", ");
  218.    logfile.print(now.month(), DEC);
  219.    logfile.print("/");
  220.    logfile.print(now.day(), DEC);
  221.    logfile.print("/");
  222.    logfile.print(now.year(), DEC);
  223.    logfile.print(",");
  224.    logfile.print(now.hour(), DEC);
  225.    logfile.print(":");
  226.    logfile.print(now.minute(), DEC);
  227.    logfile.print(":");
  228.    logfile.print(now.second(), DEC);
  229.    logfile.print(", ");    
  230.    logfile.print(photocellReading);
  231.    logfile.print(", ");    
  232.    logfile.print(dhtHumidity);
  233.    logfile.print(", ");    
  234.    logfile.print(dhtTemperature);
  235.    logfile.println();    
  236.    logfile.close();
  237. //  Delay by 1+9 (10) seconds between measurements
  238.  delay(9000);
  239. }
  240. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement