Advertisement
hlystek

Sterownik

Jul 18th, 2016
2,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ============= Solar Controller v1.21 (en) ===============================================
  2. /*
  3. You are free:
  4. to Share — to copy, distribute and transmit the work
  5. to Remix — to adapt the work
  6. Under the following conditions:
  7. Attribution — You must attribute the work in the manner specified by the author or licensor
  8. (but not in any way that suggests that they endorse you or your use of the work).
  9. Noncommercial — You may not use this work for commercial purposes.
  10. Share Alike — If you alter, transform, or build upon this work, you may distribute
  11. the resulting work only under the same or similar license to this one.
  12. All code is copyright Alvydas, alvydas (at) saulevire.lt (c)2014.
  13.  
  14. Features:
  15. Keyboard temperatures are changed the pump on / off
  16. Manual pump on/off
  17. Storing values
  18. Thermostat with cooling or heating function
  19. LCD 16x2
  20. changing LCD brightness
  21. 5 keys on the keyboard
  22. The ability to connect a network controller ENC28J60
  23. */
  24. //____________________________________________________________________________________//
  25. // ********** Remove the comments in the right line ************************
  26. #define PCB_VERSION 12 // PCB version v1.2 with Arduino Pro Mini
  27. //#define PCB_VERSION 121 // PCB version v1.21 with Arduino Nano (CH340G usb to ttl chip)
  28.  
  29. #ifndef PCB_VERSION
  30. #error "You must define PCB_VERSION in linee 26 or 27"
  31. #endif
  32.  
  33. #if PCB_VERSION == 12
  34. #define Key_Pin A7    // analog pin assigned for button reading
  35. #define BackLight_Pin 9 //LCD backlight pin (standart LCD KeeyPad use pin 10)
  36. #define ONE_WIRE_BUS1 2 // Collector
  37. #define ONE_WIRE_BUS2 8 // Boiler
  38. #define ONE_WIRE_BUS3 A3 // Thermostat
  39. #else
  40. #define Key_Pin A7    // analog pin assigned for button reading
  41. #define BackLight_Pin 9 //LCD backlight pin (standart LCD KeeyPad use pin 10)
  42. #define ONE_WIRE_BUS1 2 // Collector
  43. #define ONE_WIRE_BUS2 8 // Boiler
  44. #define ONE_WIRE_BUS3 A3 // Thermostat
  45. #endif
  46. #define DEBUGds18b20
  47.  
  48. //______________________________________________________________________________________//
  49.  
  50. #include <Wire.h>
  51. #include "MenuBackend.h"  
  52. // Thank wojtekizk, for example
  53. // http://majsterkowo.pl/forum/menubackend-jak-sie-w-nim-odnalezc-t1549.html
  54. #include <LiquidCrystal.h>        
  55. #include <OneWire.h>
  56. #include <DallasTemperature.h>
  57. #include <EEPROM.h>
  58. #include "definitions.h"
  59.  
  60. // --- definiujemy dla LCD własne znaki strzałek: dół, lewo, prawo, gora-dół i powrót ---
  61. uint8_t arrowUpDown[8] = {0x4,0xe,0x15,0x4,0x15,0xe,0x4};
  62. uint8_t arrowDown[8]  = {0x4,0x4,0x4,04,0x15,0xe,0x4};
  63. uint8_t arrowRight[8] = {0x0,0x4,0x2,0x1f,0x2,0x4,0x0};
  64. uint8_t arrowLeft[8] = {0x0,0x4,0x8,0x1f,0x8,0x4,0x0};
  65. uint8_t arrowBack[8] = {0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
  66. uint8_t arrowUp[8]={B00100,B01110,B11111,B00100,B00100,B00100,B00100,B00100};
  67. byte degree[8] ={B00110,B01001,B01001,B00110,B00000,B00000,B00000,B00000};
  68. // --- definicja pinĂłw dla LCD (sprawdĹş piny w swoim LCD)
  69.    
  70. LiquidCrystal lcd(A5, 3, 4, 5, 6, 7);
  71.  
  72. char *LCD_string_1;                      // 1 string text displayed on the LCD
  73. char *LCD_string_2;                      // 2 string text displayed on the LCD
  74. char *LCD_string_3;                      // 3 string text displayed on the LCD
  75. char *LCD_string_4;                      // 4 string text displayed on the LCD
  76.  
  77.  
  78.     boolean InMenu = false;
  79. int freeRam () {
  80.   extern int __heap_start, *__brkval;
  81.   int v;
  82.   return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  83. }
  84.  
  85. // --- create all of the options menu: ---------------------------------------
  86. // de facto create a MenuItem class objects, which inherit the class MenuBackend
  87. MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent); // menu design
  88.    //
  89.    MenuItem P1 =  MenuItem("USTAWIENIA KOLEKTOR",1);
  90.       MenuItem P11 = MenuItem("ROZNICA WL",2);
  91.       MenuItem P12 = MenuItem("ROZNICA WYL",2);
  92.       MenuItem P13 = MenuItem("POMPA MANUALNA",2);
  93.      
  94.    MenuItem P2 = MenuItem("USTAWIENIA POMPY",1);
  95.       MenuItem P21 = MenuItem("USTAW TEMP.WL",2);
  96.       MenuItem P22 = MenuItem("USTAW TEMP.WYL",2);
  97.       MenuItem P23 = MenuItem("STAN",2);
  98.    
  99.    MenuItem P3 = MenuItem("USTAWIENIA FABRYKA",1);
  100.       MenuItem P31 = MenuItem("ZAPISZ",2);
  101.       MenuItem P32 = MenuItem("PRZYWROC",2);
  102.       MenuItem P33 = MenuItem("JASNOSC LCD",2);
  103.  
  104. /* --- Now position the menu (according to the setting specified above) ------------
  105. add - adds vertical addRight - adds a level to the right, to the left adds addLeft
  106. */
  107. void menuSetup()                       // funkcja klasy MenuBackend
  108. {
  109.       menu.getRoot().add(P1);          // ustawiamy korzeĹ„ Menu, czyli pierwszÄ… opcjÄ™
  110.         P1.add(P11);                     // rodzic USTAWIENIA KOLEKTORA ma dziecko ROZNICA WL wiÄ™c dodaje je w pionie
  111.         P11.add(P12);P11.addLeft(P1);  // poniĹĽej ROZNICA WL jest ROZNICA WYL wiÄ™c takĹĽe w pionie
  112.         P12.add(P13);P12.addLeft(P1);  // a addLeft(P1) pozwoli nam wrÄ‚Ĺ‚cić klawiszem w lewo do USTAWIENIA KOLEKTORA
  113.         P13.add(P11);P13.addLeft(P1);  // analogicznie robimy ze wszystkimi podopcjami dla USTAWIENIA KOLEKTORA
  114.         menu.getRoot().add(P2);
  115.       P1.addRight(P2);                 // tutaj zamykamy pÄ™tlÄ™ i wracamy do pierwszej podopcji
  116.                                        // dzieki temu nie musimy wracać na gÄ‚Ĺ‚rÄ™ przez uciÄ…ĹĽliwe
  117.                                        // klikanie klawisza Up
  118.            
  119.         P2.add(P21);                     //
  120.         P21.add(P22);P21.addLeft(P2);  //
  121.         P22.add(P23);P22.addLeft(P2);  //
  122.         P23.add(P21);P23.addLeft(P2);  //
  123.       menu.getRoot().add(P3);
  124.       P2.addRight(P3);    
  125.  
  126.       P3.add(P31);                     //
  127.         P31.add(P32);P31.addLeft(P3);  //
  128.         P32.add(P33);P32.addLeft(P3);  //
  129.         P33.add(P31);P33.addLeft(P3);  //
  130.       menu.getRoot().add(P1);
  131.       P3.addRight(P1);
  132. }
  133. // -----------  -----------------------------------------------------------------------
  134. void menuUseEvent(MenuUseEvent used)      // feature class MenuBackend - after pressing OK
  135.                                           // Here is the menu we offer for shares of handling the OK button
  136. {
  137.    Serial.print("pasirinkta:  "); Serial.println(used.item.getName()); // test and then unnecessary
  138. // --- Below are some of service options -----------
  139. /* __________________________Settings brithness __________________ */
  140.   if (used.item.getName() == "LCD brightness")
  141.   {
  142.   lcd.setCursor(0,0);
  143.   lcd.print("UF");
  144.   lcd.print("                  ");
  145.   lcd.setCursor(3,2);
  146.   lcd.print("JASNOSC");
  147.   lcd.setCursor(16,2);
  148.   lcd.print(LCD_brightness);
  149.   lcd.print("0% ");
  150.   lcd.write(7);
  151.   int  action=-1;delay(1000);          
  152.   while(action!=4)
  153. {
  154. Keyboard_change=-1;
  155. action=Read_keyboard(Key_Pin);        
  156. if(Keyboard_change!=action)            
  157. {lcd.setCursor(12,2);
  158. if (action==1) {LCD_brightness++;
  159. analogWrite(BackLight_Pin,LCD_brightness*25);
  160. delay(300);}
  161. if (action==2)  {LCD_brightness--;
  162. analogWrite(BackLight_Pin,LCD_brightness*25);
  163. delay(300);}
  164. if (LCD_brightness > 10)  LCD_brightness = 1;
  165. if (LCD_brightness < 1)  LCD_brightness = 10;
  166. if (LCD_brightness < 10) lcd.print(" ");
  167. lcd.print(LCD_brightness);
  168. if (LCD_brightness == 0) lcd.print("");
  169. lcd.print("0% ");
  170. if(action==4)
  171. {
  172. lcd.setCursor(17,2);
  173. lcd.print("OK");
  174. lcd.setCursor(3,2);
  175. lcd.print("                ");
  176. menu.moveDown();
  177. }
  178. }
  179. } Keyboard_change=action;
  180. }
  181. //////////////////////////////////////////////////////////////////
  182. /* ______________________ SETTINGS Save _______________________ */
  183. // Save to EEPROM
  184. if (used.item.getName() == "ZAPISZ")
  185. {
  186. SaveConfig();
  187. lcd.setCursor(0,0);lcd.print("ZAPISYWANIE")
  188. ;delay(2000);                                          // show OK for 2 sec
  189. lcd.setCursor(0,0);lcd.print("           ");        // clear line
  190. lcd.setCursor(3,0);lcd.print("*");
  191.  
  192. lcd.print(LCD_string_3);
  193. // reconstruct the previous state at LCD
  194. lcd.setCursor(3,3);lcd.print("*");
  195. menu.moveDown();
  196. }
  197. /////////////////////////////////////////////////////////////
  198. /* __________________________ SETTINGS default ____________ */
  199. // Save to EEPROM
  200. if (used.item.getName() == "PRZYWROC")
  201. {
  202. lcd.print("UF");lcd.setCursor(3,1);lcd.print("OK");
  203. delay(2000);                                          // show OK for 2 sec
  204. lcd.setCursor(19,1);                                   // clear line
  205. lcd.print("              ");
  206. lcd.setCursor(0,0);
  207. lcd.print("*");
  208. //lcd.print(LCD_string_1);
  209. //lcd.print(LCD_string_2);
  210. //lcd.print(LCD_string_3);
  211. //lcd.print(LCD_string_4);                              // reconstruct the previous state at LCD
  212. Pump_power_on_difference = 6;  
  213. Pump_power_off_difference = 3;
  214. temperature_1 = 20;  
  215. temperature_2 = 25;
  216. Thermostat_status = 3;                                // off
  217. Manual_pump_status = false;
  218. LCD_brightness = 5;                
  219. SaveConfig();
  220. menu.moveDown();
  221. }
  222. ////////////////////////////////////////////////////////////////////
  223. /* __________________________ Collector   _______________________ */
  224. //  ON - the difference between the temperature      
  225. if (used.item.getName() == "ROZNICA WL")    // exactly the same string "Difference on "
  226. Pump_power_on_difference =  MeniuFunkcija ("ROZNICA WL", Pump_power_on_difference, 25, 1, "OK");
  227. ///////////////////////////////////////////////////////////////////
  228. /* __________________________ Collector _______________________ */
  229. // OFF - the difference between the temperature            
  230. if (used.item.getName() == "ROZNICA WYL")   // exactly the same string "Difference off"
  231. Pump_power_off_difference =  MeniuFunkcija ("ROZNICA WL", Pump_power_off_difference, 25, 1, "OK");
  232. ///////////////////////////////////////////////////////////////////    
  233.  
  234. /* __________________________ Collector Manual pump on ____________________________________ */
  235. if (used.item.getName() == "POMPA MANUALNA")
  236. {      
  237. lcd.setCursor(6,3);
  238. //lcd.write(7);    
  239. lcd.setCursor(0,3);
  240. lcd.print("POMPA MANUALNA");
  241. if (Manual_pump_status == true) lcd.print("WL"); //
  242. if (Manual_pump_status == false) lcd.print("WY"); //
  243. int  action=-1; delay(1000);         //
  244. while(action!=4)                   //
  245. {
  246. Keyboard_change=-1;
  247. action=Read_keyboard(Key_Pin); //delay(300);
  248.  
  249. if(Keyboard_change!=action)          
  250. {
  251.  if(action==2) {Manual_pump_status = false;
  252.  lcd.setCursor(18,3);
  253.  lcd.print("WY");
  254.  delay(200);
  255. }
  256.  if(action==1) {Manual_pump_status = true;  
  257.  lcd.setCursor(18,3);
  258.  lcd.print("WL");
  259.  delay(200);
  260. }
  261.  if(action==4) // 0
  262. {
  263.  lcd.setCursor(0,0);
  264.  lcd.print("POMPA MANUALNA OK");
  265.  delay(2000); // 0
  266.  lcd.setCursor(0,0);
  267.  lcd.print("              ");
  268.  lcd.setCursor(1,0);
  269.  lcd.print(LCD_string_1);          
  270.  menu.moveDown();
  271. }
  272. }
  273. } Keyboard_change=action;
  274. }
  275. /* __________________________ Termostat temperature 1   _______________________ */
  276. if (used.item.getName() == "USTAW TEMP.WL")   // exactly the same string "temperature 1 "
  277. temperature_1 =  MeniuFunkcija ("USTAW TEMP.WL", temperature_1, 99, -25, "OK");
  278.     ///////////////////////////////////////////////////////////////////
  279. /* __________________________ Termostat temperature 2  _______________________ */    
  280. if (used.item.getName() == "USTAW TEMP.WYL ")   // exactly the same string "temperature 2 "
  281. temperature_2 =  MeniuFunkcija ("USTAW TEMP.WYL", temperature_2, 99, -25, "OK");
  282.     ///////////////////////////////////////////////////////////////////    
  283. /* __________________________ Termostat status  _______________________ */    
  284. if (used.item.getName() == "STAN")
  285. {      
  286. lcd.setCursor(0,0);
  287. lcd.write(7);    
  288. lcd.setCursor(1,1);
  289. lcd.print("STAN-");
  290. if (Thermostat_status == 1) lcd.print("heating"); // heating
  291. if (Thermostat_status == 2) lcd.print("freezing"); // freezing
  292. if (Thermostat_status == 3) lcd.print("WYL"); // turned off
  293. int  action=-1; delay(1000);
  294. while(action!=4)
  295. {
  296. Keyboard_change=-1;
  297. action=Read_keyboard(Key_Pin); //delay(300);  
  298. if(Keyboard_change!=action)          
  299. {
  300. if (action==1) {Thermostat_status++; if(Thermostat_status>3) Thermostat_status=1;
  301. lcd.setCursor(6,3);
  302. if (Thermostat_status == 1) lcd.print("GRZANIE "); // heating
  303. if (Thermostat_status == 2) lcd.print("freezing"); // freezing
  304. if (Thermostat_status == 3) lcd.print("WYL    "); // turned off
  305. delay(200);}
  306. if(action==2)  {Thermostat_status--;
  307. if(Thermostat_status<1) Thermostat_status=3;
  308. lcd.setCursor(6,3);
  309. if (Thermostat_status == 1) lcd.print("heating "); // heating
  310. if (Thermostat_status == 2) lcd.print("freezing"); // freezing
  311. if (Thermostat_status == 3) lcd.print("WYL    "); // turned off
  312. delay(200);}
  313. if(action==4)
  314. {
  315. lcd.setCursor(0,0);
  316. lcd.print(">STAN OK");
  317. delay(2000);
  318. lcd.setCursor(0,0);
  319. lcd.print("              ");
  320. lcd.setCursor(1,0);
  321. lcd.print(LCD_string_1);
  322. menu.moveDown();
  323. }
  324. }
  325. } Keyboard_change=action;
  326. }
  327. }
  328. // --- Reakcja na wciÄŹĹĽËťniÄŹĹĽËťcie klawisza -----------------------------------------------------------------
  329. void menuChangeEvent(MenuChangeEvent changed)  // funkcja klasy MenuBackend
  330. {
  331. if(changed.to.getName()==menu.getRoot())
  332. {
  333. InMenu =false;
  334. Serial.println("Now we are on MenuRoot");
  335. LCD_T_template();
  336. Temperature_Imaging();
  337. }
  338. // tak naprawdę to tylko tutaj przydaje się ów shortkey i służy przede wszystkim do wzbogacenia menu
  339. //o symbole strzałek w zależności co wybrano. Wszystko co tutaj się wyprawia jest pokazywane na LCD.
  340. //
  341.  int c=changed.to.getShortkey();                         // shortkey charge (1,2,3, or 4)
  342.   lcd.clear();                                            // clear lcd
  343.   lcd.setCursor(0,0);
  344.  
  345.   if(c==1)                                                // If this menu Main contacts (shortkey = 1) are:
  346.     {InMenu =true;
  347.     //lcd.write(3);                                         // Left arrow
  348.     strcpy(LCD_string_1,changed.to.getName());            // Create a string in the first line
  349.     lcd.print(LCD_string_1);                              // Display it
  350.     //lcd.setCursor(15,0);lcd.write(4);                     // Right arrow
  351.     //lcd.setCursor(0,1);lcd.write(5);                      // Down arrow
  352.     //lcd.setCursor(15,1);lcd.write(5);                     // Down arrow
  353.     }
  354.     if(c==2)                                              // if the submenu for the child - (shortkey = 2) are:
  355.     {InMenu =true;
  356.     //lcd.print("*");                                       // draw a star
  357.     strcpy(LCD_string_2,changed.to.getName());            // create a string in the first line
  358.     lcd.print(LCD_string_1);                              // print it
  359.     //lcd.setCursor(15,0);lcd.print("*");                   // draw a star
  360.     //lcd.setCursor(0,1);lcd.write(6);                      // the second line and arrow return (arrowBack)
  361.     lcd.print(changed.to.getName());                      // display name of "child"
  362.     //lcd.setCursor(15,1);lcd.write(7);                     // arrow up-down
  363.     }
  364.     if(c==3)                                              // if the child has a child - (shortkey = 3) are:
  365.     {InMenu =true;
  366.     //lcd.print("*");                                       // draw a star
  367.     strcpy(LCD_string_2,changed.to.getName());            // the name of the menu options to the variable line 2
  368.     lcd.print(LCD_string_1);                              // and display the first line of
  369.     //lcd.setCursor(15,0);lcd.print("*");                   // draw a star
  370.     //lcd.setCursor(0,1);lcd.write(6);                      // the second line and arrow arrowBack
  371.     lcd.print(changed.to.getName());                      // display the grandson of the second line
  372.     //lcd.setCursor(15,1);lcd.write(4);                     // arrow to the right because they are the grandchildren
  373.     }
  374.    
  375.     if(c==4)                                              // if grandchild (shortkey = 4) are:
  376.     {InMenu =true;
  377.     //lcd.print("*");                                       // draw a star
  378.     lcd.print(LCD_string_2);                              // in the first line of the display child (or parent grandchild)
  379.     //lcd.setCursor(15,0);lcd.print("*");                   // draw a star
  380.     //lcd.setCursor(0,1);lcd.write(6);                      // the second line and arrow arrowBack
  381.     lcd.print(changed.to.getName());                      // display grandson
  382.     //lcd.setCursor(15,1);lcd.write(7);                     // arrow up-down
  383.     }
  384. }
  385.  
  386. // --- 5 analog buttons keyboard scan version DFRobot --------------------------------------
  387. volatile int Read_keyboard(int analog)
  388. {
  389.    int stan_Analog = analogRead(analog);delay(30);//Serial.println(stan_Analog);
  390.    if (stan_Analog > 1000) return -1; // limit
  391.    if (stan_Analog < 50)   return 3;  // right
  392.   if (stan_Analog < 200)  return 1;  // up
  393.   if (stan_Analog < 400)  return 2;  // down
  394.   if (stan_Analog < 600)  return 0;  // left
  395.   if (stan_Analog < 800)  return 4;  // OK
  396.   return -1;                         // Not pressed
  397. }
  398. // ============================================================================================
  399. //
  400. void setup()
  401. {
  402. LoadConfig();
  403.  
  404. /* ********************************************************* */
  405.  
  406.  pinMode(BackLight_Pin, OUTPUT);
  407.  digitalWrite(BackLight_Pin,HIGH);
  408.  analogWrite(BackLight_Pin,LCD_brightness*25);
  409.  LCD_string_1=new char[20];
  410.  LCD_string_2=new char[20];
  411.  LCD_string_3=new char[20];
  412.  LCD_string_4=new char[20];
  413.                        
  414.  Serial.begin(9600);  
  415.  lcd.begin(20, 4);    
  416.  lcd.clear();
  417.  lcd.createChar(3,arrowLeft);    // LCD symbol left
  418.  lcd.createChar(4,arrowRight);
  419.  lcd.createChar(5,arrowDown);
  420.  lcd.createChar(6,arrowBack);
  421.  lcd.createChar(7,arrowUpDown);
  422.  lcd.createChar(1,arrowUp);
  423.  lcd.createChar(1, degree);
  424.  
  425. lcd.setCursor(0,0);
  426. flashbacklight();
  427. lcd.print("STEROWNIK KOLEKTOROW");
  428. lcd.setCursor(0,1);
  429. lcd.print("     SLONECZNYCH    ");
  430. lcd.setCursor(0,2);
  431. lcd.print("       PELNA        ");
  432. lcd.setCursor(0,3);
  433. lcd.print("     CHATA 2016     ");
  434. delay(1000);
  435. lcd.clear();
  436. Collector_sensor.begin();
  437. Boiler_sensor.begin();
  438. Thermostat_sensor.begin();
  439.  
  440. pinMode(13,OUTPUT);digitalWrite(13,LOW); // only for test
  441. pinMode(Relay_Collector,OUTPUT);pinMode(Relay_Thermostat,OUTPUT);
  442. digitalWrite(Relay_Collector,HIGH);digitalWrite(Relay_Thermostat,HIGH);
  443. menuSetup();
  444. //  menu.moveUp();      
  445. Temperature_measurements_1();
  446. LCD_T_template();
  447. //  Temperature_Imaging();
  448. LCD_switching_on_Time = millis();
  449. temperature_measurement_time_1 = millis();
  450. }  
  451. // setup()
  452. //************ END **************//
  453. // ************************ START void loop() *******************************
  454. void loop()    
  455. {
  456. // measured temperature specified time intervals (Temperature_measurement_interval)
  457. /* +++++++++++++++++++++++++++ First level ++++++++++++++++++++++++++++++++++++ */
  458. if (millis() > temperature_measurement_time_1 ) {
  459.   temperature_measurement_time_1 = millis() + Temperature_measurement_interval_1;
  460.   Temperature_measurements_1();}
  461.  
  462.    
  463.   // if the screen, without application of the button illuminates more than the tasks, backlight off
  464.       if (millis()- LCD_switching_on_Time > The_LCD_light_Break) {
  465.       analogWrite(BackLight_Pin, 0);
  466.        pinMode(13,OUTPUT);digitalWrite(13,LOW); // only for test
  467.       Backlighting = false;
  468.       LCD_switching_on_Time = millis();}
  469.  // When you press any key, the screen backlight is turned on when it is turned off
  470. if ((x != -1) && (Backlighting == false)){ analogWrite(BackLight_Pin,lcd_backlight*25);
  471.                                             digitalWrite(13,HIGH); // only for test
  472.                                             Backlighting = true;}
  473.  
  474. // If the menu is inactive for some time, it returns to the main program
  475. //if ((x != -1) && (InMenu == true))Menu_time_spent_inactive = millis();
  476. //    else {if (millis()- Menu_time_spent_inactive > inactive_menu)
  477. //          menu.toRoot();
  478. //}
  479.   x=Read_keyboard(Key_Pin);delay(30);             // read the state of the keyboard:
  480.   if(Keyboard_change!=x)                               // if there was a change in the state are:
  481.     {
  482.       switch(x)                           // check what was pressed
  483.       {
  484.       case 0: menu.moveRight();break;     // If pressed, move it in the right menu to the right
  485.       case 1: menu.moveUp();break;        // Menu to top
  486.       case 2: menu.moveDown();break;      // Menu Down
  487.       case 3: menu.moveLeft();break;      // Menu to the left
  488.       case 4: menu.use();break;           // pressed OK, so jump to the function menuUseEvent (MenuUseEvend used)
  489.                                           // This function is just serve our menu, check here
  490.                                           // Which option is selected, and here we create the code to the event handler.
  491.       }
  492.     } Keyboard_change=x;                 //Assign the value of x variable amended so that the long pressing the
  493.                                          // Same key did not result in the re-generation event.
  494.                                          // Program responds to a change in the keyboard.
  495. // If you are not currently within the menu is of a continuous program
  496. if (InMenu == false){
  497.   // time interval used for the LCD refresh
  498.   if (millis() > LCD_Update_Time ) {
  499.   LCD_Update_Time = millis() + LCD_Update_Interval;
  500.   LCD_T_template();
  501.   Temperature_Imaging();
  502.  
  503.   //#ifdef DEBUGds18b20
  504. //Serial.println("Temperate_measurement");
  505. //unsigned long start = millis();
  506. //#endif
  507.  
  508. #ifdef DEBUGds18b20
  509. //unsigned long stop = millis();
  510. //Serial.print("Temperature measurement time: ");  Serial.println(stop - start);
  511. Serial.print("K/ ");Serial.print(K);Serial.print(" B/ ");Serial.print(B);Serial.print(" T/ ");Serial.println(T);
  512. Serial.println("----");
  513. Serial.print("Thermostat_status- ");Serial.println(Thermostat_status);
  514. Serial.print("temperature_1- ");Serial.print(temperature_1);
  515. Serial.print("  temperature_2- ");Serial.println(temperature_2);
  516. Serial.println("----");
  517. Serial.print("Pump_power_on_difference- ");Serial.print(Pump_power_on_difference);
  518. Serial.print("  Pump_power_off_difference- ");Serial.println(Pump_power_off_difference);
  519.  
  520. Serial.print("millis- ");Serial.println(millis()/1000);
  521. Serial.println(freeRam());
  522. #endif
  523.   }
  524. }
  525.  
  526.  
  527. //------------------ collector pump and thermostat control -----------------------//
  528. if (millis() > Relay_switching_time )
  529.  {
  530.    Relay_switching_time=millis()+Relay_switching_interval;
  531. if (Manual_pump_status == true) {digitalWrite(Relay_Collector,LOW);}
  532. else{
  533.    if (K-B>=Pump_power_on_difference) digitalWrite(Relay_Collector,LOW);
  534.    if (K-B<=Pump_power_off_difference) digitalWrite(Relay_Collector,HIGH);
  535.    }
  536.  if (Thermostat_status == 1)
  537.   {// If the heating mode (Thermostat_status = 1)
  538.    if (T <= temperature_1) digitalWrite(Relay_Thermostat,LOW);  
  539.    if (T >= temperature_2) digitalWrite(Relay_Thermostat,HIGH);
  540.    }
  541.    if (Thermostat_status == 2)
  542.     {// If the freezing mode (Thermostat_status = 2)
  543.      if (T >= temperature_1) digitalWrite(Relay_Thermostat,LOW);  
  544.      if (T <= temperature_2) digitalWrite(Relay_Thermostat,HIGH);
  545.    }
  546.    if (Thermostat_status == 3)
  547.     // If you do not need a second relay-mode off
  548.      digitalWrite(Relay_Thermostat,HIGH);
  549. }
  550. }// === END ===========================================================
  551. void Temperature_measurements_1(){
  552.  //____________________________ Start Sensor 1 _________________________________
  553. #ifdef SetWaitForConversionFALSE
  554.  Collector_sensor.setWaitForConversion(false);  // makes it async
  555. #endif
  556.  Collector_sensor.requestTemperatures(); // Send the command to get temperatures
  557.  K=Collector_sensor.getTempCByIndex(0);
  558. //_____________________________ Stop Sensor 1 ___________________________________
  559.  //______________________ Start Sensor 3 ________________________________________
  560.  #ifdef SetWaitForConversionFALSE
  561.  Thermostat_sensor.setWaitForConversion(false);  // makes it async
  562. #endif
  563.  Thermostat_sensor.requestTemperatures(); // Send the command to get temperatures
  564. T=Thermostat_sensor.getTempCByIndex(0);
  565. //___________________ Stop Sensor 3 ______________________________________________
  566. //__________________________________________ Start Sensor 2 _____________________
  567. #ifdef SetWaitForConversionFALSE
  568.  Boiler_sensor.setWaitForConversion(false);  // makes it async
  569. #endif
  570.  Boiler_sensor.requestTemperatures(); // Send the command to get temperatures
  571.  B=Boiler_sensor.getTempCByIndex(0);
  572. //_____________________________________ Stop Sensor 2 ____________________________
  573. }
  574. void LCD_T_template(){
  575. //  analogWrite(BackLight_Pin,LCD_brightness*25);
  576.  
  577.  lcd.setCursor(0,0);
  578.  lcd.print("KOLEKTOR");
  579.  lcd.setCursor(13,0);
  580.  lcd.print((char)223);
  581.  lcd.setCursor(15,0);
  582.  lcd.print("R");
  583.  lcd.setCursor(0,1);
  584.  lcd.print("BOILER-A");
  585.  lcd.setCursor(13,1);
  586.  lcd.print((char)223);
  587.  lcd.setCursor(0,2);
  588.  lcd.print("BOILER-B");
  589.  lcd.setCursor(13,2);
  590.  lcd.print((char)223);
  591.  lcd.setCursor(0,3);
  592.  lcd.print("P-CO");
  593.  
  594.  
  595. }
  596.  
  597. void Temperature_Imaging(){
  598. lcd.setCursor(9,0);
  599. lcd.print(K,1);
  600. if (K-B>0)
  601. {lcd.setCursor(16,0);
  602. lcd.print("+");
  603. lcd.print((K-B),1);}
  604. else {lcd.setCursor(16,0);
  605. lcd.print((K-B),1);}
  606. if (K-B>=Pump_power_on_difference)  
  607. {lcd.setCursor(9,3);
  608. lcd.print("C");
  609. lcd.write(1);}  
  610. if (K-B<=Pump_power_off_difference)
  611. {lcd.setCursor(9,3);
  612. lcd.print("P-CW");
  613. lcd.write(5);}  
  614.                                            
  615. lcd.setCursor(9,1);
  616. lcd.print(B,1);
  617. lcd.setCursor(9,2);
  618. lcd.print(T,1);//(int(K + 0.5));
  619. if (Thermostat_status == 1) {// If the heating mode (Thermostat_status = 1)
  620. if (T <= temperature_1)
  621. {lcd.setCursor(14,3);
  622. lcd.print("H");
  623. lcd.write(1);}  
  624. if (T >= temperature_2)
  625. {lcd.setCursor(14,3);
  626. lcd.print("H");
  627. lcd.write(5);}
  628. }
  629. if (Thermostat_status == 2)
  630. {// If the freezing mode (Thermostat_status = 2)
  631. if (T >= temperature_1)
  632. {lcd.setCursor(14,3);
  633. lcd.print("F");
  634. lcd.write(1);}  
  635. if (T <= temperature_2)
  636. {lcd.setCursor(14,3);
  637. lcd.print("F");
  638. lcd.write(5);}  
  639.    }
  640. if (Thermostat_status == 3)
  641. {lcd.setCursor(5,3);
  642. lcd.print("WYL");}
  643. }
  644. int MeniuFunkcija (String text_1, int  Converted_Value, int Max_Value, int Min_Value, String text_2)
  645.             {
  646.        lcd.setCursor(0,0);lcd.write(7);    
  647.        lcd.setCursor(1,1);lcd.print(text_1); //("Nustatyta=   ");
  648.        lcd.setCursor(11,1);lcd.print( Converted_Value); // shows the current value
  649.        int  action=-1; delay(1000);         //
  650.                                          
  651.        while(action!=4)                   //
  652.         {
  653.           Keyboard_change=-1;
  654.           action=Read_keyboard(Key_Pin); //delay(300);  
  655.                                            
  656. if(Keyboard_change!=action)          
  657. {
  658. if (action==1) { Converted_Value++;
  659. if( Converted_Value>Max_Value)  Converted_Value=Max_Value;
  660. lcd.setCursor(11,1);
  661. if( Converted_Value<10) lcd.print(" ");
  662. lcd.print( Converted_Value);
  663. delay(200);}
  664. if(action==2)  { Converted_Value--;
  665. if( Converted_Value<Min_Value)  Converted_Value=Min_Value;
  666. lcd.setCursor(11,1);
  667. if( Converted_Value<10) lcd.print(" ");
  668. lcd.print( Converted_Value);
  669. delay(200);}
  670. if(action==4)
  671. {
  672. lcd.setCursor(0,0);
  673. lcd.print(text_2);
  674. delay(2000);
  675. lcd.setCursor(0,0);
  676. lcd.print("                ");
  677. lcd.setCursor(0,0);
  678. lcd.print("*");
  679. lcd.print(LCD_string_1);
  680. lcd.setCursor(19,0);
  681. lcd.print("*");
  682. menu.moveDown();
  683. }
  684. }
  685. } Keyboard_change=action;                   // Keyboard_change update, in order to react only Keyboard_change keyboard status
  686.                                            // This is an important moment - while loop ends and turn the control to the main loop loop ()
  687. return  Converted_Value;                    // Scan settings
  688. }
  689. boolean LoadConfig(){
  690. if((EEPROM.read(0) == 27) && (EEPROM.read(1) == 28) &&  
  691.   (EEPROM.read(2) == 13) && (EEPROM.read(3) == 18)) {
  692. if (EEPROM.read(4) == EEPROM.read(5)) Pump_power_on_difference = EEPROM.read(4);  
  693. if (EEPROM.read(6) == EEPROM.read(7)) Pump_power_off_difference = EEPROM.read(6);
  694. if (EEPROM.read(8) == EEPROM.read(9)) temperature_1 = EEPROM.read(8);  
  695. if (EEPROM.read(10) == EEPROM.read(11)) temperature_2 = EEPROM.read(10);
  696. if (EEPROM.read(12) == EEPROM.read(13)) Thermostat_status = EEPROM.read(12);
  697. if (EEPROM.read(14) == EEPROM.read(15)) LCD_brightness = EEPROM.read(14);
  698. return true;
  699. }
  700.  return false;
  701. }
  702. // Write settings
  703. void SaveConfig(){
  704.  EEPROM.write(0,27);
  705.  EEPROM.write(1,28);
  706.  EEPROM.write(2,13);
  707.  EEPROM.write(3,18);
  708.  EEPROM.write(4,Pump_power_on_difference);EEPROM.write(5,Pump_power_on_difference);  //
  709.  EEPROM.write(6,Pump_power_off_difference); EEPROM.write(7,Pump_power_off_difference);  //
  710.  EEPROM.write(8,temperature_1);EEPROM.write(9,temperature_1);  //
  711.  EEPROM.write(10,temperature_2); EEPROM.write(11,temperature_2);  //
  712.  EEPROM.write(12,Thermostat_status); EEPROM.write(13,Thermostat_status);  //
  713.  EEPROM.write(14,LCD_brightness); EEPROM.write(15,LCD_brightness);  //
  714. }
  715. void flashbacklight() {
  716.  digitalWrite(BackLight_Pin, LOW);  delay(150);
  717.  digitalWrite(BackLight_Pin, HIGH); delay(150);
  718. }
  719.  
  720. ////////////////////////////
  721. MenuBackend.h
  722. /*
  723. ||
  724. || @file   MenuBackend.h
  725. || @version 1.4
  726. || @author  Alexander Brevig
  727. || @contact alexanderbrevig@gmail.com
  728. || @contribution Adrian Brzezinski adrb@wp.pl, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=vzhang
  729. ||
  730. || @description
  731. || | Provide an easy way of making menus
  732. || #
  733. ||
  734. || @license
  735. || | This library is free software; you can redistribute it and/or
  736. || | modify it under the terms of the GNU Lesser General Public
  737. || | License as published by the Free Software Foundation; version
  738. || | 2.1 of the License.
  739. || |
  740. || | This library is distributed in the hope that it will be useful,
  741. || | but WITHOUT ANY WARRANTY; without even the implied warranty of
  742. || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  743. || | Lesser General Public License for more details.
  744. || |
  745. || | You should have received a copy of the GNU Lesser General Public
  746. || | License along with this library; if not, write to the Free Software
  747. || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  748. || #
  749. ||
  750. */
  751.  
  752. #ifndef MenuBackend_h
  753. #define MenuBackend_h
  754.  
  755. /*
  756.   A menu item will be a container for an item that is a part of a menu
  757.   Each such item has a logical position in the hierarchy as well as a text and maybe a mnemonic shortkey
  758. */
  759. class MenuItem {
  760. public:
  761.  MenuItem(const char* itemName, char shortKey='\0' ) : name(itemName), shortkey(shortKey) {
  762.    before = right = after = left = 0;
  763.  }
  764.  
  765.  //void use(){} //update some internal data / statistics
  766.  inline const char* getName() const { return name; }
  767.  inline const char getShortkey() const { return shortkey; }
  768.  inline const bool hasShortkey() const { return (shortkey!='\0'); }
  769.  inline void setBack(MenuItem *b) { back = b; }
  770.  inline MenuItem* getBack() const { return back; }
  771.  inline MenuItem* getBefore() const { return before; }
  772.  inline MenuItem* getRight() const { return right; }
  773.  inline MenuItem* getAfter() const { return after; }
  774.  inline MenuItem* getLeft() const { return left; }
  775.  
  776.  MenuItem *moveBack() { return back; }
  777.  
  778.  MenuItem *moveUp() {
  779.    if (before) { before->back = this; }
  780.     return before;
  781.   }
  782.  
  783.   MenuItem *moveDown() {
  784.     if (after) { after->back = this; }
  785.     return after;
  786.   }
  787.  
  788.   MenuItem *moveLeft() {
  789.     if (left) { left->back = this; }
  790.     return left;
  791.   }
  792.  
  793.   MenuItem *moveRight() {
  794.     if (right) { right->back = this; }
  795.     return right;
  796.   }
  797.  
  798.   //default vertical menu
  799.   MenuItem &add(MenuItem &mi) { return addAfter(mi); }
  800.  
  801.   MenuItem &addBefore(MenuItem &mi) {
  802.    mi.after = this;
  803.     before = &mi;
  804.     if ( !mi.back ) mi.back = back;
  805.     return mi;
  806.   }
  807.   MenuItem &addRight(MenuItem &mi) {
  808.    mi.left = this;
  809.     right = &mi;
  810.     if ( !mi.back ) mi.back = back;
  811.     return mi;
  812.   }
  813.   MenuItem &addAfter(MenuItem &mi) {
  814.    mi.before = this;
  815.     after = &mi;
  816.     if ( !mi.back ) mi.back = back;
  817.     return mi;
  818.   }
  819.   MenuItem &addLeft(MenuItem &mi) {
  820.    mi.right = this;
  821.     left = &mi;
  822.     if ( !mi.back ) mi.back = back;
  823.     return mi;
  824.   }
  825. protected:
  826.  
  827.   const char* name;
  828.   const char shortkey;
  829.  
  830.   MenuItem *before;
  831.   MenuItem *right;
  832.   MenuItem *after;
  833.   MenuItem *left;
  834.   MenuItem *back;
  835. };
  836.  
  837. //no dependant inclusion of string or cstring
  838. bool menuTestStrings(const char *a, const char *b) {
  839.   while (*a) { if (*a != *b) { return false; } b++; a++; }
  840.   return true;
  841. }
  842. bool operator==(MenuItem &lhs, char* test) {
  843.  return menuTestStrings(lhs.getName(),test);
  844. }
  845. bool operator==(const MenuItem &lhs, char* test) {
  846.  return menuTestStrings(lhs.getName(),test);
  847. }
  848. bool operator==(MenuItem &lhs, MenuItem &rhs) {
  849.  return menuTestStrings(lhs.getName(),rhs.getName());
  850. }
  851. bool operator==(const MenuItem &lhs, MenuItem &rhs) {
  852.  return menuTestStrings(lhs.getName(),rhs.getName());
  853. }
  854.  
  855. struct MenuChangeEvent {
  856.   const MenuItem &from;
  857.   const MenuItem &to;
  858. };
  859.  
  860. struct MenuUseEvent {
  861.   const MenuItem &item;
  862. };
  863.  
  864. typedef void (*cb_change)(MenuChangeEvent);
  865. typedef void (*cb_use)(MenuUseEvent);
  866.  
  867. class MenuBackend {
  868. public:
  869.  
  870.   MenuBackend(cb_use menuUse, cb_change menuChange = 0) : root("MenuRoot") {
  871.     current = &root;
  872.     cb_menuChange = menuChange;
  873.     cb_menuUse = menuUse;
  874.   }
  875.  
  876.   MenuItem &getRoot() {
  877.    return root;
  878.   }
  879.   MenuItem &getCurrent() {
  880.    return *current;
  881.   }
  882.  
  883.   void moveBack() {
  884.     setCurrent(current->getBack());
  885.   }
  886.  
  887.   void moveUp() {
  888.     setCurrent(current->moveUp());
  889.   }
  890.  
  891.   void moveDown() {
  892.     setCurrent(current->moveDown());
  893.   }
  894.  
  895.   void moveLeft() {
  896.     setCurrent(current->moveLeft());
  897.   }
  898.  
  899.   void moveRight() {
  900.     setCurrent(current->moveRight());
  901.   }
  902.  
  903.   void use(char shortkey)
  904.   {
  905.     recursiveSearch(shortkey,&root);
  906.     use();
  907.   }
  908.  
  909.   void use() {
  910.     //current->use();
  911.     if (cb_menuUse) {
  912.       MenuUseEvent mue = { *current };
  913.       cb_menuUse(mue);
  914.     }
  915.   }
  916. void toRoot() {      
  917.  
  918.         setCurrent( &getRoot() );
  919.     }
  920. private:
  921.   void setCurrent( MenuItem *next ) {
  922.     if (next) {
  923.       if (cb_menuChange) {
  924.         MenuChangeEvent mce = { *current, *next };
  925.         (*cb_menuChange)(mce);
  926.       }
  927.       current = next;
  928.     }
  929.   }
  930.   void foundShortkeyItem(MenuItem *mi) {
  931.     mi->setBack(current);
  932.     current = mi;
  933.   }
  934.   char canSearch(const char shortkey, MenuItem *m) {
  935.     if (m==0) { return 0; }
  936.     else  {
  937.       if (m->getShortkey()==shortkey) {
  938.         foundShortkeyItem(m);
  939.         return 1;
  940.       }
  941.       return -1;
  942.     }
  943.   }
  944.   void rSAfter(const char shortkey, MenuItem *m) {
  945.     if (canSearch(shortkey,m)!=1) {
  946.       rSAfter(shortkey, m->getAfter());
  947.       rSRight(shortkey, m->getRight());
  948.       rSLeft(shortkey, m->getLeft());
  949.     }
  950.   }
  951.   void rSRight(const char shortkey, MenuItem *m) {
  952.     if (canSearch(shortkey,m)!=1) {
  953.       rSAfter(shortkey, m->getAfter());
  954.       rSRight(shortkey, m->getRight());
  955.       rSBefore(shortkey, m->getBefore());
  956.     }
  957.   }
  958.   void rSLeft(const char shortkey, MenuItem *m) {
  959.     if (canSearch(shortkey,m)!=1) {
  960.       rSAfter(shortkey, m->getAfter());
  961.       rSLeft(shortkey, m->getLeft());
  962.       rSBefore(shortkey, m->getBefore());
  963.     }
  964.   }
  965.   void rSBefore(const char shortkey, MenuItem *m) {
  966.     if (canSearch(shortkey,m)!=1) {
  967.       rSRight(shortkey, m->getRight());
  968.       rSLeft(shortkey, m->getLeft());
  969.       rSBefore(shortkey, m->getBefore());
  970.     }
  971.   }
  972.   void recursiveSearch(const char shortkey, MenuItem *m) {
  973.     if (canSearch(shortkey,m)!=1) {
  974.       rSAfter(shortkey, m->getAfter());
  975.       rSRight(shortkey, m->getRight());
  976.       rSLeft(shortkey, m->getLeft());
  977.       rSBefore(shortkey, m->getBefore());
  978.     }
  979.   }
  980.  
  981.   MenuItem root;
  982.   MenuItem *current;
  983.  
  984.   cb_change cb_menuChange;
  985.   cb_use cb_menuUse;
  986. };
  987.  
  988. #endif
  989. ///////////////
  990. definitions.h
  991.  
  992. /* @@@@@@@@@@@@@@@@@@@@@@ for testing @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
  993.  
  994.  #define SetWaitForConversionFALSE // accelerated DS18B20 temperature measurement
  995.  
  996. #define DEBUGds18b20 // for temperature measurement testing
  997.  
  998.  
  999. /* ********************** Laikai *************************************** */
  1000. unsigned long Menu_time_spent_inactive; //Meniu_praleistas_neaktyvus_laikas
  1001. #define inactive_menu 20000
  1002. unsigned long temperature_measurement_time_1 = 0;
  1003. unsigned long Relay_switching_time = 0;
  1004. unsigned long temperature_measurement_time_3 = 0;
  1005. #define Temperature_measurement_interval_1 5000
  1006. #define Relay_switching_interval 5000
  1007. #
  1008.  
  1009. unsigned long LCD_switching_on_Time;
  1010. unsigned long  The_LCD_light_Break = 600000;
  1011. unsigned long LCD_Update_Time = 0;
  1012. #define LCD_Update_Interval 5000
  1013.  
  1014.  
  1015. /* ************** Keyboard variables ************************************* */
  1016. volatile int Keyboard_change =-1;  // Check or change the keyboard value
  1017. volatile int x=-1;                 //
  1018. volatile int stan_Analog;          //
  1019. /* ********** LCD description ******************* */
  1020. byte lcd_backlight = 10; // lcd_backlight * 25 = MAX LCD backlight
  1021. byte LCD_brightness = 10; // lcd_pasvietimas * 25 = MAX LCD apsviestumas
  1022. boolean Backlighting = true; // mark on the screen backlight
  1023. // The pump on indicator (arrow up)
  1024. byte arrow_up[8]={ B00100,B01110,B11111,B00100,B00100,B00100,B00100,B00100};
  1025. // Pump shut-off symbol (arrow in the bottom)
  1026. byte arrow_down[8]={ B00100,B00100,B00100,B00100,B00100,B11111,B01110,B00100};
  1027.  
  1028. /* ******************** Relay *********************************** */
  1029. #define Relay_Collector A1 // Collector
  1030. #define Relay_Thermostat A2 // Thermostas
  1031. /* ************************** davikliai *********************** */
  1032.  
  1033. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  1034. OneWire K_t(ONE_WIRE_BUS1); // Collector
  1035. OneWire B_t(ONE_WIRE_BUS2); // Boiler
  1036. OneWire T_t(ONE_WIRE_BUS3); // Thermostat
  1037.  
  1038. // Pass our oneWire reference to Dallas Temperature.
  1039. DallasTemperature Collector_sensor(&K_t);
  1040. DallasTemperature Boiler_sensor(&B_t);
  1041. DallasTemperature Thermostat_sensor(&T_t);
  1042.  
  1043. // variables to store the measured temperature values
  1044. float K,B,T;
  1045.  
  1046. byte Pump_power_on_difference = 5;
  1047. byte Pump_power_off_difference = 3;
  1048. boolean k_uzsalimas = false;
  1049. boolean S_C_pump_manual_control = false; // Mark  manual pump control
  1050. /* ********** Thermostat variables ******************* */
  1051. byte temperature_1 = 20;
  1052. byte temperature_2 = 25;
  1053. byte Thermostat_status =3;
  1054. /* ********** Pump variables ******************* */
  1055. boolean Manual_pump_status = false; //false- manual off, true- manual-on
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement