Advertisement
onil80

Untitled

Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "RTClib.h"
  3. #include <LiquidCrystal_I2C.h>
  4. #include <dht11.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. #define ONE_WIRE_BUS 2
  9. OneWire oneWire(ONE_WIRE_BUS);
  10. DallasTemperature sensors(&oneWire);
  11.  
  12. dht11 DHT;
  13. #define DHT11_PIN 4
  14.  
  15. RTC_DS3231 rtc;
  16. LiquidCrystal_I2C lcd(0x3F, 16, 2);
  17. int i = 0;
  18. int pwm;
  19.  
  20. int minfrom00;
  21. int sunrise;
  22. int sunset;
  23.  
  24. byte oraa;
  25. byte mina;
  26. byte orat;
  27. byte mint;
  28.  
  29. void setup() {
  30.  
  31. pinMode(6, OUTPUT); // uscita PWM per dimmer
  32. pinMode(13, OUTPUT); // uscita ON/OFF luce
  33. sensors.begin();
  34.  
  35. lcd.init();
  36.  
  37. delay(3000); // wait for console opening
  38.  
  39. if (! rtc.begin()) {
  40. Serial.println("Couldn't find RTC");
  41. while (1);
  42. }
  43.  
  44. if ( rtc.lostPower()) {
  45. Serial.println("RTC is NOT running!");
  46. // following line sets the RTC to the date & time this sketch was compiled
  47. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  48. // This line sets the RTC with an explicit date & time, for example to set
  49. // January 21, 2014 at 3am you would call:
  50. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  51. }
  52. rtc.adjust(DateTime(2017, 3, 25, 06, 23, 50)); // usata per settare l'orologio ad orari diversi e verificare il funzionamento del tutto
  53. }
  54.  
  55. void loop() {
  56. int chk;
  57. Serial.print("DHT11, \t");
  58. chk = DHT.read(DHT11_PIN);
  59. DateTime now = rtc.now();
  60. sensors.requestTemperatures();
  61.  
  62. minfrom00 = (now.hour() * 60) + now.minute(); //calcola il numero di minuti a partire dalla mezzanotte
  63.  
  64. sunrise = int(389 + (105 * cos(((((now.month() - 1) * 30.5) + now.day()) + 8) / 58.1)));
  65. sunset = int(1121 + (108 * sin(((((now.month() - 1) * 30.5) + now.day()) - 83) / 58.1)));
  66. oraa = (int)sunrise / 60; // ora alba
  67. mina = sunrise - oraa * 60; // minuti alba
  68. orat = (int)sunset / 60; // ora tramonto
  69. mint = sunset - orat * 60; // minuti tramonto
  70. lcd.backlight();
  71. lcd.print("Ora Alba ");
  72. lcd.print(oraa);
  73. lcd.print(":");
  74. lcd.print(mina);
  75. lcd.setCursor(0, 1);
  76. lcd.print("Ora Tram. ");
  77. lcd.print(orat);
  78. lcd.print(":");
  79. lcd.print(mint);
  80. delay(5000);
  81. lcd.setCursor(0, 0);
  82. lcd.clear();
  83. lcd.print("Ora Att ");
  84. lcd.print(now.hour(), DEC);
  85. lcd.print(":");
  86. lcd.print(now.minute(), DEC);
  87. lcd.setCursor(0, 1);
  88. lcd.print("lum. pwm ");
  89. lcd.print(pwm);
  90. delay(5000);
  91. lcd.setCursor(0, 0);
  92. lcd.clear();
  93. lcd.print("Temp. C. ");
  94. lcd.print(DHT.temperature, 1);
  95. lcd.setCursor(0, 1);
  96. lcd.print("Umid. ");
  97. lcd.print(DHT.humidity, 1);
  98. delay(5000);
  99. lcd.setCursor(0, 0);
  100. lcd.clear();
  101. lcd.print("Temp. H2O ");
  102. lcd.print (sensors.getTempCByIndex(0));
  103. lcd.print (" C");
  104. delay(5000);
  105. lcd.clear();
  106.  
  107.  
  108.  
  109. if (minfrom00 >= sunrise && (minfrom00 < sunrise + 60)) { // è ora dell'alba
  110. digitalWrite(13, HIGH); // accende la luce
  111. i = minfrom00 - sunrise;
  112. pwm = map(i, 0, 59, 0, 255);
  113. analogWrite(6, pwm); // dimming ALBA
  114. }
  115.  
  116.  
  117. if ((minfrom00 >= (sunrise + 60)) && (minfrom00 < sunset)) {
  118. pwm = 255;
  119. analogWrite(6, pwm); // dimmer al massimo
  120. }
  121.  
  122.  
  123. if (minfrom00 >= sunset && (minfrom00 < sunset + 60)) { // è ora del tramonto
  124. i = minfrom00 - sunset;
  125. pwm = map(i, 0, 59, -255, 0);
  126. pwm = abs(pwm);
  127. analogWrite(6, pwm); // dimming TRAMONTO
  128. }
  129.  
  130. //
  131. if (minfrom00 >= (sunset + 60)) {
  132. digitalWrite(13, LOW); // spegne la luce
  133. pwm = 0;
  134. analogWrite(6, pwm); // dimmer al minimo
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement