Hi, I'm new to Arduino and I didn't use C++ since I finished my uni 10 years ago.
I have an Arduino Micro and 16x2 I2C LCD. I try to create a menu (6 positions at the moment). Each but last position will have own set of options (in brackets below), example:
SETTINGS
1.Language (Eng, ..., ...)
2.Temp.scale (*C, *F, K)
3.Scr.format (Row, Col)
4.Sensor 1 (LM35, PT100, ...)
5.Sensor 2 (LM35, PT100, ...)
6.Exit // back to main display
I want to use 3 push buttons (I know would be easier with 4) to control menu and options for each setting. Button1 (-/down), button2 (+/up), button3 (enter/exit).
At the moment I have menu done. For now, each menu line as separate function that is called with own parameters in 'switch(menu)..case'. I can scroll down/up using buttons. '6.Exit' works when I press button3.
My problem is, let's say I want to change '1.Language' from 'Eng' to another. I scroll to menu position '1.' using (-/down) (+/up) buttons and press (enter/exit) button once. It displays cursor and I can scroll through all language options with (-/down) (+/up) buttons. Problem is with code to save chosen option and exit to scroll 'Settings' menu again. I tried few different ways to do that. Here are 3 different ways how it works, depending on code I use.
- I can display cursor, enter and scroll options, but then I'm stuck scrolling language options, can't save chosen option and exit to scroll menu again; or
- I can display cursor, enter and scroll options, but to save and exit I have to press (enter/exit) and either (-/down) or (+/up) button, but that scrolls menu at same time; or
- I have to hold (enter/exit) button and press either (-/down) or (+/up) button to change option, but can't enter options to display cursor and scroll them.
Here is part of code I'm working on and declaration of variables. I display two menu lines at once (i.e. '1.(...)' and '2.(...)) but only top one can have options accessed and changed at any given time.
For code testing I temporarily display on LCD values of concerned variables, to see what values they have at a time. Anyway, can find solution yet.
Can you please point me in the right direction how to exit scrolling options and go back to scroll menu? Do I need some more variables or I have to find right combination of those I already use?
Thank you, Amy
bool down_btn = 0, // state of button '-/down' (0-released, 1-pressed)
up_btn = 0, // state of button '+/up'
enter_btn = 0, // state of button 'Enter/Exit'
subMenu = 0, // menu option (0-not entered, 1-entered)
start = 0, // 'Start' screen (0-not displayed, 1-displayed)
enter_btn_last_state = 0;
int menu = 0; // controls which menu line is displayed on top LCD line
void loop()
{
if (start == 0) // then display 'Settings' screen (for now, will be welcome screen)
{
screen4_1();
start = 1;
}
down_btn = digitalRead(12);
up_btn = digitalRead(11);
enter_btn = digitalRead(10);
delay(75);
if (enter_btn == 1 && enter_btn_last_state == 0) enter_btn_last_state = 1;
switch (menu) // menu SETTINGS
{
case 0:
if (down_btn == 1 && subMenu == 0) // down (menu 1. & 2.)
{
clrscr();
screen4_2(0, 'E');
screen4_3(1, 'C');
menu = 1;
}
// stops carrying enter button state to other menus
if (enter_btn_last_state == 1) enter_btn_last_state = 0;
break;
case 1: // menu 1.Language
if (up_btn == 1 && subMenu == 0) // up ('Settings' screen)
{
clrscr();
screen4_1();
menu = 0;
}
if (down_btn == 1 && subMenu == 0) // down (menu 2. & 3.)
{
clrscr();
screen4_3(0, 'C');
screen4_4(1, 'R');
menu = 2;
}
if (enter_btn_last_state == 1) // button 'enter' pressed, enter options
{
lcd.setCursor(15, 0);
lcd.blink_on();
subMenu = 1;
if (down_btn == 1) // button '-/down' pressed, show previous option
{
lcd.home();
screen4_2(0, 'P');
}
if (up_btn == 1) // button '+/up' pressed, show next option
{
lcd.home();
screen4_2(0, 'E');
}
// without this section I can press (enter/exit) button to display cursor and
// press (-/down) or (+/up) to change option but obviuosly can't exit back to
// scrolling menu.
// I tried few different parameter for 'if' but can't find the right one yet
/*
if (enter_btn == 1 && enter_btn_last_state == 1 && subMenu == 1)
{
lcd.blink_off();
enter_btn_last_state = 0;
subMenu = 0;
}
*/
break;
case 2:
(...)
break;