Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.88 KB | None | 0 0
  1. #include <IRremote.h> //Include the IRremote library
  2. #include <Wire.h> //Include the Wire library
  3. #include <LiquidCrystal_I2C.h> //Include the LCD library
  4. #include <avr/pgmspace.h>
  5. LiquidCrystal_I2C lcd(0x3F, 16, 2); //Set the LCD kind
  6.  
  7. unsigned int z = 600;
  8. unsigned int o = 1800;
  9. unsigned int t = 4200;
  10. unsigned int h = 8000;
  11. const unsigned int ACIntro PROGMEM = {8000,4250, 1800,600, 600,1750, 650,1700, 650,1700, 650,1750, 650,1700, 700,1700, 650,1700, 1800,600, 650,1700, 650,1700, 650,1750, 650,1750, 600,1750, 1800,600, 600,1750, 600,1750, 650,1700, 650,1750, 650,1700, 1800,600, 1750,650, 600,1750, 600,1750, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 600,1800, 600,1750, 650,1700, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750};
  12. const unsigned int ACTurnOff PROGMEM = {650,1700, 650,1750, 1800,550, 1800,600, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 1800,550, 1800,600, 1750,600, 650,1750, 600,1750, 650,1750, 600,1750, 1800,550};
  13. const unsigned int ACEnd PROGMEM = {z, o, z, o, o};
  14.  
  15. IRsend irsend; //Create an Infra-red LED object
  16.  
  17. //name the buttons:
  18. #define a 8
  19. #define b 4
  20. #define up 5
  21. #define down 6
  22. #define cr 7
  23.  
  24. String cmd = "12:24,25|"; //This string containses the commands, this way: hour, :, minute, ,, temperature, |
  25. unsigned long int timer = millis();//Set last millisecond which had writen
  26. unsigned long int lightTime;//Set an integer which be the number of milliseconds until the screen turns off
  27. int second = 0; //Set current second
  28. int minute = 0;//Set current minute
  29. int hour = 0;//Set current hour
  30. //int hom = 0; /*Delete it*/
  31. int temperature = 24;//Set current temperature
  32.  
  33.  
  34.  
  35. void setup() {
  36. Serial.begin(9600); //Start serial
  37. //Serial.println(string_delete(cmd, 26, 9)); /*Delete it*/
  38. //Set all the buttons pins as input pullup pins
  39. pinMode(a, INPUT_PULLUP);
  40. pinMode(b, INPUT_PULLUP);
  41. pinMode(up, INPUT_PULLUP);
  42. pinMode(down, INPUT_PULLUP);
  43. pinMode(cr, INPUT_PULLUP);
  44.  
  45. //Start the LCD
  46. lcd.init();
  47. lcd.init();
  48. lcd.backlight();
  49. lcd.setCursor(0,0);/*
  50. lcd.print("Time not setted");
  51. lcd.backlight(); //Turn on LCD lights
  52. while(hom < 2) { /*Delete this while*//*
  53. while(Serial.available() < 1) {}
  54. int data = Serial.parseInt();
  55. if(hom == 0) {
  56. hour = data;
  57. hom++;
  58. }
  59. else {
  60. minute = data;
  61. hom++;
  62. }
  63. }*/
  64. timer = millis(); //Set timer as current millisecond
  65. lightTime = millis(); //Set lightTime as current millisecond
  66. lcd.clear();
  67. //Serial.println("Start"); /*Delete it*/
  68. }
  69. int hmt = 0;
  70. boolean light = true; //Set current light mode
  71. void loop() {
  72. check_command(); //Check if there is a command for this current time, and if it's do it
  73. set_time(true); //Set current time and show information on the LCD
  74. if(millis() - lightTime > 20000) { //If was been crossen 20 seconds from the last button pressing
  75. lcd.noBacklight(); //Turn off the LCD light
  76. lcd.noDisplay();
  77. light = false; //Set current light mode off
  78. }
  79. if(light == true) { //If the light is on
  80. //Get input data from the buttons
  81. if(digitalRead(a) == 0) {
  82. //lcd.backlight();
  83. //lcd.display();
  84. lcd.clear();
  85. show_programm();
  86. lightTime = millis();
  87. }
  88. if(digitalRead(b) == 0) {
  89. //lcd.backlight();
  90. //lcd.display();
  91. lcd.clear();
  92. add_command_scr();
  93. lightTime = millis();
  94. }
  95. if(digitalRead(cr) == 0) {
  96. //lcd.backlight();
  97. //lcd.display();
  98. lcd.clear();
  99. lightTime = millis();
  100. while(digitalRead(cr) == 0);
  101. hmt++;
  102. if(hmt == 3) {hmt = 0;}
  103. }
  104. if(digitalRead(up) == 0) {
  105. //lcd.backlight();
  106. //lcd.display();
  107. lcd.clear();
  108. lightTime = millis();
  109. while(digitalRead(up) == 0);
  110. if(hmt == 0) {
  111. hour++;
  112. if(hour == 24) {hour = 0;}
  113. }
  114. else if(hmt == 1) {
  115. minute++;
  116. if(minute == 60) {minute = 0;}
  117. }
  118. else {temperature++;}
  119. }
  120. if(digitalRead(down) == 0) {
  121. lightTime = millis();
  122. //lcd.backlight();
  123. //lcd.display();
  124. lcd.clear();
  125. while(digitalRead(down) == 0);
  126. if(hmt == 0) {
  127. hour--;
  128. if(hour == -1) {hour = 24;}
  129. }
  130. else if(hmt == 1) {
  131. minute--;
  132. if(minute == -1) {minute = 59;}
  133. }
  134. else {temperature--;}
  135. }
  136. }
  137. else {
  138. if(digitalRead(a) == 0 or digitalRead(b) == 0 or digitalRead(cr) == 0 or digitalRead(up) == 0 or digitalRead(down) == 0) {
  139. while(digitalRead(a) == 0 or digitalRead(b) == 0 or digitalRead(cr) == 0 or digitalRead(up) == 0 or digitalRead(down) == 0);
  140. light = true;
  141. lightTime = millis();
  142. lcd.backlight();
  143. lcd.display();
  144. }
  145. }
  146. }
  147. void check_command() {
  148. String str = cmd;
  149. for(int i = 0; i < char_count(cmd ,'|'); i++) {
  150. int h = string_copy(str, 0, 1).toInt();
  151. int m = string_copy(str, 3, 4).toInt();
  152. int t = string_copy(str, 6, 7).toInt();
  153. if(h == hour && m == minute) {
  154. /*if(h == string_copy(cmd, i*9, i*9 + 1).toInt() && m == string_copy(cmd, i*9 + 3, i*9 + 4).toInt() && t == string_copy(cmd, i*9 + 6, i*9 + 7).toInt()) {
  155. cmd = string_delete(cmd, i*9 - 1, 9);
  156. }*/
  157. set_temperature_new(t);
  158. break;
  159. }
  160. else {
  161. str.remove(0, 9);
  162. }
  163. }
  164. }
  165. void add_command(int h, int m, int t) {
  166. if(h < 10) {cmd += "0";} cmd += String(h);
  167. cmd += ":";
  168. if(m < 10) {cmd += "0";} cmd += String(m);
  169. cmd += ",";
  170. if(t < 10) {cmd += "0";} cmd += String(t);//If it will be 0 (=ON/OFF) it will write "00"
  171. cmd += "|";
  172. }
  173. int lastT = -1;
  174. void set_temperature_new(int t) {
  175. if(t != lastT) {
  176. Serial.println(t);
  177. if(t > 1) {
  178. ac_command(t);
  179. }
  180. else {
  181. irsend.sendRaw(ACIntro, 90, 38);
  182. irsend.sendRaw(ACTurnOff, 36, 38);
  183. irsend.sendRaw(ACEnd, 5, 38);
  184. }
  185. lastT = t;
  186. }
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. //This is a string controls library for arduino by Eitan Rabinovitch
  194. /*char string_char_at(String str, int index) {
  195. return str.charAt(index);
  196. }*/
  197. String string_copy(String data, int index, int count) {
  198. String str = "";
  199. int number = 0;
  200.  
  201. for(int i = index; i < count + 1; i++) {
  202. str += data.charAt(i);
  203. }
  204.  
  205. return str;
  206. }
  207. /*String string_replace_all(String data, String o, String n) {
  208. data.replace(o, n);
  209. return data;
  210. }
  211. String string_delete(String data, int index, int count) {
  212. data.remove(index, count);
  213. return data;
  214. }
  215. int char_pos(String data, char nano) {
  216. int n = 0;
  217. while(n < data.length() + 1) {
  218. if(data.charAt(n) == nano) {
  219. return n;
  220. }
  221. n++;
  222. }
  223. return 0;
  224. }*/
  225. int char_count(String data, char nano) {
  226. int n = 0;
  227. int count = 0;
  228. while(n < data.length() + 1) {
  229. if(data.charAt(n) == nano) {
  230. count++;
  231. }
  232. n++;
  233. }
  234. return count;
  235. }
  236. /*String string_insert(String data, int index, String insert) {
  237. String str = string_copy(data, 0, index) + insert + string_copy(data, index + 1, data.length());
  238. return str;
  239. }*/
  240. void set_time(boolean p) {
  241. if(millis() - timer >= 1000) {
  242. second++;
  243. timer += 1000;
  244. }
  245. if(second == 60) {
  246. minute++;
  247. second = 0;
  248. }
  249. if(minute == 60) {
  250. hour++;
  251. minute = 0;
  252. second--; //Because of the delay
  253. }
  254. if(hour == 24) {
  255. hour = 0;
  256. }
  257. if(p == true) {
  258. lcd.setCursor(3, 0);
  259. lcd.print("Time " + String(hour) + ":" + String(minute) + " "); //Space because if hour/minute < 10 one digit of the temperature won't be changed
  260. lcd.setCursor(1, 1); //And then the temperature will look like 3 digit number
  261. lcd.print(F("A Show B Add"));
  262. }
  263. }
  264.  
  265. //--Screens---------------------------------------
  266. void show_programm() {
  267. String str = cmd;
  268. int number = 0;
  269. boolean c = false;
  270. while(digitalRead(a) == 0);
  271. while(number < char_count(cmd, '|')) {
  272. set_time(false);
  273. lcd.setCursor(0, 0);
  274. lcd.print("At " + string_copy(str, 0, 1) + ":" + string_copy(str, 3, 4) + " temp " + string_copy(str, 6, 7));
  275. lcd.setCursor(1, 1);
  276. lcd.print(F("A Next B Del"));
  277. if(digitalRead(a) == 0) {
  278. c = true;
  279. }
  280. if(digitalRead(a) == 1 && c == true) {
  281. if(str.length() >= 9) {
  282. str.remove(0, 9);
  283. number++;
  284. }
  285. else {break;}
  286. c = false;
  287. }
  288. if(digitalRead(b) == 0) {
  289. while(digitalRead(b) == 0);
  290. if(number != 0) {
  291. cmd.remove(number*9 - 1, 9);
  292. } else {
  293. cmd.remove(0, 9);
  294. }
  295. str = cmd;
  296. }
  297. }
  298. lcd.clear();
  299. }
  300. void add_command_scr() {
  301. int h = 0;
  302. int m = 0;
  303. int t = 24;
  304. while(digitalRead(b) == 0);
  305. while(digitalRead(b) == 1) {
  306. set_time(false);
  307. if(digitalRead(up) == 0) {
  308. while(digitalRead(up) == 0);
  309. h++;
  310. if(h == 24) {h = 0;}
  311. }
  312. if(digitalRead(down) == 0) {
  313. while(digitalRead(down) == 0);
  314. h--;
  315. if(h == -1) {h = 24;}
  316. }
  317. lcd.setCursor(0, 0);
  318. lcd.print("At " + String(h) + ":00 temp 24 ");
  319. lcd.setCursor(0, 1);
  320. lcd.print(F("Press B to min"));
  321. }
  322. while(digitalRead(b) == 0);
  323. lcd.clear();
  324. while(digitalRead(b) == 1) {
  325. set_time(false);
  326. if(digitalRead(up) == 0) {
  327. while(digitalRead(up) == 0);
  328. m++;
  329. if(m == 60) {m = 0;}
  330. }
  331. if(digitalRead(down) == 0) {
  332. while(digitalRead(down) == 0);
  333. m--;
  334. if(m == -1) {m = 59;}
  335. }
  336. lcd.setCursor(0, 0);
  337. lcd.print("At " + String(h) + ":" + String(m) + " temp 24 ");
  338. lcd.setCursor(0, 1);
  339. lcd.print(F("Press B to temp"));
  340. }
  341. while(digitalRead(b) == 0);
  342. lcd.clear();
  343. while(digitalRead(b) == 1) {
  344. set_time(false);
  345. if(digitalRead(up) == 0) {
  346. while(digitalRead(up) == 0);
  347. t++;
  348. }
  349. if(digitalRead(down) == 0) {
  350. while(digitalRead(down) == 0);
  351. t--;
  352. }
  353. lcd.setCursor(0, 0);
  354. lcd.print("At " + String(h) + ":" + String(m) + " temp " + String(t) + " ");
  355. lcd.setCursor(0, 1);
  356. lcd.print(F("Press B to save"));
  357. }
  358. add_command(h, m, t);
  359. while(digitalRead(b) == 0);
  360. lcd.clear();
  361. }
  362.  
  363.  
  364. unsigned int basecode[131] = {8000,4250, 1800,600, 600,1750, 650,1700, 650,1700, 650,1750, 650,1700, 650,1750, 650,1700, 1800,600, 600,1750, 650,1700, 650,1750, 1800,600, 700,1700, 600,1800, 600,1750, 600,1750, 1800,550, 650,1750, 600,1750, 650,1750, 1750,650, 600,1750, 600,1750, 600,1800, 600,1750, 600,1750, 650,1750, 600,1800, 550,1800, 600,1750, 600,1800, 550,1800, 600,1750, 650,1750, 600,1800, 550,1800, 600,1750, 600,1800, 550,1800, 600,1750, 600,1800, 600,1750, 600,1800, 1700,650, 1750,600, 600,1800, 550,1850, 550,1800, 600,1750, 600,1800, 550,1800, 600,1800, 550,1800, 1750,600, 1750,650, 1700,700, 550,1800, 550,1800, 1750,650, 1700,650, 550,1850/*, 550,1800, 550,1850, 1550*/};
  365. void ac_command(int temperature) {
  366. switch(temperature) {
  367. case 16: //Confirmed
  368. basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
  369.  
  370. basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  371. case 17: //Confirmed
  372. basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
  373.  
  374. basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  375. case 18: //Confirmed
  376. basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
  377.  
  378. basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  379. case 19: //Confirmed
  380. basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
  381.  
  382. basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  383. case 20: //Confirmed
  384. basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
  385.  
  386. basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  387. case 21: //Confirmed
  388. basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
  389.  
  390. basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  391. case 22: //Confirmed
  392. basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
  393.  
  394. basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = z; basecode[121] = o; basecode[122] = z; basecode[123] = o; basecode[124] = o; basecode[125] = z; break;
  395. case 23: //Confirmed
  396. basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
  397.  
  398. basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = z; basecode[121] = o; basecode[122] = z; basecode[123] = o; basecode[124] = o; basecode[125] = z; break;
  399. case 24: //Confirmed
  400. basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
  401.  
  402. basecode[114] = o; basecode[115] = z; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  403. case 25: //Confirmed
  404. basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
  405.  
  406. basecode[114] = o; basecode[115] = z; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  407. case 26: //Confirmed
  408. basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
  409.  
  410. basecode[114] = o; basecode[115] = z; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  411. case 27: //Confirmed
  412. basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
  413.  
  414. basecode[114] = o; basecode[115] = z; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
  415. }
  416. irsend.sendRaw(basecode, 126, 38);
  417. irsend.sendRaw(ACEnd, 5, 38);
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement