Need urgent help!! Please guide.  - Page 1
Forum section: electronics / general

author: radhika
author_rank: Regular Contributor
timestamp: June 23, 2019, 05:08:29 am
content: Hello, I am working on menu code and I want to do switching ON the LED by using push button which then select the desired menu on OLED SSD1306There are three button, up down and select which is connected to b5, b6, b7 pin and I want output at bo, b1, b2. Please look to the image added in main text. Now how do I switch ON the led when I select desired menu on OLED and that time other 2 led is off.The code is :Code:void discharging_USB() {SSD1306_ClearDisplay();cursor(starting_row);SSD1306_GotoXY(starting_col, starting_row);SSD1306_PutC("1. 5V 1A");delay_ms(10);start_row = starting_row;start_row +=1;SSD1306_GotoXY(starting_col, start_row);SSD1306_PutC("2. 5V 2A");delay_ms(10);start_row +=1;SSD1306_GotoXY(starting_col, start_row);SSD1306_PutC("3. 5V 3A");delay_ms(10);start_row +=1;SSD1306_GotoXY(starting_col, start_row);SSD1306_PutC("4. Back");delay_ms(100);if (m2==1 && row_Ind == 1 && button_select==0){m2++;if( m2==2 && row_Ind ==1){output_low(PIN_B0);output_low(PIN_B1);output_high(PIN_B2);button_select==1;}else if (m2==2 && row_Ind ==2){output_high(PIN_B0);output_low(PIN_B1);output_low(PIN_B2);button_select==1;}else if (m2==3 && row_Ind ==3){output_high(PIN_B0);output_low(PIN_B1);output_low(PIN_B2);button_select==1;}else{output_low(PIN_B0);output_low(PIN_B1);output_high(PIN_B2);button_select==1;}}}I don't know how do I frame this loop.m2==1 mean that there is menu which is considered as m2==1for eg:1. ABC2. DEF3. GHIand when m2==2 mean that when we go to sub menu of 1. ABC 2. DEF 3. GHIand I wanna select sub menu of ABC, which is1. 5V 1A2. 5V 2A3. 5V 3A4. BackIf I just go with m2==2 then program get confuse with which menu it will go with, either ABC, DEF or GHIGuide me what syntax do I use in it..Awaiting your response.Here is the image link.https://postimg.cc/68js4q64

author: mariush
author_rank: Super Contributor
timestamp: June 23, 2019, 06:18:37 am
content: If you're gonna have multiple submenus, I would suggest making a sort of "universal" menu.Put all your texts into an array with let's say up to 256 entries. This would also allow you to put the texts into EEPROM or as constant strings... you could have something like this:Have a char/unsigned byte variable called " menu" - keeps track in which menu you are.Have an in_menu variable which tells you if you're in a menu or not , can be boolean, char etcHave a char/unsigned byte variable called selection (keeps track over which line you are using your up/down keys)If user presses OK and menu visible (in_menu is true) , it means you selected something so have a switch or if to check stuff.ex. not actual code , sort of pseudocode but should get you an idea....strings : "Menu" // placeholder text, never shows up on screenstrings : "USB Discharge >"strings : "1. 5V 1A"strings : "2. 5V 2A"strings : "3. 5V 3A"strings : "4. Back"...strings : "Exit menu"function SetMenu(parent) {menu_changed = FALSE;if (parent==0) {menu = 0; // Homeoptions = 2; // number of option lines to display (needed by the "Show Menu" functionoptions = 1; // lD of text line "USB Discharge >";options = 255; // lD of text line "Exit menu";menu_changed=TRUE;}if (parent ==1) { // hit "Discharge USB"menu = 1;options= 4;options= 2;options= 3;options= 4;options= 5;}if menu_changed==TRUE) {selection = 1;RefreshMenu();}}function RefreshMenu () {// displays the menu lines using the strings[], options[], menu and selection variables this way you could highlight lines when going up down and so onSSD1306_ClearDisplay();cursor(starting_row);if (options>0) { // at least one option to display// maybe change this so instead of trying to print only the exact number of lines, always try to print 4 lines and if the actual options are fewer,// print some spaces to clear the line if there was another menu before// also you could add some smarts here in case you have more than 4 options in a menu, for example if options > 4 && selection > 4, then show only lines options-4 .. options ..so you add an offset variablefor (i=0;i<=options;i++) {SSD1306_GotoXY(starting_col, starting_row+i);// if (selection==i+1) { // this line is selected with up/down, maybe invert line if you want here }SSD1306_PutC(strings[options[i+1]]);delay_ms(10);}}}function ExitMenu() {// reset screen , show the regular info , whatever}if ( button_ok_pressed==true) {// special case , if you have no menu displayed and user hits ok, or user selects exit menuif (in_menu==FALSE) {in_menu = TRUE;SetMenu(0); // Init menu 0// exit function here, otherwise further below function keeps going and thinks you hit OK on a the menu that now showsreturn;} else { // already in a menu, so if the selection is 255 then exit menuif (menu==0 && options==255) { // user selected "Exit menu"in_menu=FALSE;ExitMenu(); // reset screen, show other stuff besides menu, whateverreturn; // same as above, exit function because there's no menu anymore}}if (in_menu==TRUE) {if (menu==0 && options==1) { // selected Discharge USBSetMenu(1);}// add more submenu handling here// individual optionsif (menu==1 && options==2) { // user pressed 5v 1A// set the led you want hereSetMenu(0); // Go back to previous menu (or you can exit menu completely if you want)}if (menu==1 && options==2) { // user pressed 5v 2a// set the led you want hereSetMenu(0); // Go back to previous menu (or you can exit menu completely if you want)}// ....if (menu==1 && options==5) { // user hit Back// do nothing with the ledsSetMenu(0); // Go back to previous menu}}}if (button_up_pressed ==true || (button_down_pressed==true) {if (in_menu==true) {if (button_up_pressed==true) {selection = selection -1;if (selection==0) selection = options; // was already on first line, substracted one so got to 0 which is invalid, so fix by going to last line}if (button_down_pressed==true) {selection = selection+1;if (selection > options) selection = 1; // go to first line if user pressed down on last line}RefreshMenu();}}Something like this ... keep in mind I wrote this now completely in the forum text box, didn't check

author: radhika
author_rank: Regular Contributor
timestamp: June 25, 2019, 07:57:23 am
content: Thank you so much It works perfectly fine.