#include #include #include "SSD1306Ascii.h" #include "SSD1306AsciiWire.h" #include #include BH1750 lightMeter; // 0X3C+SA0 - 0x3C or 0x3D #define I2C_ADDRESS 0x3C //light level to switch on if lower float lighThreshold = 2000.0f; int pwmPin = 3; // set output pin for the LED SSD1306AsciiWire Oled; DS3231 Clock; bool h12; bool PM; RTClib RTC; // create a Sunrise object Sunrise mySunrise(51.481845, 7.216236, 2);//Bochum example:Lisbon, Portugal, Europe - Latitude/Longitude and Timezone 38.79/-9.12, 0 void setup() { // Clock.setYear(21); // Clock.setMonth(3); // Clock.setDate(29); // Clock.setHour(8); // Clock.setMinute(44); // Clock.setSecond(30); mySunrise.Actual(); //Actual, Civil, Nautical, Astronomical // Start the serial port Serial.begin(9600); // Start the I2C interface Wire.begin(); Wire.setClock(400000L); Oled.begin(&Adafruit128x64, I2C_ADDRESS); lightMeter.begin(); pinMode(pwmPin, OUTPUT); } String addZeros(unsigned long t) { String retVal = ""; if (t == 0) { retVal = "00"; } else if (t < 10) { retVal = "0" + (String)t; } else { retVal = (String)t; } return retVal; } void loop() { Oled.setFont(Callibri14); Oled.clear(); float lux = lightMeter.readLightLevel(); DateTime now = RTC.now(); int riseMinutes;// t= minutes past midnight of sunrise (6 am would be 360) riseMinutes = mySunrise.Rise(now.month(), now.day()); // (month,day) - january=1 int setMinutes;// t= minutes past midnight of sunrise (6 am would be 360) setMinutes = mySunrise.Set(now.month(), now.day()); // (month,day) - january=1 long hour = Clock.getHour(h12, PM); long minute = Clock.getMinute(); long second = Clock.getSecond(); int currentTimeMinutes = (hour*60)+minute; if (currentTimeMinutes > riseMinutes && currentTimeMinutes < setMinutes) { if(lux<133) { analogWrite(pwmPin, (0)); Oled.println("light support ON"); } else { analogWrite(pwmPin, (255)); Oled.println("light support OFF"); } } else { analogWrite(pwmPin, (255)); Oled.println("night mode"); } String riseTime = String((riseMinutes/60))+":"+String((riseMinutes%60)); String setTime = String((setMinutes/60))+":"+String((setMinutes%60)); Oled.println("T: " + addZeros(hour) + ":" + addZeros(minute) + ":" + addZeros(second) +" "+ now.day() + "." + now.month() + "." + now.year()); Oled.println("Rise:" + riseTime + " - Set:" + setTime); Oled.println("Light: " + String(lux) + " lx"); delay(1000); }