Advertisement
Guest User

Untitled

a guest
May 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. //************libraries**************//
  2. #include <Wire.h>
  3. #include <RTClib.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <SPI.h>
  6. #include <SD.h>
  7.  
  8. //************************************//
  9. LiquidCrystal_I2C lcd(0x3F,2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Display I2C 16 x 2
  10. RTC_DS1307 RTC;
  11. const int lm35_pin = A1;
  12. const int hih5030 = A0;
  13.  
  14. int P1=5; // Button SET MENU'
  15. int P2=6; // Button +
  16. int P3=7; // Button -
  17. int CS_pin = 10;
  18.  
  19.  
  20. //************variables**************//
  21. int hourupg;
  22. int minupg;
  23. int yearupg;
  24. int monthupg;
  25. int dayupg;
  26. int menu =0;
  27. int temp_adc_val;
  28. int cycle=0;
  29. float temp_val;
  30. float rhRead,rhsensorPinOut=0;
  31. float RH, rhvoltage, trueRH;
  32. void func_humidity(void);
  33. File sd_file;
  34.  
  35. void setup()
  36. {
  37.  
  38. lcd.begin(16,2);
  39. lcd.backlight();
  40. lcd.clear();
  41.  
  42. pinMode(P1,INPUT);
  43. pinMode(P2,INPUT);
  44. pinMode(P3,INPUT);
  45. pinMode(CS_pin, OUTPUT);
  46. Serial.begin(9600);
  47. while (!Serial) {
  48. ; // wait for serial
  49. }
  50. Wire.begin();
  51. RTC.begin();
  52.  
  53. // SD Card Initialization
  54. if (SD.begin()) {
  55. Serial.println("SD card is initialized. Ready to go");
  56. }
  57. else {
  58. Serial.println("Failed");
  59. return;
  60. }
  61.  
  62. //SD test
  63. sd_file = SD.open("data.txt", FILE_WRITE);
  64.  
  65. if (sd_file) {
  66. Serial.print("Time");
  67.  
  68. sd_file.print("Data");
  69. sd_file.print(" , ");
  70. sd_file.print("Laikas");
  71. sd_file.print(" , ");
  72. sd_file.print("Dregme");
  73. sd_file.print(" , ");
  74. sd_file.println("Temperatura_C");
  75. }
  76. sd_file.close();
  77.  
  78.  
  79. if (! RTC.isrunning()) {
  80. Serial.println("RTC is NOT running!");
  81. // Set the date and time at compile time
  82. RTC.adjust(DateTime(__DATE__, __TIME__));
  83. }
  84. // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time
  85. // The default display shows the date and time
  86. int menu=0;
  87. }
  88.  
  89. void loop()
  90. {
  91. cycle=cycle+1;
  92.  
  93. if (cycle==20){
  94. //////////////// Hum
  95. func_humidity();
  96.  
  97.  
  98. //////////////// Temp
  99. temp();
  100. }
  101. if (cycle==20){
  102. sd_file = SD.open("data.txt", FILE_WRITE);
  103. if (sd_file) {
  104. senddata();
  105. }
  106. else {
  107. Serial.println("error opening file");
  108. }
  109. cycle=0;
  110. delay(500);
  111. }
  112. // check for SET button
  113. if(digitalRead(P1))
  114. {
  115. menu=menu+1;
  116. }
  117. if (menu==0)
  118. {
  119. DisplayDateTime(); // void DisplayDateTime
  120.  
  121. }
  122. if (menu==1)
  123. {
  124. DisplaySetHour();
  125. }
  126. if (menu==2)
  127. {
  128. DisplaySetMinute();
  129. }
  130. if (menu==3)
  131. {
  132. DisplaySetYear();
  133. }
  134. if (menu==4)
  135. {
  136. DisplaySetMonth();
  137. }
  138. if (menu==5)
  139. {
  140. DisplaySetDay();
  141. }
  142. if (menu==6)
  143. {
  144. StoreAgg();
  145. delay(400);
  146. menu=0;
  147. }
  148. delay(200);
  149.  
  150.  
  151. }
  152.  
  153. void senddata(){
  154. DateTime now = RTC.now();
  155.  
  156. File sd_file = SD.open("data.txt", FILE_WRITE);
  157. sd_file.print(now.year(), DEC);
  158. yearupg=now.year();
  159. sd_file.print("-");
  160. if (now.month()<=9)
  161. {
  162. sd_file.print("0");
  163. }
  164. sd_file.print(now.month(), DEC);
  165. monthupg=now.month();
  166. sd_file.print("-");
  167. if (now.day()<=9)
  168. {
  169. sd_file.print("0");
  170. }
  171. sd_file.print(now.day(), DEC);
  172. dayupg=now.day();
  173. sd_file.print(" , ");
  174.  
  175. if (now.hour()<=9)
  176. {
  177. sd_file.print("0");
  178. }
  179. sd_file.print(now.hour(), DEC);
  180. hourupg=now.hour();
  181. sd_file.print(":");
  182. if (now.minute()<=9)
  183. {
  184. sd_file.print("0");
  185. }
  186. sd_file.print(now.minute(), DEC);
  187. minupg=now.minute();
  188. sd_file.print(":");
  189. if (now.second()<=9)
  190. {
  191. sd_file.print("0");
  192. }
  193. sd_file.print(now.second(), DEC);
  194. sd_file.print(" , ");
  195. sd_file.print(trueRH,1);
  196. sd_file.print("%");
  197. sd_file.print(" , ");
  198. sd_file.print(temp_val, 1);
  199. sd_file.println("C");
  200. delay(500);
  201. sd_file.close(); //closing the file
  202. }
  203. void temp()
  204. {
  205. temp_adc_val = analogRead(lm35_pin);
  206. temp_val = (temp_adc_val/1024.0)*5000;
  207. temp_val = (temp_val/10);
  208. }
  209.  
  210. void func_humidity(void)
  211. {
  212. rhRead = analogRead(hih5030);
  213. rhvoltage = (rhRead/512) *2.5;
  214. RH = ((rhvoltage/3.3)-0.1515)/0.00636;
  215. trueRH = (RH*1.0546)-(0.00216*temp_val);
  216. }
  217.  
  218. void DisplayDateTime ()
  219. {
  220. // We show the current date and time
  221. DateTime now = RTC.now();
  222.  
  223. lcd.setCursor(0, 1);
  224. if (now.hour()<=9)
  225. {
  226. lcd.print("0");
  227. }
  228. lcd.print(now.hour(), DEC);
  229. hourupg=now.hour();
  230. lcd.print(":");
  231. if (now.minute()<=9)
  232. {
  233. lcd.print("0");
  234. }
  235. lcd.print(now.minute(), DEC);
  236. minupg=now.minute();
  237. //sekundes ekrane
  238. //lcd.print(":");
  239. //if (now.second()<=9)
  240. //{
  241. // lcd.print("0");
  242. //}
  243. //lcd.print(now.second(), DEC);
  244. lcd.print(" ");
  245. lcd.print(trueRH, 1);
  246. lcd.print("% ");
  247.  
  248.  
  249. lcd.setCursor(0, 0);
  250. lcd.print(now.year(), DEC);
  251. yearupg=now.year();
  252. lcd.print("-");
  253. if (now.month()<=9)
  254. {
  255. lcd.print("0");
  256. }
  257. lcd.print(now.month(), DEC);
  258. monthupg=now.month();
  259. lcd.print("-");
  260. if (now.day()<=9)
  261. {
  262. lcd.print("0");
  263. }
  264. lcd.print(now.day(), DEC);
  265. dayupg=now.day();
  266. lcd.print(" ");
  267. lcd.print(temp_val, 1);
  268. lcd.print("C");
  269.  
  270. }
  271.  
  272. void DisplaySetHour()
  273. {
  274. // time setting
  275. lcd.clear();
  276. DateTime now = RTC.now();
  277. if(digitalRead(P2)==HIGH)
  278. {
  279. if(hourupg==23)
  280. {
  281. hourupg=0;
  282. }
  283. else
  284. {
  285. hourupg=hourupg+1;
  286. }
  287. }
  288. if(digitalRead(P3)==HIGH)
  289. {
  290. if(hourupg==0)
  291. {
  292. hourupg=23;
  293. }
  294. else
  295. {
  296. hourupg=hourupg-1;
  297. }
  298. }
  299. lcd.setCursor(0,0);
  300. lcd.print("Valandos:");
  301. lcd.setCursor(0,1);
  302. lcd.print(hourupg,DEC);
  303. delay(200);
  304. }
  305.  
  306. void DisplaySetMinute()
  307. {
  308. // Setting the minutes
  309. lcd.clear();
  310. if(digitalRead(P2)==HIGH)
  311. {
  312. if (minupg==59)
  313. {
  314. minupg=0;
  315. }
  316. else
  317. {
  318. minupg=minupg+1;
  319. }
  320. }
  321. if(digitalRead(P3)==HIGH)
  322. {
  323. if (minupg==0)
  324. {
  325. minupg=59;
  326. }
  327. else
  328. {
  329. minupg=minupg-1;
  330. }
  331. }
  332. lcd.setCursor(0,0);
  333. lcd.print("Minutes:");
  334. lcd.setCursor(0,1);
  335. lcd.print(minupg,DEC);
  336. delay(200);
  337. }
  338.  
  339. void DisplaySetYear()
  340. {
  341. // setting the year
  342. lcd.clear();
  343. if(digitalRead(P2)==HIGH)
  344. {
  345. yearupg=yearupg+1;
  346. }
  347. if(digitalRead(P3)==HIGH)
  348. {
  349. yearupg=yearupg-1;
  350. }
  351. lcd.setCursor(0,0);
  352. lcd.print("Metai:");
  353. lcd.setCursor(0,1);
  354. lcd.print(yearupg,DEC);
  355. delay(200);
  356. }
  357.  
  358. void DisplaySetMonth()
  359. {
  360. // Setting the month
  361. lcd.clear();
  362. if(digitalRead(P2)==HIGH)
  363. {
  364. if (monthupg==12)
  365. {
  366. monthupg=1;
  367. }
  368. else
  369. {
  370. monthupg=monthupg+1;
  371. }
  372. }
  373. if(digitalRead(P3)==HIGH)
  374. {
  375. if (monthupg==1)
  376. {
  377. monthupg=12;
  378. }
  379. else
  380. {
  381. monthupg=monthupg-1;
  382. }
  383. }
  384. lcd.setCursor(0,0);
  385. lcd.print("Menesis:");
  386. lcd.setCursor(0,1);
  387. lcd.print(monthupg,DEC);
  388. delay(200);
  389. }
  390.  
  391. void DisplaySetDay()
  392. {
  393. // Setting the day
  394. lcd.clear();
  395. if(digitalRead(P2)==HIGH)
  396. {
  397. if (dayupg==31)
  398. {
  399. dayupg=1;
  400. }
  401. else
  402. {
  403. dayupg=dayupg+1;
  404. }
  405. }
  406. if(digitalRead(P3)==HIGH)
  407. {
  408. if (dayupg==1)
  409. {
  410. dayupg=31;
  411. }
  412. else
  413. {
  414. dayupg=dayupg-1;
  415. }
  416. }
  417. lcd.setCursor(0,0);
  418. lcd.print("Diena:");
  419. lcd.setCursor(0,1);
  420. lcd.print(dayupg,DEC);
  421. delay(200);
  422. }
  423.  
  424. void StoreAgg()
  425. {
  426. // Variable saving
  427. lcd.clear();
  428. lcd.setCursor(0,0);
  429. lcd.print("Saugomas");
  430. lcd.setCursor(0,1);
  431. lcd.print("laikas");
  432. RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0));
  433. delay(200);
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement