Room Management System – The Menu –
part 2
Today we start building the actual
menu. The structure isn't that much different as the first approach.
Let's start again with adding some
variables. There fore we jump in the variable section of our sketch
where we declare all the variables for the menu and user interface.
/////////////////////////////////////////////////////////////////////////// ///////////////////Menu and user interface///////////////////////////////// /////////////////////////////////////////////////////////////////////////// const byte btnMenu = 1; //defining the menu button – moves through the menu const byte btnSearch = 2; //defining the search button – moves through values const byte btnSelect = 3; //defining the select button – selects a menu or a value const byte btnNone = 0; //defining the non button pressed var int act_key_in = 0; //var holding the key related sensor reading //////////////////////////////////////////////////////////////////////////////////////////////// //>>>>>>>>>><<<addition starts here<<<<<<<<<<<<< byte menuOption = 0; //var to count current menu option const byte menuOptions = 21; //available menu options //>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<< //////////////////////////////////////////////////////////////////////////////////////////////// char buffer_M[20]; //var holding the menu strings retrieved from //the program memory //Storing some menu messages in the program memory prog_char msg_0[] PROGMEM = "Not Used"; prog_char msg_1[] PROGMEM = "Saving...."; prog_char msg_2[] PROGMEM = "Setup mode"; prog_char msg_3[] PROGMEM = "Starting...."; prog_char msg_4[] PROGMEM = "RMU 1.2.6"; //Creating the table for the stored menu messages PROGMEM const char *msg_table[] = { msg_0, msg_1, msg_2, msg_3, msg_4 }; ////////////////////////////////////////////////////////////////////////////////////// //>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<< ////////////////////////////////////////////////////////////////////////////////////// //storing some special char's in the program memory const byte char_table[] PROGMEM = { B01111110, //Arrow right B01111111 //Arrow left }; //storing the main menu points in the program memory prog_char menu_0[] PROGMEM = "Date/Time"; prog_char menu_1[] PROGMEM = "Sensitivity"; prog_char menu_2[] PROGMEM = "Room photo cut"; prog_char menu_3[] PROGMEM = "Room photo limit"; prog_char menu_4[] PROGMEM = "OS photo cut"; prog_char menu_5[] PROGMEM = "OS photo limit"; prog_char menu_6[] PROGMEM = "Room 1"; prog_char menu_7[] PROGMEM = "Room 2"; prog_char menu_8[] PROGMEM = "Room 3"; prog_char menu_9[] PROGMEM = "Room 4"; prog_char menu_10[] PROGMEM = "Room 5"; prog_char menu_11[] PROGMEM = "Room 6"; prog_char menu_12[] PROGMEM = "Room 7"; prog_char menu_13[] PROGMEM = "Room 8"; prog_char menu_14[] PROGMEM = "Room 9"; prog_char menu_15[] PROGMEM = "Room 10"; prog_char menu_16[] PROGMEM = "AC 1"; prog_char menu_17[] PROGMEM = "AC 2"; prog_char menu_18[] PROGMEM = "AC 3"; prog_char menu_19[] PROGMEM = "AC 4"; PROGMEM const char *menu_table[] = { menu_0, menu_1, menu_2, menu_3, menu_4, menu_5, menu_6, menu_7, menu_8, menu_9, menu_10, menu_11, menu_12, menu_13, menu_14, menu_15, menu_16, menu_17, menu_18, menu_19 }; /////////////////////////////////////////////////////////////////////////////////////////////////////// //>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<< /////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { //////////////Start Serial for Debugging/////////////////////
Please note, we have two additions in
the above section. First we add variables for a menu option counter
and a constant for available menu options.
Second addition is a table of binary
numbers representing special characters to be displayed on the LCD,
stored in the program memory, and all main menu points stored in the
program memory.
Ow we move down to the
read_act_button() function. There we need to add a small delay as
de-bounce time for the buttons.
int read_act_buttons(){ act_key_in = analogRead(1); //reading the sensor //>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<< delay(250); //button de-bounce delay //>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<< //Readings centred at 170 for menu //203 for search and 252 for select //Serial.print("Reading: "); //Serial.println(act_key_in); if(act_key_in < 160) return btnNone; //most likely result first
from here we move down to the
button_loop() function and there we replace the lcd.print statement
with another function call – selectMenu().
i//Checking for a pressed menu button void button_loop(){ //Serial.println("in button loop"); byte button = read_act_buttons(); //function call to read the buttons if(button == btnMenu){ //if button menu was pressed //>>>>>>>>>>>>>change starts here<<<<<<<<<<<<< selectMenu(); //function holding the actual menu //>>>>>>>>>>>>>change ends here<<<<<<<<<<<<< } }
Last for this post, we are building the
main menu section. Again, we move to the end of the sketch, where we
build the selectMenu() function. Apart from adding a few more
functions, changing the values, this will be the part where we will
be adding all the menu commands over the next few posts.
void selectMenu(){ byte button = 0; //var holding the button value menuOption = 1; //current menu option lcd.clear(); //clear screen //print the retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[0])))); while(menuOption <= menuOptions){ //loop through menu options button = read_act_buttons(); //check if button was pressed if(button == btnMenu){ //if it was btn menu menuOption++; //add 1 to menu option if(menuOption == 2){ //if it's menu option 2 lcd.clear(); //clear display //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[1])))); } if(menuOption == 3){ //if it's menu option 3 lcd.clear(); //clear display //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[2])))); } if(menuOption == 4){ //if it's menu option4 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[3])))); lcd.setCursor(0, 1); //set cursor to row 2 column 1 //print retrieved message string lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(msg_table[0])))); } if(menuOption == 5){ //if it's menu option 5 lcd.clear(); //clear display //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[4])))); } if(menuOption == 6){ //if it's menu option 6 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[5])))); lcd.setCursor(0, 1); //set cursor to row 2 column 1 //print retrieved message string lcd.print(strcpy_P(buffer, (char*)pgm_read_word(&(msg_table[0])))); } if(menuOption == 7){ //if it's menu option 7 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[6])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 8){ //if it's menu option 8 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[7])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 9){ //if it's menu option 9 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[8])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 10){ //if it's menu option 10 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[9])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 11){ //if it's menu option 11 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[10])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 12){ //if it's menu option 12 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[11])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 13){ //if it's menu option 13 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[12])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 14){ //if it's menu option 14 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[13])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 15){ //if it's menu option 15 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[14])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 16){ //if it's menu option 16 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[15])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 17){ //if it's menu option 17 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[16])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 18){ //if it's menu option 18 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[17])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 19){ //if it's menu option 19 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[18])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } if(menuOption == 20){ //if it's menu option 20 lcd.clear(); //clear lcd //print retrieved string on lcd lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(menu_table[19])))); lcd.setCursor(15, 0); //set cursor to row 2 column 1 //print retrieved message string lcd.write(pgm_read_byte(&char_table[0])); } } } }
Lets have a quick look what is
happening in this part of the select menu function. As we enter the
function we are at menu option 1 the following while loop keeps us in
the function until we moved through all main menu point. Now every
time we press the menu button 1 will be added to the menu option
counter bringing us to the next menu point until we moved through all
of them as jut mentioned.
No comments:
Post a Comment