нужна помощь в разъяснении работы библиотеки.
- Войдите на сайт для отправки комментариев
Добрый день!
Долго искал способ быстрого создания меню для ардуины и перепробовав доступные библиотеки меню остановился на MenuBackend.h.
все хорошо,но при ее использовании т.к не совсем понятен принцип работы возникли некоторые трудности.
например стоит задача изменять определенную переменную(например значение порога температуры)в подпункте2.
структура меню
Меню-Пункт -Подпункт1
-Подпункт2
если у меня один подпункт,то значения изменяются нормально.
но в случае ,если их 2 то у меня получается либо навигация по меню ,либо изменение только первого подпункта.
Обгладорить могу через веб-мани.
Вот ссылка на библиотеку.
http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip
вот ссылка на пример
http://www.coagula.org/content/pages/tutorial-manage-menu-and-lcd-display-arduino#comment-2294
вот человек описывает подобную проблему
001 | #include <Wire.h> |
002 | #include <Rtc_Pcf8563.h> |
003 | #include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig |
004 | #include <LiquidCrystal.h> //this library is included in the Arduino IDE |
005 |
006 | const int encoderPushButton = 45; // pin for the Enter button |
007 | const int buttonPinEsc = 42; // pin for the Esc button |
008 |
009 | int encoder0PinA = 2; |
010 | int encoder0PinB = 3; |
011 | int encoder0Pos = 0; |
012 | int encoder0PinALast = LOW; |
013 | int n = HIGH; |
014 |
015 | int menuRoot = true ; // A global flag. true = root (top) menu and display clock. false = don't display clock |
016 |
017 | int lastButtonPushed = 0; |
018 | int lastButtonEnterState = LOW; // the previous reading from the Enter input pin |
019 | int lastButtonEscState = LOW; // the previous reading from the Esc input pin |
020 | long lastEnterDebounceTime = 0; // the last time the output pin was toggled |
021 | long lastEscDebounceTime = 0; // the last time the output pin was toggled |
022 | long debounceDelay = 150; // the debounce time |
023 |
024 | LiquidCrystal lcd(7,8,9,10,11,12); |
025 |
026 | Rtc_Pcf8563 rtc; //init the real time clock |
027 |
028 | // Time and Date |
029 |
030 | byte sec; |
031 | byte min; |
032 | byte hour; |
033 | byte day; |
034 | byte weekday; |
035 | byte month; |
036 | byte year; |
037 | byte century; |
038 |
039 | //Menu variables i.e. whats in the menu |
040 | MenuBackend menu = MenuBackend(menuUsed,menuChanged); |
041 |
042 | //initialize menuitems |
043 | MenuItem menuSetClock = MenuItem( "menuSetClock" ); // 2nd menu item |
044 | MenuItem menuItemHr = MenuItem( "menuItemHr" ); // sub menu |
045 | MenuItem menuItemMin = MenuItem( "menuItemMin" ); //sub menu |
046 | MenuItem menuExit = MenuItem( "menuExit" ); // 6th menu item |
047 |
048 |
049 |
050 | void setup () |
051 | { |
052 | lcd.begin(16, 2); |
053 | pinMode(encoderPushButton, INPUT); |
054 | pinMode(buttonPinEsc, INPUT); |
055 | pinMode (encoder0PinA,INPUT); |
056 | pinMode (encoder0PinB,INPUT); |
057 |
058 |
059 |
060 | //How the menu will look. |
061 | lcd.setCursor(0,0); |
062 | menu.getRoot().addRight(menuSetClock); // set main menu items |
063 | menuSetClock.add(menuItemHr).addRight(menuItemMin); // add first sub menu then addright additional sub menues for that menu |
064 | menuExit.add(menuExit); |
065 | menu.toRoot(); |
066 | } |
067 |
068 |
069 |
070 | void loop () |
071 | { |
072 | if (menuRoot == true ) |
073 | { |
074 | lcd.setCursor(4, 0); |
075 | lcd.print(rtc.formatTime()); |
076 | lcd.setCursor(3, 1); |
077 | lcd.print(rtc.formatDate()); |
078 |
079 | } |
080 | readButtons(); //I splitted button reading and navigation in two procedures because |
081 | readEncoder(); |
082 | navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings) |
083 | { |
084 | } |
085 | } |
086 |
087 |
088 | void readButtons() //read buttons status |
089 | { |
090 | int reading; |
091 | int buttonEnterState=LOW; // the current reading from the Enter input pin |
092 | int buttonEscState=LOW; // the current reading from the input pin |
093 |
094 |
095 | //ENTER BUTTON DEBOUNCE |
096 |
097 | reading = digitalRead(encoderPushButton); // read the state of the switch into a local variable: |
098 |
099 | if (reading != lastButtonEnterState) // If the switch changed, due to noise or pressing: |
100 | { |
101 | lastEnterDebounceTime = millis(); // reset the debouncing timer |
102 | } |
103 | if ((millis() - lastEnterDebounceTime) > debounceDelay) // whatever the reading is at, it's been there for longerthan the debounce delay, so take it as the actual current state: |
104 | { |
105 | buttonEnterState=reading; |
106 | lastEnterDebounceTime=millis(); |
107 | } |
108 | lastButtonEnterState = reading; // save the reading. Next time through the loop, it'll be the lastButtonState: |
109 |
110 |
111 | //ESC BUTTON DEBOUNCE |
112 |
113 | reading = digitalRead(buttonPinEsc); // read the state of the switch into a local variable: |
114 |
115 | if (reading != lastButtonEscState) // If the switch changed, due to noise or pressing: |
116 | { |
117 | lastEscDebounceTime = millis(); // reset the debouncing timer |
118 | } |
119 | if ((millis() - lastEscDebounceTime) > debounceDelay) // whatever the reading is at, it's been there for longerthan the debounce delay, so take it as the actual current state: |
120 | { |
121 | buttonEscState = reading; |
122 | lastEscDebounceTime=millis(); |
123 | } |
124 | lastButtonEscState = reading; // save the reading. Next time through the loop, it'll be the lastButtonState: |
125 |
126 | //Records which button has been pressed |
127 | if (buttonEnterState==HIGH){ |
128 | lastButtonPushed=encoderPushButton; |
129 | } |
130 | else if (buttonEscState==HIGH){ |
131 | lastButtonPushed=buttonPinEsc; |
132 | } |
133 | else { |
134 | lastButtonPushed=0; |
135 | } |
136 | } |
137 |
138 |
139 | void readEncoder() //read the encoder and set lastButtonPushed to values used by navigateMenu() |
140 | { |
141 | n = digitalRead(encoder0PinA); // n is current state of encoder0PinA pin |
142 | if ((encoder0PinALast == HIGH) && (n == LOW)) // check if encoder0PinA pin has changed state |
143 | { |
144 | if (digitalRead(encoder0PinB) == LOW) |
145 | { |
146 | lastButtonPushed = 1; //if it has changed and its now low decrement encoder0Pos; |
147 | } |
148 | else |
149 | { |
150 | lastButtonPushed = 2; // if it has changed and its now high, increment encoder0Pos |
151 | } |
152 | } |
153 | encoder0PinALast = n; // set the variable holding the previous state to the value n read above |
154 | } |
155 |
156 |
157 | void menuChanged(MenuChangeEvent changed){ |
158 |
159 | MenuItem newMenuItem=changed.to; //get the destination menu |
160 |
161 | lcd.setCursor(0,0); //set the start position for lcd printing to the second row |
162 |
163 | if (newMenuItem.getName()==menu.getRoot()){ |
164 | lcd.clear(); |
165 | menuRoot = true ; |
166 | } |
167 | //MENU SET CLOCK |
168 | else if (newMenuItem.getName()== "menuSetClock" ){ |
169 | menuRoot = false ; |
170 | lcd.clear(); |
171 | lcd.print( "Set Clock" ); |
172 | } |
173 | else if (newMenuItem.getName()== "menuItemHr" ){ |
174 | lcd.clear(); |
175 | lcd.setCursor(0,1); |
176 | lcd.print( "Hr" ); |
177 | } |
178 | else if (newMenuItem.getName()== "menuItemMin" ){ |
179 | lcd.clear(); |
180 | lcd.setCursor(0,1); |
181 | lcd.print( "Min" ); |
182 | } |
183 | } |
184 |
185 |
186 | void menuUsed(MenuUseEvent used){ // **if you have a function call it here** |
187 | if ( used.item == menuItemHr ){ |
188 | lcd.clear(); //etc. |
189 | lcd.setCursor(0, 0); |
190 | lcd.print(rtc.formatTime()); |
191 | changeHour(); |
192 | } |
193 | else if ( used.item == menuItemMin ){ |
194 | lcd.clear(); //etc. |
195 | lcd.setCursor(0, 0); |
196 | lcd.print(rtc.formatTime()); |
197 | changeMin(); |
198 | } |
199 |
200 | if ( used.item == menuExit ){ |
201 | menu.toRoot(); //etc. |
202 | } |
203 | } |
204 |
205 |
206 | void navigateMenus() |
207 | { |
208 | MenuItem currentMenu=menu.getCurrent(); |
209 |
210 | switch (lastButtonPushed) |
211 | { |
212 | case encoderPushButton: // enter pin |
213 | if (!(currentMenu.moveDown())) //if the current menu has a child and enter has been pressed then navigate menu to item below |
214 | { |
215 | menu.use(); |
216 | } |
217 | else //otherwise, if menu has no child and enter has been pressed---- the current menu is used |
218 | { |
219 | menu.moveDown(); |
220 | } |
221 | break ; |
222 | case buttonPinEsc: |
223 | lcd.clear(); |
224 | menu.toRoot(); //back to main |
225 | break ; |
226 | case 1: //Encoder right/buttonPinRight --: |
227 | menu.moveRight(); |
228 |
229 | break ; |
230 | case 2: //Encoder left/buttonPinLeft ++: |
231 | menu.moveLeft(); |
232 | break ; |
233 | } |
234 | lastButtonPushed=0; //reset the lastButtonPushed variable |
235 | } |
236 |
237 |
238 | void changeMin(){ |
239 |
240 | //min=rtc.getMinute(); |
241 | if (digitalRead(encoderPushButton) == HIGH) |
242 | { |
243 | min = ++min % 60; // this is better than // min += 1;as it rolls overfrom 59 to 00 |
244 | //if (incrementMinuteButtonPressDectected) min = ++min % 60 // increment and rollover past 59 |
245 | //if (decrementMinuteButtonPressDectected) min = (--min + 60) % 60 // decrement and rollunder past 0 |
246 | } |
247 | rtc.setTime(hour, min,sec); |
248 | } |
249 |
250 | void changeHour(){ |
251 |
252 | // hour=rtc.getHour(); |
253 |
254 | if (digitalRead(encoderPushButton) == HIGH) |
255 | { |
256 | hour = ++hour % 24; // this is better than // hour += 1;as it rolls overfrom 59 to 00 |
257 | //if (incrementMinuteButtonPressDectected) hour = ++hour % 60 // increment and rollover past 59 |
258 | //if (decrementMinuteButtonPressDectected) hour = (--hour + 60) % 60 // decrement and rollunder past 0 |
259 | } |
260 | rtc.setTime(hour, min,sec); |
261 | rtc.setTime(sec, min, hour); |
262 | } |
263 |
264 |
265 | void changeDay(){ |
266 |
267 | // Yet to be done |
268 | if (digitalRead(encoderPushButton) == HIGH) |
269 | { |
270 | day = ++day % 32; // this is better than // day += 1;as it rolls overfrom 59 to 00 |
271 | //if (incrementMinuteButtonPressDectected) dayn = ++day % 31 // increment and rollover past 59 |
272 | //if (decrementMinuteButtonPressDectected) day = (--day + 31) % 31 // decrement and rollunder past 0 |
273 | } |
274 | rtc.setDate(day,weekday, month, century, year); |
275 | } |
и вот мой код
001 | include <DallasTemperature.h> //датчик температуры |
002 | #include <OneWire.h> //1 проводной интерфейс |
003 | #include <LiquidCrystal_I2C.h> //экран и2с |
004 | #include <Wire.h> // 1-проводной интерфейс |
005 | #include <MenuBackend.h> //меню |
006 | ///////////////////темп обозначения |
007 | #define ONE_WIRE_BUS 3 // номер пина датчик температуры даллас 18б20 |
008 | OneWire oneWire(ONE_WIRE_BUS); |
009 | DallasTemperature sensors(&oneWire); |
010 | /////////////////////////////****************назначение пинов*********************************** |
011 | //////////////////кнопки управления |
012 | const int buttonPinLeft = 9; // pin for the Up button |
013 | const int buttonPinRight = 7; // pin for the Down button |
014 | const int buttonPinEsc = 8; // pin for the Esc button |
015 | const int buttonPinEnter = 6; // pin for the Enter button |
016 | /////////////управление моторами и нагревателем |
017 | const int svet_pin =0 ; //пин света |
018 | const int nagr_pin =1 ; // пин нагрева |
019 | ////////////переменные для времени |
020 | long prevmicros = 0; //переменная для хранения значений таймера |
021 | int sek=0; //значение секунд |
022 | int minu=0; //значение минут |
023 | int chas=10; //значение часов |
024 | boolean counter= false ; // счетчик для полусекунд |
025 | |
026 | unsigned int Temp = 24; |
027 | int Hister =1; |
028 | int svetvkl = 10; |
029 | int svetvikl = 0; |
030 | int a = 0; //для гистерезиса |
031 | int b = 0; //для гистерезиса |
032 | int z = 0; //переменная температуры с датчика |
033 | |
034 | int menuRoot = true ; // условие вывода меню или осн.экрана |
035 | int lastButtonPushed = 0; |
036 |
037 |
038 | float temperature = 0.0; //переменная температуры |
039 | int lastButtonEnterState = LOW; // the previous reading from the Enter input pin |
040 | int lastButtonEscState = LOW; // the previous reading from the Esc input pin |
041 | int lastButtonLeftState = LOW; // the previous reading from the Left input pin |
042 | int lastButtonRightState = LOW; // the previous reading from the Right input pin |
043 |
044 |
045 | long lastEnterDebounceTime = 0; // the last time the output pin was toggled |
046 | long lastEscDebounceTime = 0; // the last time the output pin was toggled |
047 | long lastLeftDebounceTime = 0; // the last time the output pin was toggled |
048 | long lastRightDebounceTime = 0; // the last time the output pin was toggled |
049 | long debounceDelay = 300; // задержка клавиш |
050 |
051 | // Включение экрана и2с |
052 | LiquidCrystal_I2C lcd(0x27, 16, 2); |
053 |
054 | //Переменные меню ,здесь создается структура меню |
055 | MenuBackend menu = MenuBackend(menuUsed,menuChanged); |
056 | //initialize menuitems |
057 | MenuItem menu1Item1 = MenuItem( "Item1" ); |
058 | MenuItem menuItem1SubItem1 = MenuItem( "Item1SubItem1" ); |
059 | MenuItem menuItem1SubItem2 = MenuItem( "Item1SubItem2" ); |
060 | MenuItem menu1Item2 = MenuItem( "Item2" ); |
061 | MenuItem menuItem2SubItem1 = MenuItem( "Item2SubItem1" ); |
062 | MenuItem menuItem2SubItem2 = MenuItem( "Item2SubItem2" ); |
063 | MenuItem menu1Item3 = MenuItem( "Item3" ); |
064 |
065 | ///////////////////////////////****************установка значений программы************************ |
066 | void setup () |
067 | { |
068 | |
069 | ///////////////////////// состояние пинов |
070 | //кнопки управления |
071 | pinMode(buttonPinLeft, INPUT); |
072 | pinMode(buttonPinRight, INPUT); |
073 | pinMode(buttonPinEnter, INPUT); |
074 | pinMode(buttonPinEsc, INPUT); |
075 | //нагреватель,моторы |
076 | pinMode(svet_pin, OUTPUT); |
077 | pinMode(nagr_pin, OUTPUT); |
078 | // Start up the library(темп??) |
079 | |
080 | Serial .begin(9600); |
081 | sensors.begin(); |
082 | //включение экрана |
083 | lcd.init(); |
084 | lcd.backlight(); |
085 | //переходы в меню (установить для левой кнопки!!!!!!!!!!!!!!!!!!!!!!!! |
086 | menu.getRoot().add(menu1Item1); |
087 | //************** |
088 | menu1Item1.addRight(menu1Item2).addRight(menu1Item3).addRight(menu1Item1); |
089 | menu1Item1.addLeft(menu1Item3).addLeft(menu1Item2).addLeft(menu1Item1); |
090 | //************** |
091 | menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2).addRight(menuItem1SubItem1); |
092 | menuItem1SubItem1.addLeft(menuItem1SubItem2).addLeft(menuItem1SubItem1); |
093 | //************** |
094 | menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2).addRight(menuItem2SubItem1); |
095 | |
096 | menu.toRoot(); |
097 | /////////////////////////////приветствие/////////////////// |
098 | lcd.setCursor(3,0); |
099 | lcd.print( "Kontroller " ); |
100 | lcd.setCursor(6,1); |
101 | lcd.print( "v.1.0" ); |
102 | delay (4000); |
103 | lcd.clear(); |
104 |
105 | } // setup()... |
106 |
107 |
108 |
109 | void loop () |
110 | { |
111 | readButtons(); //считывание кнопок программа |
112 | navigateMenus(); //программа меню |
113 |
114 | ////////////////////////**************************часы*********************************////////// |
115 | if (micros() - prevmicros >=500000) |
116 | { prevmicros = micros(); //принимает значение каждые полсекунды lcd.print(":"); counter=!counter; |
117 | if (counter== false ) |
118 | { sek++; //переменная секунда + 1 |
119 | lcd.setCursor(2,0); |
120 |
121 | } |
122 | else |
123 | { |
124 | lcd.setCursor(2,0); |
125 | lcd.print( " " ); |
126 | } |
127 | if (sek>59) //если переменная секунда больше 59 ... |
128 | { |
129 | sek=0; //сбрасываем ее на 0 |
130 | minu++; //пишем +1 в переменную минута |
131 | } |
132 | if (minu>59) //если переменная минута больше 59 ... |
133 | { |
134 | minu=0; //сбрасываем ее на 0 |
135 | chas++; //пишем +1 в переменную час |
136 | } |
137 | if (chas>23) //если переменная час больше 23 ... |
138 | { |
139 | chas=0; //сбрасываем ее на 0 |
140 | } |
141 | } |
142 | |
143 | ///////////////////////////*********************** основной экран с выводом параметров************************* |
144 | if (menuRoot == true ) //задание выполнения считывания датчиков в основном выводе(без меню) |
145 | { |
146 | ///////////////////////////////////////время//////////////////////////////// |
147 | lcd.setCursor(0,0); //выводим значение часов |
148 | if (chas>=0 && chas<10) { |
149 | lcd.print( "0" ); |
150 | lcd.print(chas);} //количество часов |
151 | else |
152 | {lcd.print(chas); |
153 | lcd.setCursor(3,0);} //выводим значение минут |
154 | if (minu>=0 && minu<10) { |
155 | lcd.print( "0" ); |
156 | lcd.print(minu);} //количество минут |
157 | else |
158 | {lcd.print(minu);} |
159 | ////////////////////////////////////////датчик температуры///////////////////// |
160 | sensors.requestTemperatures(); |
161 | z = sensors.getTempCByIndex(0); |
162 | lcd.setCursor(0,1); |
163 | lcd.print ( "T=" ); |
164 | lcd.print (z); |
165 | lcd.print ( "C" ); |
166 | /////////////////////////////////////показание работы тем-ры///////////////////// |
167 | lcd.setCursor(7,1); |
168 | lcd.print ( "Nagr:" ); |
169 | if (z <= b){ |
170 | lcd.setCursor(12,1); |
171 | lcd.print ( "ON " ); |
172 | } |
173 | else |
174 | if (z >= a){ |
175 | lcd.setCursor(12,1); |
176 | lcd.print ( "OFF" ); |
177 | } |
178 | ///////////////////////////////////показание работы света//////////////////////// |
179 | lcd.setCursor(7,0); |
180 | lcd.print ( "Svet:" ); |
181 | if (svetvkl == chas){ |
182 | lcd.setCursor(12,0); |
183 | lcd.print ( "ON " ); |
184 | } |
185 | else |
186 | if (svetvkl != chas){ |
187 | lcd.setCursor(12,0); |
188 | lcd.print ( "OFF" );} |
189 | //////////////////////////////////////////////////*********************действия контроллера в зависимости от датчиков******************* |
190 | /////////////////////////////////действие температуры/////////////////////////////////// |
191 | |
192 | a = Temp+Hister; |
193 | b = Temp-Hister; |
194 | if (z <= b){ |
195 | digitalWrite(nagr_pin, HIGH); |
196 | } |
197 | else |
198 | if (z >= a){ |
199 | digitalWrite(nagr_pin, LOW);} |
200 | //////////////////////////////действие света от часов/////////////////////////////////// |
201 | if (svetvkl == chas) |
202 | { |
203 | digitalWrite(svet_pin, HIGH); |
204 | Serial .print ( "ne vklycheno" ); |
205 | } |
206 | else if (svetvikl == minu) |
207 | { |
208 | digitalWrite(svet_pin, HIGH); |
209 | Serial .print ( "22222222222222" ); |
210 | } |
211 | //loop()... |
212 | } |
213 | } |
214 |
215 | void menuChanged(MenuChangeEvent changed){ |
216 | |
217 | MenuItem newMenuItem=changed.to; //get the destination menu |
218 | |
219 | lcd.setCursor(0,1); //set the start position for lcd printing to the second row |
220 | |
221 | if (newMenuItem.getName()==menu.getRoot()){ |
222 | menuRoot = true ; |
223 | } else if (newMenuItem.getName()== "Item1" ){ |
224 | menuRoot = false ; |
225 | lcd.setCursor(0,0); |
226 | lcd.print( " Main Menu " ); |
227 | lcd.setCursor(0,1); |
228 | lcd.print( "1)Temperatura " ); |
229 | } else if (newMenuItem.getName()== "Item1SubItem1" ){ |
230 | lcd.setCursor(0,0); |
231 | lcd.print( "1)Temperatura " ); |
232 | lcd.setCursor(0,1); |
233 | lcd.print( "Temp. podderzh." ); |
234 | } else if (newMenuItem.getName()== "Item1SubItem2" ){ |
235 | lcd.setCursor(0,0); |
236 | lcd.print( "1)Temperatura " ); |
237 | lcd.setCursor(0,1); |
238 | lcd.print( "Histerezis" ); |
239 | } else if (newMenuItem.getName()== "Item2" ){ |
240 | lcd.setCursor(0,0); |
241 | lcd.print( " Main Menu " ); |
242 | lcd.setCursor(0,1); |
243 | lcd.print( "2)Svet " ); |
244 | } else if (newMenuItem.getName()== "Item2SubItem1" ){ |
245 | lcd.setCursor(0,0); |
246 | lcd.print( "2)Svet " ); |
247 | lcd.setCursor(0,1); |
248 | lcd.print( "Vremya vklychenia" ); |
249 | } else if (newMenuItem.getName()== "Item2SubItem2" ){ |
250 | lcd.setCursor(0,0); |
251 | lcd.print( "2)Svet " ); |
252 | lcd.setCursor(0,1); |
253 | lcd.print( "Vremya vIklychenia" ); |
254 | } else if (newMenuItem.getName()== "Item3" ){ |
255 | lcd.setCursor(0,0); |
256 | lcd.print( " Main Menu " ); |
257 | lcd.setCursor(0,1); |
258 | lcd.print( "Item3 " ); |
259 | } |
260 | } |
261 |
262 |
263 | void menuUsed(MenuUseEvent used){ |
264 | lcd.setCursor(0,0); |
265 | lcd.print( "You used " ); |
266 | lcd.setCursor(0,1); |
267 | lcd.print(used.item.getName()); |
268 | |
269 | menu.toRoot(); //back to Main |
270 | } |
271 |
272 |
273 | void readButtons(){ //read buttons status |
274 | int reading; |
275 | int buttonEnterState=LOW; // the current reading from the Enter input pin |
276 | int buttonEscState=LOW; // the current reading from the input pin |
277 | int buttonLeftState=LOW; // the current reading from the input pin |
278 | int buttonRightState=LOW; // the current reading from the input pin |
279 |
280 | //Enter button |
281 | // read the state of the switch into a local variable: |
282 | reading = digitalRead(buttonPinEnter); |
283 |
284 | // check to see if you just pressed the enter button |
285 | // (i.e. the input went from LOW to HIGH), and you've waited |
286 | // long enough since the last press to ignore any noise: |
287 | |
288 | // If the switch changed, due to noise or pressing: |
289 | if (reading != lastButtonEnterState) { |
290 | // reset the debouncing timer |
291 | lastEnterDebounceTime = millis(); |
292 | } |
293 | |
294 | if ((millis() - lastEnterDebounceTime) > debounceDelay) { |
295 | // whatever the reading is at, it's been there for longer |
296 | // than the debounce delay, so take it as the actual current state: |
297 | buttonEnterState=reading; |
298 | lastEnterDebounceTime=millis(); |
299 | } |
300 | |
301 | // save the reading. Next time through the loop, |
302 | // it'll be the lastButtonState: |
303 | lastButtonEnterState = reading; |
304 | |
305 |
306 | //Esc button |
307 | // read the state of the switch into a local variable: |
308 | reading = digitalRead(buttonPinEsc); |
309 |
310 | // check to see if you just pressed the Down button |
311 | // (i.e. the input went from LOW to HIGH), and you've waited |
312 | // long enough since the last press to ignore any noise: |
313 | |
314 | // If the switch changed, due to noise or pressing: |
315 | if (reading != lastButtonEscState) { |
316 | // reset the debouncing timer |
317 | lastEscDebounceTime = millis(); |
318 | } |
319 | |
320 | if ((millis() - lastEscDebounceTime) > debounceDelay) { |
321 | // whatever the reading is at, it's been there for longer |
322 | // than the debounce delay, so take it as the actual current state: |
323 | buttonEscState = reading; |
324 | lastEscDebounceTime=millis(); |
325 | } |
326 | |
327 | // save the reading. Next time through the loop, |
328 | // it'll be the lastButtonState: |
329 | lastButtonEscState = reading; |
330 | |
331 | |
332 | //Down button |
333 | // read the state of the switch into a local variable: |
334 | reading = digitalRead(buttonPinRight); |
335 |
336 | // check to see if you just pressed the Down button |
337 | // (i.e. the input went from LOW to HIGH), and you've waited |
338 | // long enough since the last press to ignore any noise: |
339 | |
340 | // If the switch changed, due to noise or pressing: |
341 | if (reading != lastButtonRightState) { |
342 | // reset the debouncing timer |
343 | lastRightDebounceTime = millis(); |
344 | } |
345 | |
346 | if ((millis() - lastRightDebounceTime) > debounceDelay) { |
347 | // whatever the reading is at, it's been there for longer |
348 | // than the debounce delay, so take it as the actual current state: |
349 | buttonRightState = reading; |
350 | lastRightDebounceTime =millis(); |
351 | } |
352 | |
353 | // save the reading. Next time through the loop, |
354 | // it'll be the lastButtonState: |
355 | lastButtonRightState = reading; |
356 | |
357 | |
358 | //Up button |
359 | // read the state of the switch into a local variable: |
360 | reading = digitalRead(buttonPinLeft); |
361 |
362 | // check to see if you just pressed the Down button |
363 | // (i.e. the input went from LOW to HIGH), and you've waited |
364 | // long enough since the last press to ignore any noise: |
365 | |
366 | // If the switch changed, due to noise or pressing: |
367 | if (reading != lastButtonLeftState) { |
368 | // reset the debouncing timer |
369 | lastLeftDebounceTime = millis(); |
370 | } |
371 | |
372 | if ((millis() - lastLeftDebounceTime) > debounceDelay) { |
373 | // whatever the reading is at, it's been there for longer |
374 | // than the debounce delay, so take it as the actual current state: |
375 | buttonLeftState = reading; |
376 | lastLeftDebounceTime=millis();; |
377 | } |
378 | |
379 | // save the reading. Next time through the loop, |
380 | // it'll be the lastButtonState: |
381 | lastButtonLeftState = reading; |
382 |
383 | //records which button has been pressed |
384 | if (buttonEnterState==HIGH){ |
385 | lastButtonPushed=buttonPinEnter; |
386 |
387 | } else if (buttonEscState==HIGH){ |
388 | lastButtonPushed=buttonPinEsc; |
389 |
390 | } else if (buttonRightState==HIGH){ |
391 | lastButtonPushed=buttonPinRight; |
392 |
393 | } else if (buttonLeftState==HIGH){ |
394 | lastButtonPushed=buttonPinLeft; |
395 |
396 | } else { |
397 | lastButtonPushed=0; |
398 | } |
399 | } |
400 |
401 | void navigateMenus() ///////////////навигация по меню |
402 | { |
403 | MenuItem currentMenu=menu.getCurrent(); |
404 | |
405 | switch (lastButtonPushed){ |
406 | |
407 | //////////////////////выбор кнопки********************////////////////////////////////// |
408 | case buttonPinEnter: |
409 | ////////////температура****************************** |
410 | if (currentMenu.getName() == "Item1SubItem1" ){ //otherwise, if menu has no child and has been pressed enter the current menu is used |
411 | lcd.clear(); |
412 | lcd.setCursor(0,0); |
413 | lcd.print( "Temperature= " ); |
414 | lcd.print(Temp); |
415 | lcd.setCursor(0,1); |
416 | lcd.print ( "Ustanovite znach" );} |
417 | else if (currentMenu.getName() == "Item1SubItem2" ){ |
418 | lcd.clear(); |
419 | lcd.setCursor(0,0); |
420 | lcd.print( "Histerezis= " ); |
421 | lcd.print(Hister); |
422 | lcd.setCursor(0,1); |
423 | lcd.print ( "Ustanovite znach" ); |
424 | } |
425 | ////////////свет****************************** |
426 | else if (currentMenu.getName() == "Item2SubItem1" ) |
427 | {lcd.clear(); |
428 | lcd.setCursor(0,0); |
429 | lcd.print( "Vremya = " ); |
430 | lcd.print (chas); |
431 | lcd.print( ":" ); |
432 | lcd.print(minu); |
433 | lcd.setCursor(0,1); |
434 | lcd.print ( "Ustanovite znach" ); |
435 | } |
436 | else |
437 | lcd.clear(); |
438 | menu.moveDown(); |
439 | break ; |
440 | //////////////////выход*******************************///////////////////////////////////// |
441 | case buttonPinEsc: |
442 | lcd.clear(); |
443 | menu.toRoot(); //back to main |
444 | break ; |
445 | ////////////////////вправо*************************///////////////////////////////////////// |
446 | case buttonPinRight: //вправо |
447 | ////////////температура****************************** |
448 | if (currentMenu.getName() == "Item1SubItem1" ) // In setting the number of meals |
449 | { |
450 | Temp=Temp+1; //if Right button is press Meals+1 |
451 | lcd.clear(); |
452 | lcd.setCursor(1,0); |
453 | lcd.print( "Ustanivite znachenie" ); |
454 | lcd.setCursor(0,1); |
455 | lcd.print(Temp);} |
456 | else if (currentMenu.getName() == "Item1SubItem2" ) // In setting the number of meals |
457 | { |
458 | Hister=Hister+1; //if Right button is press Meals+1 |
459 | lcd.clear(); |
460 | lcd.setCursor(1,0); |
461 | lcd.print( "Ustanivite znachenie" ); |
462 | lcd.setCursor(0,1); |
463 | lcd.print(Hister); |
464 | } |
465 | ////////////свет****************************** |
466 | else if (currentMenu.getName() == "Item2SubItem1" ) // In setting the number of meals |
467 | { |
468 | svetvkl=svetvkl+1; //if Right button is press Meals+1 |
469 | lcd.clear(); |
470 | lcd.setCursor(1,0); |
471 | lcd.print( "Ustanivite znachenie" ); |
472 | lcd.setCursor(0,1); |
473 | lcd.print(svetvkl); |
474 | } |
475 | else if (currentMenu.getName() == "Item2SubItem2" ) // In setting the number of meals |
476 | { |
477 | svetvikl=svetvikl+1; //if Right button is press Meals+1 |
478 | lcd.clear(); |
479 | lcd.setCursor(1,0); |
480 | lcd.print( "Ustanivite znachenie" ); |
481 | lcd.setCursor(0,1); |
482 | lcd.print(svetvikl); |
483 | } |
484 | |
485 | menu.moveRight(); |
486 | break ; |
487 | |
488 | ////////////влево****************************/////////////////////////////////////////////// |
489 | case buttonPinLeft: ///влево |
490 | if (currentMenu.getName() == "Item1SubItem2" ) // In setting the number of meals |
491 | { |
492 | Hister=Hister-1; //if Right button is press Meals+1 |
493 | lcd.clear(); |
494 | lcd.setCursor(1,0); |
495 | lcd.print( "Ustanivite znachenie" ); |
496 | lcd.setCursor(0,1); |
497 | lcd.print(Hister); |
498 | } |
499 | else if (currentMenu.getName() == "Item1SubItem1" ) // In setting the number of meals |
500 | { |
501 | Temp=Temp-1; //if Right button is press Meals+1 |
502 | lcd.clear(); |
503 | lcd.setCursor(1,0); |
504 | lcd.print( "Ustanivite znachenie" ); |
505 | lcd.setCursor(0,1); |
506 | lcd.print(Temp); |
507 | } |
508 | ////////////свет****************************** |
509 | else if (currentMenu.getName() == "Item2SubItem1" ) // In setting the number of meals |
510 | { |
511 | svetvkl=svetvkl-1; //if Right button is press Meals+1 |
512 | lcd.clear(); |
513 | lcd.setCursor(1,0); |
514 | lcd.print( "Ustanivite znachenie" ); |
515 | lcd.setCursor(0,1); |
516 | lcd.print(svetvkl); |
517 | } |
518 | else if (currentMenu.getName() == "Item2SubItem2" ) |
519 | { |
520 | svetvikl=svetvikl-1; //if Right button is press Meals+1 |
521 | lcd.clear(); |
522 | lcd.setCursor(1,0); |
523 | lcd.print( "Ustanivite znachenie" ); |
524 | lcd.setCursor(0,1); |
525 | lcd.print(svetvikl); |
526 | } |
527 | |
528 | menu.moveLeft(); |
529 | break ; |
530 | |
531 | } |
532 | |
533 | lastButtonPushed=0; //reset the lastButtonPushed variable |
534 | } |
Обгладорить могу через веб-мани.
жуть какая...
предлагайте через что.
и да,там буква Т потерялась.
если надо распишу код,что и где.
...там буква Т потерялась....
Боюсь спросить - где?
Да,точно.
вообще с кодом голова не работает.
Там должно быть "отблагодарю".
а по делу что?
можно удалять.
проблема решена