eniac86

light_sensitive_sun_support

May 16th, 2021 (edited)
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <DS3231.h>
  2. #include <Wire.h>
  3. #include "SSD1306Ascii.h"
  4. #include "SSD1306AsciiWire.h"
  5. #include <Sunrise.h>
  6. #include <BH1750.h>
  7. BH1750 lightMeter;
  8.  
  9. // 0X3C+SA0 - 0x3C or 0x3D
  10. #define I2C_ADDRESS 0x3C
  11.  
  12.  
  13. //light level to switch on if lower
  14. float lighThreshold = 2000.0f;
  15.  
  16. int pwmPin = 3;     // set output pin for the LED
  17. SSD1306AsciiWire Oled;
  18. DS3231 Clock;
  19. bool h12;
  20. bool PM;
  21. RTClib RTC;
  22. // create a Sunrise object
  23. Sunrise mySunrise(51.481845, 7.216236, 2);//Bochum                             example:Lisbon, Portugal, Europe - Latitude/Longitude and Timezone   38.79/-9.12, 0
  24.  
  25.  
  26. void setup() {
  27. //  Clock.setYear(21);
  28. //  Clock.setMonth(3);
  29. //  Clock.setDate(29);
  30. //  Clock.setHour(8);
  31. //  Clock.setMinute(44);
  32. //  Clock.setSecond(30);
  33.  
  34.  
  35.   mySunrise.Actual(); //Actual, Civil, Nautical, Astronomical
  36.  
  37.  
  38.   // Start the serial port
  39.   Serial.begin(9600);
  40.  
  41.   // Start the I2C interface
  42.   Wire.begin();
  43.  
  44.   Wire.setClock(400000L);
  45.   Oled.begin(&Adafruit128x64, I2C_ADDRESS);
  46.   lightMeter.begin();
  47.   pinMode(pwmPin, OUTPUT);
  48. }
  49.  
  50. String addZeros(unsigned long t)
  51. {
  52.   String retVal = "";
  53.   if (t == 0) {
  54.     retVal = "00";
  55.   }
  56.   else if (t < 10) {
  57.     retVal = "0" + (String)t;
  58.   }
  59.   else {
  60.     retVal = (String)t;
  61.   }
  62.   return retVal;
  63. }
  64.  
  65. void loop() {
  66.   Oled.setFont(Callibri14);
  67.   Oled.clear();
  68.   float lux = lightMeter.readLightLevel();
  69.  
  70.   DateTime now = RTC.now();  
  71.   int riseMinutes;// t= minutes past midnight of sunrise (6 am would be 360)
  72.   riseMinutes = mySunrise.Rise(now.month(), now.day()); // (month,day) - january=1
  73.   int setMinutes;// t= minutes past midnight of sunrise (6 am would be 360)
  74.   setMinutes = mySunrise.Set(now.month(), now.day()); // (month,day) - january=1
  75.  
  76.   long hour = Clock.getHour(h12, PM);
  77.   long minute = Clock.getMinute();
  78.   long second = Clock.getSecond();
  79.  
  80.   int currentTimeMinutes = (hour*60)+minute;
  81.  
  82.   if (currentTimeMinutes > riseMinutes && currentTimeMinutes < setMinutes)
  83.   {
  84.     if(lux<133)
  85.     {
  86.         analogWrite(pwmPin, (0));  
  87.         Oled.println("light support ON");
  88.     }
  89.     else
  90.     {
  91.       analogWrite(pwmPin, (255));
  92.       Oled.println("light support OFF");
  93.     }
  94.      
  95.   }
  96.   else
  97.   {
  98.     analogWrite(pwmPin, (255));
  99.     Oled.println("night mode");
  100.   }
  101.  
  102.   String riseTime = String((riseMinutes/60))+":"+String((riseMinutes%60));
  103.   String setTime = String((setMinutes/60))+":"+String((setMinutes%60));
  104.   Oled.println("T: " + addZeros(hour) + ":" + addZeros(minute) + ":" + addZeros(second) +" "+ now.day() + "." + now.month() + "." + now.year());
  105.   Oled.println("Rise:" + riseTime + " - Set:" + setTime);  
  106.   Oled.println("Light: " + String(lux) + " lx");
  107.   delay(1000);
  108. }
Add Comment
Please, Sign In to add comment