Advertisement
Guest User

Arduino RBL

a guest
Dec 17th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. //Library Imports
  2. #include <SD.h>
  3. #include <SPI.h>
  4. #include <DS3231.h>
  5. #include <LiquidCrystal.h>
  6.  
  7. //Creates a liquid crystal object. Parameters: (rs, enable, d4, d5, d6, d7)
  8. LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
  9.  
  10. //Define rtc as a DS3231 module variable
  11. DS3231 rtc(SDA, SCL);
  12.  
  13. //Defining myFile as an external file variable
  14. File myFile;
  15.  
  16. //Defining pin numbers
  17. const int pinCS = 10;
  18. const int trigPin = 9;
  19. const int echoPin = 8;
  20. const int ledPin = 3;
  21.  
  22. // defines variables
  23. byte brightness;
  24. long duration;
  25. int distance;
  26. Time t;
  27.  
  28. void setup() {
  29.   //Setting up the realtime clock
  30.   rtc.begin();
  31.  
  32.   //This part must be commented or omitted after running the Arduino for the first time
  33.   //to avoid resetting the RTC module time and date when disconnecting the Arduino from
  34.   //the computer and connecting it again
  35.   rtc.setDOW(FRIDAY); //Day of Week
  36.   rtc.setTime(21, 35, 00); //Time, in 24 hour format
  37.   rtc.setDate(8, 12, 2017); // Date, in DD MM YYYY format
  38.  
  39.   //Setting up the sonar sensor
  40.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  41.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  42.  
  43.   //LED pin set as output
  44.   pinMode(ledPin, OUTPUT);
  45.  
  46.   //SD Card Initialization, with setting of pinCS as an output for SD Card
  47.   pinMode(pinCS, OUTPUT);
  48.   SD.begin();
  49.  
  50.   //Initializes the interface to the LCD screen,
  51.   //and specifies the dimensions (width and height) of the display
  52.   lcd.begin(16, 2);
  53. }
  54.  
  55. //A Function used for counting the height of the plant
  56. int distCount() {
  57.   digitalWrite(trigPin, LOW);
  58.   delayMicroseconds(2);
  59.   // Sets the trigPin on HIGH state for 10 micro seconds
  60.   digitalWrite(trigPin, HIGH);
  61.   delayMicroseconds(10);
  62.   // Clears the trigPin
  63.   digitalWrite(trigPin, LOW);
  64.   // Reads the echoPin, returns the sound wave travel time in microseconds
  65.   int duration = pulseIn(echoPin, HIGH);
  66.   // Calculating the distance
  67.   int dist = (28 - (duration * 0.034 / 2)) - 3; //The number 3 is a corrective constant to make the sensor reads correctly
  68.   return dist;
  69. }
  70.  
  71. //A function that write the distance and date of the measurement to an external file
  72. void checkAndWrite(int distance) {
  73.   // Create/Open file
  74.   myFile = SD.open("growth.txt", FILE_WRITE);
  75.     // if the file opened okay, write to it:
  76.     if (myFile) {
  77.       // Write to file
  78.       myFile.print(rtc.getDateStr()); myFile.print(","); myFile.println(distance);
  79.       myFile.close(); // close the file
  80.     }
  81. }
  82.  
  83. //A function that checks if the time is correspondent to the parameters for checking and writing the distance to an external file
  84. void chkT() {
  85.   if  ((t.hour == 3 && t.min == 0 && t.sec == 10) ||
  86.        (t.hour == 9 && t.min == 0 && t.sec == 10) ||
  87.        (t.hour == 15 && t.min == 0 && t.sec == 10) ||
  88.        (t.hour == 21 && t.min == 0 && t.sec == 10)) {
  89.     for (int i = 0; i < 10; i++) {
  90.       distance = distCount();
  91.       checkAndWrite(distance); //To call the function that not only checks the distance, but also writes it to growth.txt
  92.       delay(1000);
  93.     }
  94.   }
  95. }
  96.  
  97. //A function that activates the sonar and count the distance, also for the sake of time checking
  98. void distAndTime() {
  99.   distance = distCount(); //Activates the sonar and count the distance
  100.   t = rtc.getTime(); //Get the current time, so that the Arduino always keeps track on the time
  101. }
  102.  
  103. //A function to let the time and distance be printed into the LCD Module
  104. void printToLCD(int dist){
  105.   lcd.setCursor(0, 0);
  106.   lcd.clear();
  107.   delay(500);
  108.   lcd.print(rtc.getTimeStr());
  109.   lcd.setCursor(0, 1);
  110.   lcd.print("h:"); lcd.print(dist);
  111.   delay(500);
  112. }
  113.  
  114. void loop() {
  115.   t = rtc.getTime(); //To initiate the first time check so that the Arduino can determine what time it is
  116.  
  117.   //Whiles are used for setting a limit on when the lights should be turned on (6-18) and off (18-6)
  118.   while  (t.hour >= 6 && t.hour < 18) {
  119.     distAndTime();
  120.     brightness = byte(distance * 12);
  121.     // set the brightness of the LED:
  122.     analogWrite(ledPin, brightness);
  123.     chkT();
  124.     printToLCD(distance);
  125.   }
  126.   while (t.hour >= 18 || t.hour < 6) {
  127.     distAndTime();
  128.     brightness = 0;
  129.     // set the brightness of the LED:
  130.     analogWrite(ledPin, brightness);
  131.     chkT();
  132.     printToLCD(distance);
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement