Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. // Date and time functions using a DS3231 RTC connected via I2C and Wire Lib
  2.  
  3. #include <Wire.h>
  4. #include <RTClib.h>
  5. #include <Time.h>
  6. #include <TimeAlarms.h>
  7. #include <OneWire.h>
  8. #include <DallasTemperature.h>
  9. #include <LiquidCrystal_I2C.h>
  10.  
  11.  
  12. #define BACKLIGHT_PIN 3
  13.  
  14. LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
  15.  
  16. RTC_DS1307 RTC;
  17.  
  18. #define ONE_WIRE_BUS 2
  19.  
  20. OneWire oneWire(ONE_WIRE_BUS);
  21. DallasTemperature sensors(&oneWire);
  22. int nastaw = 24; // ustaw temperature
  23. int przekaznik = 3; // pin przekaznika temp
  24. int lightRelay = 4; // pin przkeaznika led
  25. int Menu=0;
  26. int bstart=A1;
  27. int stanBstart=0;
  28.  
  29.  
  30. // Start Time
  31. int sHour = 12;
  32. int sMinute = 0;
  33. int sSecond = 0;
  34.  
  35. // End Time
  36. int eHour = 14;
  37. int eMinute = 0;
  38. int eSecond = 0;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. void setup() {
  45. // Set the relay to off immediately
  46. digitalWrite(lightRelay, HIGH);
  47.  
  48. pinMode(przekaznik,OUTPUT);
  49. pinMode(lightRelay, OUTPUT);
  50.  
  51. lcd.begin (16,2);
  52. lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  53. lcd.setBacklight(HIGH);
  54.  
  55. // Serial.print("Regulator Temp:");
  56. sensors.begin();
  57.  
  58.  
  59. Wire.begin();
  60. RTC.begin();
  61.  
  62.  
  63.  
  64. //Notify if the RTC isn't running
  65. if (! RTC.isrunning())
  66. {
  67. Serial.println("RTC is NOT running");
  68. }
  69.  
  70. // Get time from RTC
  71. DateTime current = RTC.now();
  72. DateTime compiled = DateTime(__DATE__, __TIME__);
  73. if (current.unixtime() < compiled.unixtime()) {
  74. Serial.println("RTC is older than compile time! Updating");
  75. RTC.adjust(DateTime(__DATE__, __TIME__));
  76. }
  77.  
  78. // Use RTC time to set the start time
  79. setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
  80. time_t s = now();
  81.  
  82. // Use RTC time to set the end time
  83. setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());
  84. time_t e = now();
  85.  
  86. // Use RTC time to set the current time
  87. setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
  88. time_t n = now();
  89.  
  90. // Test if grow light should be on
  91. if (s <= n && n <= e) {
  92. digitalWrite(lightRelay, LOW); // Sets the grow light "on"
  93. }
  94.  
  95. Alarm.alarmRepeat(sHour, sMinute, sSecond, LightOn);
  96. Alarm.alarmRepeat(eHour, eMinute, eSecond, LightOff);
  97.  
  98. }
  99.  
  100. void wys(void){
  101. sensors.requestTemperatures();
  102. //Serial.setCursor(0, 2);
  103. // lcd.print("T=");
  104. // lcd.print(sensors.getTempCByIndex(0));
  105. // lcd.print(" ");
  106. // lcd.print("N=");
  107. // lcd.print(nastaw);
  108. // lcd.print("Start");
  109. // lcd.print(sHour);
  110. // lcd.print(sMinute);
  111. // lcd.print(" ");
  112. // lcd.print("End");
  113. // lcd.print(eHour);
  114. // lcd.print(eMinute);
  115. // lcd.println();
  116.  
  117. if (sensors.getTempCByIndex(0) < (nastaw)) {
  118. digitalWrite(2,LOW);
  119. } else {
  120. digitalWrite(2,HIGH);
  121. }
  122. }
  123. void loop() {
  124. DateTime now = RTC.now();
  125. setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
  126. sensors.requestTemperatures();
  127. // lcd.print("Current time: ");
  128. // Serial.print(now.year(), DEC);
  129. // Serial.print('/');
  130. // Serial.print(now.month(), DEC);
  131. // Serial.print('/');
  132. // lcd.print(now.day(), DEC);
  133. // lcd.print(' ');
  134. // lcd.print(now.hour(), DEC);
  135. // lcd.print(':');
  136. // lcd.print(now.minute(), DEC);
  137. // lcd.print(':');
  138. // lcd.print(now.second(), DEC);
  139. // lcd.println();
  140. //Serial.setCursor(0, 2);
  141.  
  142. // lcd.print("S");
  143. //lcd.print(sHour);
  144. // lcd.print(":");
  145. // lcd.print(sMinute);
  146. // lcd.setCursor(8,0);
  147. // lcd.print("T=");
  148. // lcd.print(sensors.getTempCByIndex(0));
  149. // lcd.print(" ");
  150. // lcd.setCursor(0,2);
  151. // lcd.print("E");
  152. // lcd.print(eHour);
  153. // lcd.print(":");
  154. // lcd.print(eMinute);
  155. // lcd.setCursor(8,1);
  156. // lcd.print("N=");
  157. // lcd.print(nastaw);
  158. // lcd.setCursor(0,2);
  159.  
  160. if (sensors.getTempCByIndex(0) < (nastaw)) {
  161. digitalWrite(2,LOW);
  162. } else {
  163. digitalWrite(2,HIGH);
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Alarm.delay(1000);
  171. if (lightRelay, HIGH)
  172. {
  173. Serial.print("ON");
  174. }
  175. else {
  176. Serial.print("OFF");
  177. }
  178. Serial.print(' ');
  179. Serial.print(' ');
  180.  
  181.  
  182. stanBstart=analogRead(bstart);
  183.  
  184. //Serial.println(stanBstart);
  185.  
  186. // przyciski czasu start
  187. // Godzina
  188. if (stanBstart>680 && stanBstart<690) // +
  189. {
  190. Menu=Menu+1;
  191. }
  192.  
  193. if (stanBstart>160 && stanBstart<170) //-
  194. {
  195. Menu=Menu-1;
  196. }
  197.  
  198.  
  199. if(Menu<0)
  200. {
  201. Menu=5;
  202. }
  203.  
  204.  
  205. if (Menu==0)
  206. {
  207. lcd.clear();
  208. lcd.print("S");
  209. lcd.print(sHour);
  210. lcd.print(":");
  211. lcd.print(sMinute);
  212. lcd.setCursor(8,0);
  213. lcd.print("T=");
  214. lcd.print(sensors.getTempCByIndex(0));
  215. lcd.print(" ");
  216. lcd.setCursor(0,2);
  217. lcd.print("E");
  218. lcd.print(eHour);
  219. lcd.print(":");
  220. lcd.print(eMinute);
  221. lcd.setCursor(8,1);
  222. lcd.print("N=");
  223. lcd.print(nastaw);
  224. lcd.setCursor(0,2);
  225. }
  226.  
  227.  
  228.  
  229. if (Menu==1)
  230. {
  231. lcd.clear();
  232. lcd.setCursor(0,0);
  233. lcd.print("Set Temp");
  234. lcd.setCursor(0,1);
  235. lcd.print(nastaw);
  236. if (stanBstart>60 && stanBstart<65) // +
  237. {
  238. nastaw=nastaw+1;
  239. }
  240.  
  241. if (stanBstart>10 && stanBstart<15) //-
  242. {
  243. nastaw=nastaw-1;
  244. }
  245. }
  246.  
  247.  
  248. if (Menu==2)
  249. {
  250. lcd.clear();
  251. lcd.setCursor(0,0);
  252. lcd.print("Set godzina startu");
  253. lcd.setCursor(0,1);
  254. lcd.print("Start");
  255. lcd.print("=");
  256. lcd.print(sHour);
  257. lcd.print(":");
  258. lcd.print(sMinute);
  259. if (stanBstart>60 && stanBstart<65) // +
  260. {
  261. sHour= sHour+1;
  262. }
  263.  
  264. if (stanBstart>10 && stanBstart<15) //-
  265. {
  266. sHour= sHour-1;
  267. }
  268.  
  269.  
  270. }
  271.  
  272.  
  273. if (Menu==3)
  274. {
  275. lcd.clear();
  276. lcd.setCursor(0,0);
  277. lcd.print("Set minuta startu");
  278. lcd.setCursor(0,1);
  279. lcd.print("Start");
  280. lcd.print("=");
  281. lcd.print(sHour);
  282. lcd.print(":");
  283. lcd.print(sMinute);
  284. if (stanBstart>60 && stanBstart<65) // +
  285. {
  286. sMinute=sMinute+1;
  287. }
  288.  
  289. if (stanBstart>10 && stanBstart<15) //-
  290. {
  291. sMinute= sMinute-1;
  292. }
  293.  
  294.  
  295. }
  296.  
  297. if (Menu==4)
  298. {
  299. lcd.clear();
  300. lcd.setCursor(0,0);
  301. lcd.print("Set godzina konca");
  302. lcd.setCursor(0,1);
  303. lcd.print("End");
  304. lcd.print("=");
  305. lcd.print(eHour);
  306. lcd.print(":");
  307. lcd.print(eMinute);
  308. if (stanBstart>60 && stanBstart<65) // +
  309. {
  310. eHour= eHour+1;
  311. }
  312.  
  313. if (stanBstart>10 && stanBstart<15) //-
  314. {
  315. eHour= eHour-1;
  316. }
  317.  
  318.  
  319. }
  320.  
  321.  
  322. if (Menu==5)
  323. {
  324. lcd.clear();
  325. lcd.setCursor(0,0);
  326. lcd.print("Set minuta konca");
  327. lcd.setCursor(0,1);
  328. lcd.print("End");
  329. lcd.print("=");
  330. lcd.print(sHour);
  331. lcd.print(":");
  332. lcd.print(sMinute);
  333. if (stanBstart>60 && stanBstart<65) // +
  334. {
  335. eMinute=eMinute+1;
  336. }
  337.  
  338. if (stanBstart>10 && stanBstart<15) //-
  339. {
  340. eMinute= eMinute-1;
  341. }
  342.  
  343.  
  344. }
  345.  
  346. if(Menu>5)
  347. {
  348. Menu=0;
  349. }
  350.  
  351. }
  352.  
  353.  
  354. void LightOn() {
  355. Serial.println("Turning Light On");
  356. digitalWrite(lightRelay, LOW);
  357. }
  358.  
  359. void LightOff() {
  360. Serial.println("Turning Light Off");
  361. digitalWrite(lightRelay, HIGH);
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement