Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Library Imports
- #include <SD.h>
- #include <SPI.h>
- #include <DS3231.h>
- #include <LiquidCrystal.h>
- //Creates a liquid crystal object. Parameters: (rs, enable, d4, d5, d6, d7)
- LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
- //Define rtc as a DS3231 module variable
- DS3231 rtc(SDA, SCL);
- //Defining myFile as an external file variable
- File myFile;
- //Defining pin numbers
- const int pinCS = 10;
- const int trigPin = 9;
- const int echoPin = 8;
- const int ledPin = 3;
- // defines variables
- byte brightness;
- long duration;
- int distance;
- Time t;
- void setup() {
- //Setting up the realtime clock
- rtc.begin();
- //This part must be commented or omitted after running the Arduino for the first time
- //to avoid resetting the RTC module time and date when disconnecting the Arduino from
- //the computer and connecting it again
- rtc.setDOW(FRIDAY); //Day of Week
- rtc.setTime(21, 35, 00); //Time, in 24 hour format
- rtc.setDate(8, 12, 2017); // Date, in DD MM YYYY format
- //Setting up the sonar sensor
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- //LED pin set as output
- pinMode(ledPin, OUTPUT);
- //SD Card Initialization, with setting of pinCS as an output for SD Card
- pinMode(pinCS, OUTPUT);
- SD.begin();
- //Initializes the interface to the LCD screen,
- //and specifies the dimensions (width and height) of the display
- lcd.begin(16, 2);
- }
- //A Function used for counting the height of the plant
- int distCount() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- int duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- int dist = (28 - (duration * 0.034 / 2)) - 3; //The number 3 is a corrective constant to make the sensor reads correctly
- return dist;
- }
- //A function that write the distance and date of the measurement to an external file
- void checkAndWrite(int distance) {
- // Create/Open file
- myFile = SD.open("growth.txt", FILE_WRITE);
- // if the file opened okay, write to it:
- if (myFile) {
- // Write to file
- myFile.print(rtc.getDateStr()); myFile.print(","); myFile.println(distance);
- myFile.close(); // close the file
- }
- }
- //A function that checks if the time is correspondent to the parameters for checking and writing the distance to an external file
- void chkT() {
- if ((t.hour == 3 && t.min == 0 && t.sec == 10) ||
- (t.hour == 9 && t.min == 0 && t.sec == 10) ||
- (t.hour == 15 && t.min == 0 && t.sec == 10) ||
- (t.hour == 21 && t.min == 0 && t.sec == 10)) {
- for (int i = 0; i < 10; i++) {
- distance = distCount();
- checkAndWrite(distance); //To call the function that not only checks the distance, but also writes it to growth.txt
- delay(1000);
- }
- }
- }
- //A function that activates the sonar and count the distance, also for the sake of time checking
- void distAndTime() {
- distance = distCount(); //Activates the sonar and count the distance
- t = rtc.getTime(); //Get the current time, so that the Arduino always keeps track on the time
- }
- //A function to let the time and distance be printed into the LCD Module
- void printToLCD(int dist){
- lcd.setCursor(0, 0);
- lcd.clear();
- delay(500);
- lcd.print(rtc.getTimeStr());
- lcd.setCursor(0, 1);
- lcd.print("h:"); lcd.print(dist);
- delay(500);
- }
- void loop() {
- t = rtc.getTime(); //To initiate the first time check so that the Arduino can determine what time it is
- //Whiles are used for setting a limit on when the lights should be turned on (6-18) and off (18-6)
- while (t.hour >= 6 && t.hour < 18) {
- distAndTime();
- brightness = byte(distance * 12);
- // set the brightness of the LED:
- analogWrite(ledPin, brightness);
- chkT();
- printToLCD(distance);
- }
- while (t.hour >= 18 || t.hour < 6) {
- distAndTime();
- brightness = 0;
- // set the brightness of the LED:
- analogWrite(ledPin, brightness);
- chkT();
- printToLCD(distance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement