Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============= Solar Controller v1.21 (en) ===============================================
- /*
- You are free:
- to Share — to copy, distribute and transmit the work
- to Remix — to adapt the work
- Under the following conditions:
- Attribution — You must attribute the work in the manner specified by the author or licensor
- (but not in any way that suggests that they endorse you or your use of the work).
- Noncommercial — You may not use this work for commercial purposes.
- Share Alike — If you alter, transform, or build upon this work, you may distribute
- the resulting work only under the same or similar license to this one.
- All code is copyright Alvydas, alvydas (at) saulevire.lt (c)2014.
- Features:
- Keyboard temperatures are changed the pump on / off
- Manual pump on/off
- Storing values
- Thermostat with cooling or heating function
- LCD 16x2
- changing LCD brightness
- 5 keys on the keyboard
- The ability to connect a network controller ENC28J60
- */
- //____________________________________________________________________________________//
- // ********** Remove the comments in the right line ************************
- #define PCB_VERSION 12 // PCB version v1.2 with Arduino Pro Mini
- //#define PCB_VERSION 121 // PCB version v1.21 with Arduino Nano (CH340G usb to ttl chip)
- #ifndef PCB_VERSION
- #error "You must define PCB_VERSION in linee 26 or 27"
- #endif
- #if PCB_VERSION == 12
- #define Key_Pin A7 // analog pin assigned for button reading
- #define BackLight_Pin 9 //LCD backlight pin (standart LCD KeeyPad use pin 10)
- #define ONE_WIRE_BUS1 2 // Collector
- #define ONE_WIRE_BUS2 8 // Boiler
- #define ONE_WIRE_BUS3 A3 // Thermostat
- #else
- #define Key_Pin A7 // analog pin assigned for button reading
- #define BackLight_Pin 9 //LCD backlight pin (standart LCD KeeyPad use pin 10)
- #define ONE_WIRE_BUS1 2 // Collector
- #define ONE_WIRE_BUS2 8 // Boiler
- #define ONE_WIRE_BUS3 A3 // Thermostat
- #endif
- #define DEBUGds18b20
- //______________________________________________________________________________________//
- #include <Wire.h>
- #include "MenuBackend.h"
- // Thank wojtekizk, for example
- // http://majsterkowo.pl/forum/menubackend-jak-sie-w-nim-odnalezc-t1549.html
- #include <LiquidCrystal.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <EEPROM.h>
- #include "definitions.h"
- // --- definiujemy dla LCD własne znaki strzałek: dół, lewo, prawo, gora-dół i powrót ---
- uint8_t arrowUpDown[8] = {0x4,0xe,0x15,0x4,0x15,0xe,0x4};
- uint8_t arrowDown[8] = {0x4,0x4,0x4,04,0x15,0xe,0x4};
- uint8_t arrowRight[8] = {0x0,0x4,0x2,0x1f,0x2,0x4,0x0};
- uint8_t arrowLeft[8] = {0x0,0x4,0x8,0x1f,0x8,0x4,0x0};
- uint8_t arrowBack[8] = {0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
- uint8_t arrowUp[8]={B00100,B01110,B11111,B00100,B00100,B00100,B00100,B00100};
- byte degree[8] ={B00110,B01001,B01001,B00110,B00000,B00000,B00000,B00000};
- // --- definicja pinĂłw dla LCD (sprawdĹş piny w swoim LCD)
- LiquidCrystal lcd(A5, 3, 4, 5, 6, 7);
- char *LCD_string_1; // 1 string text displayed on the LCD
- char *LCD_string_2; // 2 string text displayed on the LCD
- char *LCD_string_3; // 3 string text displayed on the LCD
- char *LCD_string_4; // 4 string text displayed on the LCD
- boolean InMenu = false;
- int freeRam () {
- extern int __heap_start, *__brkval;
- int v;
- return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
- }
- // --- create all of the options menu: ---------------------------------------
- // de facto create a MenuItem class objects, which inherit the class MenuBackend
- MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent); // menu design
- //
- MenuItem P1 = MenuItem("USTAWIENIA KOLEKTOR",1);
- MenuItem P11 = MenuItem("ROZNICA WL",2);
- MenuItem P12 = MenuItem("ROZNICA WYL",2);
- MenuItem P13 = MenuItem("POMPA MANUALNA",2);
- MenuItem P2 = MenuItem("USTAWIENIA POMPY",1);
- MenuItem P21 = MenuItem("USTAW TEMP.WL",2);
- MenuItem P22 = MenuItem("USTAW TEMP.WYL",2);
- MenuItem P23 = MenuItem("STAN",2);
- MenuItem P3 = MenuItem("USTAWIENIA FABRYKA",1);
- MenuItem P31 = MenuItem("ZAPISZ",2);
- MenuItem P32 = MenuItem("PRZYWROC",2);
- MenuItem P33 = MenuItem("JASNOSC LCD",2);
- /* --- Now position the menu (according to the setting specified above) ------------
- add - adds vertical addRight - adds a level to the right, to the left adds addLeft
- */
- void menuSetup() // funkcja klasy MenuBackend
- {
- menu.getRoot().add(P1); // ustawiamy korzeń Menu, czyli pierwszą opcję
- P1.add(P11); // rodzic USTAWIENIA KOLEKTORA ma dziecko ROZNICA WL więc dodaje je w pionie
- P11.add(P12);P11.addLeft(P1); // poniżej ROZNICA WL jest ROZNICA WYL więc także w pionie
- P12.add(P13);P12.addLeft(P1); // a addLeft(P1) pozwoli nam wrócić klawiszem w lewo do USTAWIENIA KOLEKTORA
- P13.add(P11);P13.addLeft(P1); // analogicznie robimy ze wszystkimi podopcjami dla USTAWIENIA KOLEKTORA
- menu.getRoot().add(P2);
- P1.addRight(P2); // tutaj zamykamy pętlę i wracamy do pierwszej podopcji
- // dzieki temu nie musimy wracać na górę przez uciążliwe
- // klikanie klawisza Up
- P2.add(P21); //
- P21.add(P22);P21.addLeft(P2); //
- P22.add(P23);P22.addLeft(P2); //
- P23.add(P21);P23.addLeft(P2); //
- menu.getRoot().add(P3);
- P2.addRight(P3);
- P3.add(P31); //
- P31.add(P32);P31.addLeft(P3); //
- P32.add(P33);P32.addLeft(P3); //
- P33.add(P31);P33.addLeft(P3); //
- menu.getRoot().add(P1);
- P3.addRight(P1);
- }
- // ----------- -----------------------------------------------------------------------
- void menuUseEvent(MenuUseEvent used) // feature class MenuBackend - after pressing OK
- // Here is the menu we offer for shares of handling the OK button
- {
- Serial.print("pasirinkta: "); Serial.println(used.item.getName()); // test and then unnecessary
- // --- Below are some of service options -----------
- /* __________________________Settings brithness __________________ */
- if (used.item.getName() == "LCD brightness")
- {
- lcd.setCursor(0,0);
- lcd.print("UF");
- lcd.print(" ");
- lcd.setCursor(3,2);
- lcd.print("JASNOSC");
- lcd.setCursor(16,2);
- lcd.print(LCD_brightness);
- lcd.print("0% ");
- lcd.write(7);
- int action=-1;delay(1000);
- while(action!=4)
- {
- Keyboard_change=-1;
- action=Read_keyboard(Key_Pin);
- if(Keyboard_change!=action)
- {lcd.setCursor(12,2);
- if (action==1) {LCD_brightness++;
- analogWrite(BackLight_Pin,LCD_brightness*25);
- delay(300);}
- if (action==2) {LCD_brightness--;
- analogWrite(BackLight_Pin,LCD_brightness*25);
- delay(300);}
- if (LCD_brightness > 10) LCD_brightness = 1;
- if (LCD_brightness < 1) LCD_brightness = 10;
- if (LCD_brightness < 10) lcd.print(" ");
- lcd.print(LCD_brightness);
- if (LCD_brightness == 0) lcd.print("");
- lcd.print("0% ");
- if(action==4)
- {
- lcd.setCursor(17,2);
- lcd.print("OK");
- lcd.setCursor(3,2);
- lcd.print(" ");
- menu.moveDown();
- }
- }
- } Keyboard_change=action;
- }
- //////////////////////////////////////////////////////////////////
- /* ______________________ SETTINGS Save _______________________ */
- // Save to EEPROM
- if (used.item.getName() == "ZAPISZ")
- {
- SaveConfig();
- lcd.setCursor(0,0);lcd.print("ZAPISYWANIE")
- ;delay(2000); // show OK for 2 sec
- lcd.setCursor(0,0);lcd.print(" "); // clear line
- lcd.setCursor(3,0);lcd.print("*");
- lcd.print(LCD_string_3);
- // reconstruct the previous state at LCD
- lcd.setCursor(3,3);lcd.print("*");
- menu.moveDown();
- }
- /////////////////////////////////////////////////////////////
- /* __________________________ SETTINGS default ____________ */
- // Save to EEPROM
- if (used.item.getName() == "PRZYWROC")
- {
- lcd.print("UF");lcd.setCursor(3,1);lcd.print("OK");
- delay(2000); // show OK for 2 sec
- lcd.setCursor(19,1); // clear line
- lcd.print(" ");
- lcd.setCursor(0,0);
- lcd.print("*");
- //lcd.print(LCD_string_1);
- //lcd.print(LCD_string_2);
- //lcd.print(LCD_string_3);
- //lcd.print(LCD_string_4); // reconstruct the previous state at LCD
- Pump_power_on_difference = 6;
- Pump_power_off_difference = 3;
- temperature_1 = 20;
- temperature_2 = 25;
- Thermostat_status = 3; // off
- Manual_pump_status = false;
- LCD_brightness = 5;
- SaveConfig();
- menu.moveDown();
- }
- ////////////////////////////////////////////////////////////////////
- /* __________________________ Collector _______________________ */
- // ON - the difference between the temperature
- if (used.item.getName() == "ROZNICA WL") // exactly the same string "Difference on "
- Pump_power_on_difference = MeniuFunkcija ("ROZNICA WL", Pump_power_on_difference, 25, 1, "OK");
- ///////////////////////////////////////////////////////////////////
- /* __________________________ Collector _______________________ */
- // OFF - the difference between the temperature
- if (used.item.getName() == "ROZNICA WYL") // exactly the same string "Difference off"
- Pump_power_off_difference = MeniuFunkcija ("ROZNICA WL", Pump_power_off_difference, 25, 1, "OK");
- ///////////////////////////////////////////////////////////////////
- /* __________________________ Collector Manual pump on ____________________________________ */
- if (used.item.getName() == "POMPA MANUALNA")
- {
- lcd.setCursor(6,3);
- //lcd.write(7);
- lcd.setCursor(0,3);
- lcd.print("POMPA MANUALNA");
- if (Manual_pump_status == true) lcd.print("WL"); //
- if (Manual_pump_status == false) lcd.print("WY"); //
- int action=-1; delay(1000); //
- while(action!=4) //
- {
- Keyboard_change=-1;
- action=Read_keyboard(Key_Pin); //delay(300);
- if(Keyboard_change!=action)
- {
- if(action==2) {Manual_pump_status = false;
- lcd.setCursor(18,3);
- lcd.print("WY");
- delay(200);
- }
- if(action==1) {Manual_pump_status = true;
- lcd.setCursor(18,3);
- lcd.print("WL");
- delay(200);
- }
- if(action==4) // 0
- {
- lcd.setCursor(0,0);
- lcd.print("POMPA MANUALNA OK");
- delay(2000); // 0
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(1,0);
- lcd.print(LCD_string_1);
- menu.moveDown();
- }
- }
- } Keyboard_change=action;
- }
- /* __________________________ Termostat temperature 1 _______________________ */
- if (used.item.getName() == "USTAW TEMP.WL") // exactly the same string "temperature 1 "
- temperature_1 = MeniuFunkcija ("USTAW TEMP.WL", temperature_1, 99, -25, "OK");
- ///////////////////////////////////////////////////////////////////
- /* __________________________ Termostat temperature 2 _______________________ */
- if (used.item.getName() == "USTAW TEMP.WYL ") // exactly the same string "temperature 2 "
- temperature_2 = MeniuFunkcija ("USTAW TEMP.WYL", temperature_2, 99, -25, "OK");
- ///////////////////////////////////////////////////////////////////
- /* __________________________ Termostat status _______________________ */
- if (used.item.getName() == "STAN")
- {
- lcd.setCursor(0,0);
- lcd.write(7);
- lcd.setCursor(1,1);
- lcd.print("STAN-");
- if (Thermostat_status == 1) lcd.print("heating"); // heating
- if (Thermostat_status == 2) lcd.print("freezing"); // freezing
- if (Thermostat_status == 3) lcd.print("WYL"); // turned off
- int action=-1; delay(1000);
- while(action!=4)
- {
- Keyboard_change=-1;
- action=Read_keyboard(Key_Pin); //delay(300);
- if(Keyboard_change!=action)
- {
- if (action==1) {Thermostat_status++; if(Thermostat_status>3) Thermostat_status=1;
- lcd.setCursor(6,3);
- if (Thermostat_status == 1) lcd.print("GRZANIE "); // heating
- if (Thermostat_status == 2) lcd.print("freezing"); // freezing
- if (Thermostat_status == 3) lcd.print("WYL "); // turned off
- delay(200);}
- if(action==2) {Thermostat_status--;
- if(Thermostat_status<1) Thermostat_status=3;
- lcd.setCursor(6,3);
- if (Thermostat_status == 1) lcd.print("heating "); // heating
- if (Thermostat_status == 2) lcd.print("freezing"); // freezing
- if (Thermostat_status == 3) lcd.print("WYL "); // turned off
- delay(200);}
- if(action==4)
- {
- lcd.setCursor(0,0);
- lcd.print(">STAN OK");
- delay(2000);
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(1,0);
- lcd.print(LCD_string_1);
- menu.moveDown();
- }
- }
- } Keyboard_change=action;
- }
- }
- // --- Reakcja na wciÄŹĹĽËťniÄŹĹĽËťcie klawisza -----------------------------------------------------------------
- void menuChangeEvent(MenuChangeEvent changed) // funkcja klasy MenuBackend
- {
- if(changed.to.getName()==menu.getRoot())
- {
- InMenu =false;
- Serial.println("Now we are on MenuRoot");
- LCD_T_template();
- Temperature_Imaging();
- }
- // tak naprawdę to tylko tutaj przydaje się ów shortkey i służy przede wszystkim do wzbogacenia menu
- //o symbole strzałek w zależności co wybrano. Wszystko co tutaj się wyprawia jest pokazywane na LCD.
- //
- int c=changed.to.getShortkey(); // shortkey charge (1,2,3, or 4)
- lcd.clear(); // clear lcd
- lcd.setCursor(0,0);
- if(c==1) // If this menu Main contacts (shortkey = 1) are:
- {InMenu =true;
- //lcd.write(3); // Left arrow
- strcpy(LCD_string_1,changed.to.getName()); // Create a string in the first line
- lcd.print(LCD_string_1); // Display it
- //lcd.setCursor(15,0);lcd.write(4); // Right arrow
- //lcd.setCursor(0,1);lcd.write(5); // Down arrow
- //lcd.setCursor(15,1);lcd.write(5); // Down arrow
- }
- if(c==2) // if the submenu for the child - (shortkey = 2) are:
- {InMenu =true;
- //lcd.print("*"); // draw a star
- strcpy(LCD_string_2,changed.to.getName()); // create a string in the first line
- lcd.print(LCD_string_1); // print it
- //lcd.setCursor(15,0);lcd.print("*"); // draw a star
- //lcd.setCursor(0,1);lcd.write(6); // the second line and arrow return (arrowBack)
- lcd.print(changed.to.getName()); // display name of "child"
- //lcd.setCursor(15,1);lcd.write(7); // arrow up-down
- }
- if(c==3) // if the child has a child - (shortkey = 3) are:
- {InMenu =true;
- //lcd.print("*"); // draw a star
- strcpy(LCD_string_2,changed.to.getName()); // the name of the menu options to the variable line 2
- lcd.print(LCD_string_1); // and display the first line of
- //lcd.setCursor(15,0);lcd.print("*"); // draw a star
- //lcd.setCursor(0,1);lcd.write(6); // the second line and arrow arrowBack
- lcd.print(changed.to.getName()); // display the grandson of the second line
- //lcd.setCursor(15,1);lcd.write(4); // arrow to the right because they are the grandchildren
- }
- if(c==4) // if grandchild (shortkey = 4) are:
- {InMenu =true;
- //lcd.print("*"); // draw a star
- lcd.print(LCD_string_2); // in the first line of the display child (or parent grandchild)
- //lcd.setCursor(15,0);lcd.print("*"); // draw a star
- //lcd.setCursor(0,1);lcd.write(6); // the second line and arrow arrowBack
- lcd.print(changed.to.getName()); // display grandson
- //lcd.setCursor(15,1);lcd.write(7); // arrow up-down
- }
- }
- // --- 5 analog buttons keyboard scan version DFRobot --------------------------------------
- volatile int Read_keyboard(int analog)
- {
- int stan_Analog = analogRead(analog);delay(30);//Serial.println(stan_Analog);
- if (stan_Analog > 1000) return -1; // limit
- if (stan_Analog < 50) return 3; // right
- if (stan_Analog < 200) return 1; // up
- if (stan_Analog < 400) return 2; // down
- if (stan_Analog < 600) return 0; // left
- if (stan_Analog < 800) return 4; // OK
- return -1; // Not pressed
- }
- // ============================================================================================
- //
- void setup()
- {
- LoadConfig();
- /* ********************************************************* */
- pinMode(BackLight_Pin, OUTPUT);
- digitalWrite(BackLight_Pin,HIGH);
- analogWrite(BackLight_Pin,LCD_brightness*25);
- LCD_string_1=new char[20];
- LCD_string_2=new char[20];
- LCD_string_3=new char[20];
- LCD_string_4=new char[20];
- Serial.begin(9600);
- lcd.begin(20, 4);
- lcd.clear();
- lcd.createChar(3,arrowLeft); // LCD symbol left
- lcd.createChar(4,arrowRight);
- lcd.createChar(5,arrowDown);
- lcd.createChar(6,arrowBack);
- lcd.createChar(7,arrowUpDown);
- lcd.createChar(1,arrowUp);
- lcd.createChar(1, degree);
- lcd.setCursor(0,0);
- flashbacklight();
- lcd.print("STEROWNIK KOLEKTOROW");
- lcd.setCursor(0,1);
- lcd.print(" SLONECZNYCH ");
- lcd.setCursor(0,2);
- lcd.print(" PELNA ");
- lcd.setCursor(0,3);
- lcd.print(" CHATA 2016 ");
- delay(1000);
- lcd.clear();
- Collector_sensor.begin();
- Boiler_sensor.begin();
- Thermostat_sensor.begin();
- pinMode(13,OUTPUT);digitalWrite(13,LOW); // only for test
- pinMode(Relay_Collector,OUTPUT);pinMode(Relay_Thermostat,OUTPUT);
- digitalWrite(Relay_Collector,HIGH);digitalWrite(Relay_Thermostat,HIGH);
- menuSetup();
- // menu.moveUp();
- Temperature_measurements_1();
- LCD_T_template();
- // Temperature_Imaging();
- LCD_switching_on_Time = millis();
- temperature_measurement_time_1 = millis();
- }
- // setup()
- //************ END **************//
- // ************************ START void loop() *******************************
- void loop()
- {
- // measured temperature specified time intervals (Temperature_measurement_interval)
- /* +++++++++++++++++++++++++++ First level ++++++++++++++++++++++++++++++++++++ */
- if (millis() > temperature_measurement_time_1 ) {
- temperature_measurement_time_1 = millis() + Temperature_measurement_interval_1;
- Temperature_measurements_1();}
- // if the screen, without application of the button illuminates more than the tasks, backlight off
- if (millis()- LCD_switching_on_Time > The_LCD_light_Break) {
- analogWrite(BackLight_Pin, 0);
- pinMode(13,OUTPUT);digitalWrite(13,LOW); // only for test
- Backlighting = false;
- LCD_switching_on_Time = millis();}
- // When you press any key, the screen backlight is turned on when it is turned off
- if ((x != -1) && (Backlighting == false)){ analogWrite(BackLight_Pin,lcd_backlight*25);
- digitalWrite(13,HIGH); // only for test
- Backlighting = true;}
- // If the menu is inactive for some time, it returns to the main program
- //if ((x != -1) && (InMenu == true))Menu_time_spent_inactive = millis();
- // else {if (millis()- Menu_time_spent_inactive > inactive_menu)
- // menu.toRoot();
- //}
- x=Read_keyboard(Key_Pin);delay(30); // read the state of the keyboard:
- if(Keyboard_change!=x) // if there was a change in the state are:
- {
- switch(x) // check what was pressed
- {
- case 0: menu.moveRight();break; // If pressed, move it in the right menu to the right
- case 1: menu.moveUp();break; // Menu to top
- case 2: menu.moveDown();break; // Menu Down
- case 3: menu.moveLeft();break; // Menu to the left
- case 4: menu.use();break; // pressed OK, so jump to the function menuUseEvent (MenuUseEvend used)
- // This function is just serve our menu, check here
- // Which option is selected, and here we create the code to the event handler.
- }
- } Keyboard_change=x; //Assign the value of x variable amended so that the long pressing the
- // Same key did not result in the re-generation event.
- // Program responds to a change in the keyboard.
- // If you are not currently within the menu is of a continuous program
- if (InMenu == false){
- // time interval used for the LCD refresh
- if (millis() > LCD_Update_Time ) {
- LCD_Update_Time = millis() + LCD_Update_Interval;
- LCD_T_template();
- Temperature_Imaging();
- //#ifdef DEBUGds18b20
- //Serial.println("Temperate_measurement");
- //unsigned long start = millis();
- //#endif
- #ifdef DEBUGds18b20
- //unsigned long stop = millis();
- //Serial.print("Temperature measurement time: "); Serial.println(stop - start);
- Serial.print("K/ ");Serial.print(K);Serial.print(" B/ ");Serial.print(B);Serial.print(" T/ ");Serial.println(T);
- Serial.println("----");
- Serial.print("Thermostat_status- ");Serial.println(Thermostat_status);
- Serial.print("temperature_1- ");Serial.print(temperature_1);
- Serial.print(" temperature_2- ");Serial.println(temperature_2);
- Serial.println("----");
- Serial.print("Pump_power_on_difference- ");Serial.print(Pump_power_on_difference);
- Serial.print(" Pump_power_off_difference- ");Serial.println(Pump_power_off_difference);
- Serial.print("millis- ");Serial.println(millis()/1000);
- Serial.println(freeRam());
- #endif
- }
- }
- //------------------ collector pump and thermostat control -----------------------//
- if (millis() > Relay_switching_time )
- {
- Relay_switching_time=millis()+Relay_switching_interval;
- if (Manual_pump_status == true) {digitalWrite(Relay_Collector,LOW);}
- else{
- if (K-B>=Pump_power_on_difference) digitalWrite(Relay_Collector,LOW);
- if (K-B<=Pump_power_off_difference) digitalWrite(Relay_Collector,HIGH);
- }
- if (Thermostat_status == 1)
- {// If the heating mode (Thermostat_status = 1)
- if (T <= temperature_1) digitalWrite(Relay_Thermostat,LOW);
- if (T >= temperature_2) digitalWrite(Relay_Thermostat,HIGH);
- }
- if (Thermostat_status == 2)
- {// If the freezing mode (Thermostat_status = 2)
- if (T >= temperature_1) digitalWrite(Relay_Thermostat,LOW);
- if (T <= temperature_2) digitalWrite(Relay_Thermostat,HIGH);
- }
- if (Thermostat_status == 3)
- // If you do not need a second relay-mode off
- digitalWrite(Relay_Thermostat,HIGH);
- }
- }// === END ===========================================================
- void Temperature_measurements_1(){
- //____________________________ Start Sensor 1 _________________________________
- #ifdef SetWaitForConversionFALSE
- Collector_sensor.setWaitForConversion(false); // makes it async
- #endif
- Collector_sensor.requestTemperatures(); // Send the command to get temperatures
- K=Collector_sensor.getTempCByIndex(0);
- //_____________________________ Stop Sensor 1 ___________________________________
- //______________________ Start Sensor 3 ________________________________________
- #ifdef SetWaitForConversionFALSE
- Thermostat_sensor.setWaitForConversion(false); // makes it async
- #endif
- Thermostat_sensor.requestTemperatures(); // Send the command to get temperatures
- T=Thermostat_sensor.getTempCByIndex(0);
- //___________________ Stop Sensor 3 ______________________________________________
- //__________________________________________ Start Sensor 2 _____________________
- #ifdef SetWaitForConversionFALSE
- Boiler_sensor.setWaitForConversion(false); // makes it async
- #endif
- Boiler_sensor.requestTemperatures(); // Send the command to get temperatures
- B=Boiler_sensor.getTempCByIndex(0);
- //_____________________________________ Stop Sensor 2 ____________________________
- }
- void LCD_T_template(){
- // analogWrite(BackLight_Pin,LCD_brightness*25);
- lcd.setCursor(0,0);
- lcd.print("KOLEKTOR");
- lcd.setCursor(13,0);
- lcd.print((char)223);
- lcd.setCursor(15,0);
- lcd.print("R");
- lcd.setCursor(0,1);
- lcd.print("BOILER-A");
- lcd.setCursor(13,1);
- lcd.print((char)223);
- lcd.setCursor(0,2);
- lcd.print("BOILER-B");
- lcd.setCursor(13,2);
- lcd.print((char)223);
- lcd.setCursor(0,3);
- lcd.print("P-CO");
- }
- void Temperature_Imaging(){
- lcd.setCursor(9,0);
- lcd.print(K,1);
- if (K-B>0)
- {lcd.setCursor(16,0);
- lcd.print("+");
- lcd.print((K-B),1);}
- else {lcd.setCursor(16,0);
- lcd.print((K-B),1);}
- if (K-B>=Pump_power_on_difference)
- {lcd.setCursor(9,3);
- lcd.print("C");
- lcd.write(1);}
- if (K-B<=Pump_power_off_difference)
- {lcd.setCursor(9,3);
- lcd.print("P-CW");
- lcd.write(5);}
- lcd.setCursor(9,1);
- lcd.print(B,1);
- lcd.setCursor(9,2);
- lcd.print(T,1);//(int(K + 0.5));
- if (Thermostat_status == 1) {// If the heating mode (Thermostat_status = 1)
- if (T <= temperature_1)
- {lcd.setCursor(14,3);
- lcd.print("H");
- lcd.write(1);}
- if (T >= temperature_2)
- {lcd.setCursor(14,3);
- lcd.print("H");
- lcd.write(5);}
- }
- if (Thermostat_status == 2)
- {// If the freezing mode (Thermostat_status = 2)
- if (T >= temperature_1)
- {lcd.setCursor(14,3);
- lcd.print("F");
- lcd.write(1);}
- if (T <= temperature_2)
- {lcd.setCursor(14,3);
- lcd.print("F");
- lcd.write(5);}
- }
- if (Thermostat_status == 3)
- {lcd.setCursor(5,3);
- lcd.print("WYL");}
- }
- int MeniuFunkcija (String text_1, int Converted_Value, int Max_Value, int Min_Value, String text_2)
- {
- lcd.setCursor(0,0);lcd.write(7);
- lcd.setCursor(1,1);lcd.print(text_1); //("Nustatyta= ");
- lcd.setCursor(11,1);lcd.print( Converted_Value); // shows the current value
- int action=-1; delay(1000); //
- while(action!=4) //
- {
- Keyboard_change=-1;
- action=Read_keyboard(Key_Pin); //delay(300);
- if(Keyboard_change!=action)
- {
- if (action==1) { Converted_Value++;
- if( Converted_Value>Max_Value) Converted_Value=Max_Value;
- lcd.setCursor(11,1);
- if( Converted_Value<10) lcd.print(" ");
- lcd.print( Converted_Value);
- delay(200);}
- if(action==2) { Converted_Value--;
- if( Converted_Value<Min_Value) Converted_Value=Min_Value;
- lcd.setCursor(11,1);
- if( Converted_Value<10) lcd.print(" ");
- lcd.print( Converted_Value);
- delay(200);}
- if(action==4)
- {
- lcd.setCursor(0,0);
- lcd.print(text_2);
- delay(2000);
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(0,0);
- lcd.print("*");
- lcd.print(LCD_string_1);
- lcd.setCursor(19,0);
- lcd.print("*");
- menu.moveDown();
- }
- }
- } Keyboard_change=action; // Keyboard_change update, in order to react only Keyboard_change keyboard status
- // This is an important moment - while loop ends and turn the control to the main loop loop ()
- return Converted_Value; // Scan settings
- }
- boolean LoadConfig(){
- if((EEPROM.read(0) == 27) && (EEPROM.read(1) == 28) &&
- (EEPROM.read(2) == 13) && (EEPROM.read(3) == 18)) {
- if (EEPROM.read(4) == EEPROM.read(5)) Pump_power_on_difference = EEPROM.read(4);
- if (EEPROM.read(6) == EEPROM.read(7)) Pump_power_off_difference = EEPROM.read(6);
- if (EEPROM.read(8) == EEPROM.read(9)) temperature_1 = EEPROM.read(8);
- if (EEPROM.read(10) == EEPROM.read(11)) temperature_2 = EEPROM.read(10);
- if (EEPROM.read(12) == EEPROM.read(13)) Thermostat_status = EEPROM.read(12);
- if (EEPROM.read(14) == EEPROM.read(15)) LCD_brightness = EEPROM.read(14);
- return true;
- }
- return false;
- }
- // Write settings
- void SaveConfig(){
- EEPROM.write(0,27);
- EEPROM.write(1,28);
- EEPROM.write(2,13);
- EEPROM.write(3,18);
- EEPROM.write(4,Pump_power_on_difference);EEPROM.write(5,Pump_power_on_difference); //
- EEPROM.write(6,Pump_power_off_difference); EEPROM.write(7,Pump_power_off_difference); //
- EEPROM.write(8,temperature_1);EEPROM.write(9,temperature_1); //
- EEPROM.write(10,temperature_2); EEPROM.write(11,temperature_2); //
- EEPROM.write(12,Thermostat_status); EEPROM.write(13,Thermostat_status); //
- EEPROM.write(14,LCD_brightness); EEPROM.write(15,LCD_brightness); //
- }
- void flashbacklight() {
- digitalWrite(BackLight_Pin, LOW); delay(150);
- digitalWrite(BackLight_Pin, HIGH); delay(150);
- }
- ////////////////////////////
- MenuBackend.h
- /*
- ||
- || @file MenuBackend.h
- || @version 1.4
- || @author Alexander Brevig
- || @contact alexanderbrevig@gmail.com
- || @contribution Adrian Brzezinski adrb@wp.pl, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=vzhang
- ||
- || @description
- || | Provide an easy way of making menus
- || #
- ||
- || @license
- || | This library is free software; you can redistribute it and/or
- || | modify it under the terms of the GNU Lesser General Public
- || | License as published by the Free Software Foundation; version
- || | 2.1 of the License.
- || |
- || | This library is distributed in the hope that it will be useful,
- || | but WITHOUT ANY WARRANTY; without even the implied warranty of
- || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- || | Lesser General Public License for more details.
- || |
- || | You should have received a copy of the GNU Lesser General Public
- || | License along with this library; if not, write to the Free Software
- || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- || #
- ||
- */
- #ifndef MenuBackend_h
- #define MenuBackend_h
- /*
- A menu item will be a container for an item that is a part of a menu
- Each such item has a logical position in the hierarchy as well as a text and maybe a mnemonic shortkey
- */
- class MenuItem {
- public:
- MenuItem(const char* itemName, char shortKey='\0' ) : name(itemName), shortkey(shortKey) {
- before = right = after = left = 0;
- }
- //void use(){} //update some internal data / statistics
- inline const char* getName() const { return name; }
- inline const char getShortkey() const { return shortkey; }
- inline const bool hasShortkey() const { return (shortkey!='\0'); }
- inline void setBack(MenuItem *b) { back = b; }
- inline MenuItem* getBack() const { return back; }
- inline MenuItem* getBefore() const { return before; }
- inline MenuItem* getRight() const { return right; }
- inline MenuItem* getAfter() const { return after; }
- inline MenuItem* getLeft() const { return left; }
- MenuItem *moveBack() { return back; }
- MenuItem *moveUp() {
- if (before) { before->back = this; }
- return before;
- }
- MenuItem *moveDown() {
- if (after) { after->back = this; }
- return after;
- }
- MenuItem *moveLeft() {
- if (left) { left->back = this; }
- return left;
- }
- MenuItem *moveRight() {
- if (right) { right->back = this; }
- return right;
- }
- //default vertical menu
- MenuItem &add(MenuItem &mi) { return addAfter(mi); }
- MenuItem &addBefore(MenuItem &mi) {
- mi.after = this;
- before = &mi;
- if ( !mi.back ) mi.back = back;
- return mi;
- }
- MenuItem &addRight(MenuItem &mi) {
- mi.left = this;
- right = &mi;
- if ( !mi.back ) mi.back = back;
- return mi;
- }
- MenuItem &addAfter(MenuItem &mi) {
- mi.before = this;
- after = &mi;
- if ( !mi.back ) mi.back = back;
- return mi;
- }
- MenuItem &addLeft(MenuItem &mi) {
- mi.right = this;
- left = &mi;
- if ( !mi.back ) mi.back = back;
- return mi;
- }
- protected:
- const char* name;
- const char shortkey;
- MenuItem *before;
- MenuItem *right;
- MenuItem *after;
- MenuItem *left;
- MenuItem *back;
- };
- //no dependant inclusion of string or cstring
- bool menuTestStrings(const char *a, const char *b) {
- while (*a) { if (*a != *b) { return false; } b++; a++; }
- return true;
- }
- bool operator==(MenuItem &lhs, char* test) {
- return menuTestStrings(lhs.getName(),test);
- }
- bool operator==(const MenuItem &lhs, char* test) {
- return menuTestStrings(lhs.getName(),test);
- }
- bool operator==(MenuItem &lhs, MenuItem &rhs) {
- return menuTestStrings(lhs.getName(),rhs.getName());
- }
- bool operator==(const MenuItem &lhs, MenuItem &rhs) {
- return menuTestStrings(lhs.getName(),rhs.getName());
- }
- struct MenuChangeEvent {
- const MenuItem &from;
- const MenuItem &to;
- };
- struct MenuUseEvent {
- const MenuItem &item;
- };
- typedef void (*cb_change)(MenuChangeEvent);
- typedef void (*cb_use)(MenuUseEvent);
- class MenuBackend {
- public:
- MenuBackend(cb_use menuUse, cb_change menuChange = 0) : root("MenuRoot") {
- current = &root;
- cb_menuChange = menuChange;
- cb_menuUse = menuUse;
- }
- MenuItem &getRoot() {
- return root;
- }
- MenuItem &getCurrent() {
- return *current;
- }
- void moveBack() {
- setCurrent(current->getBack());
- }
- void moveUp() {
- setCurrent(current->moveUp());
- }
- void moveDown() {
- setCurrent(current->moveDown());
- }
- void moveLeft() {
- setCurrent(current->moveLeft());
- }
- void moveRight() {
- setCurrent(current->moveRight());
- }
- void use(char shortkey)
- {
- recursiveSearch(shortkey,&root);
- use();
- }
- void use() {
- //current->use();
- if (cb_menuUse) {
- MenuUseEvent mue = { *current };
- cb_menuUse(mue);
- }
- }
- void toRoot() {
- setCurrent( &getRoot() );
- }
- private:
- void setCurrent( MenuItem *next ) {
- if (next) {
- if (cb_menuChange) {
- MenuChangeEvent mce = { *current, *next };
- (*cb_menuChange)(mce);
- }
- current = next;
- }
- }
- void foundShortkeyItem(MenuItem *mi) {
- mi->setBack(current);
- current = mi;
- }
- char canSearch(const char shortkey, MenuItem *m) {
- if (m==0) { return 0; }
- else {
- if (m->getShortkey()==shortkey) {
- foundShortkeyItem(m);
- return 1;
- }
- return -1;
- }
- }
- void rSAfter(const char shortkey, MenuItem *m) {
- if (canSearch(shortkey,m)!=1) {
- rSAfter(shortkey, m->getAfter());
- rSRight(shortkey, m->getRight());
- rSLeft(shortkey, m->getLeft());
- }
- }
- void rSRight(const char shortkey, MenuItem *m) {
- if (canSearch(shortkey,m)!=1) {
- rSAfter(shortkey, m->getAfter());
- rSRight(shortkey, m->getRight());
- rSBefore(shortkey, m->getBefore());
- }
- }
- void rSLeft(const char shortkey, MenuItem *m) {
- if (canSearch(shortkey,m)!=1) {
- rSAfter(shortkey, m->getAfter());
- rSLeft(shortkey, m->getLeft());
- rSBefore(shortkey, m->getBefore());
- }
- }
- void rSBefore(const char shortkey, MenuItem *m) {
- if (canSearch(shortkey,m)!=1) {
- rSRight(shortkey, m->getRight());
- rSLeft(shortkey, m->getLeft());
- rSBefore(shortkey, m->getBefore());
- }
- }
- void recursiveSearch(const char shortkey, MenuItem *m) {
- if (canSearch(shortkey,m)!=1) {
- rSAfter(shortkey, m->getAfter());
- rSRight(shortkey, m->getRight());
- rSLeft(shortkey, m->getLeft());
- rSBefore(shortkey, m->getBefore());
- }
- }
- MenuItem root;
- MenuItem *current;
- cb_change cb_menuChange;
- cb_use cb_menuUse;
- };
- #endif
- ///////////////
- definitions.h
- /* @@@@@@@@@@@@@@@@@@@@@@ for testing @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
- #define SetWaitForConversionFALSE // accelerated DS18B20 temperature measurement
- #define DEBUGds18b20 // for temperature measurement testing
- /* ********************** Laikai *************************************** */
- unsigned long Menu_time_spent_inactive; //Meniu_praleistas_neaktyvus_laikas
- #define inactive_menu 20000
- unsigned long temperature_measurement_time_1 = 0;
- unsigned long Relay_switching_time = 0;
- unsigned long temperature_measurement_time_3 = 0;
- #define Temperature_measurement_interval_1 5000
- #define Relay_switching_interval 5000
- #
- unsigned long LCD_switching_on_Time;
- unsigned long The_LCD_light_Break = 600000;
- unsigned long LCD_Update_Time = 0;
- #define LCD_Update_Interval 5000
- /* ************** Keyboard variables ************************************* */
- volatile int Keyboard_change =-1; // Check or change the keyboard value
- volatile int x=-1; //
- volatile int stan_Analog; //
- /* ********** LCD description ******************* */
- byte lcd_backlight = 10; // lcd_backlight * 25 = MAX LCD backlight
- byte LCD_brightness = 10; // lcd_pasvietimas * 25 = MAX LCD apsviestumas
- boolean Backlighting = true; // mark on the screen backlight
- // The pump on indicator (arrow up)
- byte arrow_up[8]={ B00100,B01110,B11111,B00100,B00100,B00100,B00100,B00100};
- // Pump shut-off symbol (arrow in the bottom)
- byte arrow_down[8]={ B00100,B00100,B00100,B00100,B00100,B11111,B01110,B00100};
- /* ******************** Relay *********************************** */
- #define Relay_Collector A1 // Collector
- #define Relay_Thermostat A2 // Thermostas
- /* ************************** davikliai *********************** */
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire K_t(ONE_WIRE_BUS1); // Collector
- OneWire B_t(ONE_WIRE_BUS2); // Boiler
- OneWire T_t(ONE_WIRE_BUS3); // Thermostat
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature Collector_sensor(&K_t);
- DallasTemperature Boiler_sensor(&B_t);
- DallasTemperature Thermostat_sensor(&T_t);
- // variables to store the measured temperature values
- float K,B,T;
- byte Pump_power_on_difference = 5;
- byte Pump_power_off_difference = 3;
- boolean k_uzsalimas = false;
- boolean S_C_pump_manual_control = false; // Mark manual pump control
- /* ********** Thermostat variables ******************* */
- byte temperature_1 = 20;
- byte temperature_2 = 25;
- byte Thermostat_status =3;
- /* ********** Pump variables ******************* */
- boolean Manual_pump_status = false; //false- manual off, true- manual-on
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement