Arduino Menu Ideas, and MenuBackEnd problem - Page 1
Forum section: electronics / general

author: cowboy303
author_rank: Regular Contributor
timestamp: April 30, 2013, 04:41:38 pm
content: I'm using a arduino uno Rev3 and a LCD.I'm working with MenuBackEnd right now.It worked perfect for the first little bit when I kept the mune size small, but now that I'm trying to enlarge it, it doesn't like it, it compiles, but the arduino seems to keep looping through the setup function.this is the current menu size that BARELY works;QuoteNow here's the funny part, in that Item3SubItem4 right now if you select it on the menu, I have it do nothing. If I make it call a function or ANYTHING. LCD backlight does not turn on (which is in the setup funtion), it's seems pretty well dead.So I either need a fix for this, or maybe if anybody knows of a better Menu library that would be awesome.Sorry for the lack of info, so Please ask for any info that would help, and I would be happy to help in anyway I can.Thanks,Oh and the Sketch size is 900 lines long, so if you want the code if you could maybe say what part you would like. So I don't have to make you look through the whole thing.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 01:48:42 am
content: Sorry that the first post didn't have a whole lot of info, BUT I've been working with it more, and I Decided to start from the example code that comes with the MenuBackend library. And Make the menu the size I need it, and then add all the stuff and see what kills it, step by step. And well, I have found my first culprit.Serial.Ok so I took the sample menu that you can find here,http://wiring.org.co/reference/libraries/MenuItem/MenuItem_addBefore_.html. and I added a bunch of menus/submenus to it, and it worked still, when you "used" a item I didn't have it do anything, but none the less it work perfect.Here's the current code I'm at now, I put "//" before I all the Serial stuff to cancel it out and now it works.(over 20000 characters) so it's in the next postOK as you might have seen, I have a function called blink LED (bottum of sketch), it's like a heartbeat, tells me if it's still working. Anyway as you can see it not only blinks a LED but it also reports that to the Serial monitor. Well this is what was on the serial monitor,QuoteSerial monitor would only show a "1" once, while the LED is blinking away, and if I hit ANY button it would freeze (LED stops blinking), sometimes I don't have to hit anything for it to freeze. But everything will work perfect if I get rid of the Serial stuff.I have NO CLUE what's going on here, I'm really still a noob to this stuff.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 01:52:41 am
content: Quotecontinued...

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 01:54:49 am
content: Quote

author: ecat
author_rank: Frequent Contributor
timestamp: May 02, 2013, 03:50:35 am
content: A few things to try...In Setup() comment out menuSetup(); and in Loop() comment out ReadButtons(); and NavigateMenu();// menuSetup();// ReadButtons();// NavigateMenu();You should now have serial comms and a blinking LED that will run all day. It doesn't do anything else of course, but the idea is to get the code to a point that is stable. From this point you can add stuff until it breaks again at which point you can mefairly surethe problem is with the last bit of stuff you added.If the above works try reinstating ReadButtons();// menuSetup();ReadButtons();// NavigateMenu();If this works things start to become complicated since all the menu stuff is interrelated. Try reinstating menuSetup();menuSetup();ReadButtons();// NavigateMenu();Mybonestell me this is where the problem is most likely to be, something in the setup is overwriting something important.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 04:25:56 am
content: Awesome! Great Idea, it seems that the problem lies in the ReadButtons() function, which I didn't expect.I only "//" the ReadButton function, and it's working perfect (besides the fact that I can't navigate the Menu with no buttons lol) it's been sitting here for 10 minutes just blinking and reporting through Serial.I also had this setup://MenuSetup()//NavigateMenu()ReadButtons()and it was having problems, it would only report through Serial once if that and then freeze within 30 seconds.

author: ecat
author_rank: Frequent Contributor
timestamp: May 02, 2013, 04:56:29 am
content: Surprising find.The only interesting part I see in ReadButtons() is the declaration of 5 integers at the start, so 10 bytes on the stack if Arduino works that way.Moveint reading;int buttonEnterState=LOW; // the current reading from the Enter input pinint buttonEscState=LOW; // the current reading from the input pinint buttonLeftState=LOW; // the current reading from the input pinint buttonRightState=LOW; // the current reading from the input pinout of the function so you haveint reading;int buttonEnterState; // the current reading from the Enter input pinint buttonEscState; // the current reading from the input pinint buttonLeftState; // the current reading from the input pinint buttonRightState; // the current reading from the input pinvoid ReadButtons(){ //read buttons statusbuttonEnterState=LOW; // the current reading from the Enter input pinbuttonEscState=LOW; // the current reading from the input pinbuttonLeftState=LOW; // the current reading from the input pinbuttonRightState=LOW; // the current reading from the input pinthis way you move these integers from the stack and into the main data memory.

author: TerminalJack505
author_rank: Super Contributor
timestamp: May 02, 2013, 05:14:45 am
content: I suspect that you might have run out of ram. Your stack is likely colliding with your data.All of those menu item strings ("Item2Sub1SubThisSubThat") are likely in data memory. If they don't change then a better place for them would be program memory. I've never used that menu library so I don't know if there's a constructor that takes a pointer to program memory or not. If it does then you might change all those strings so that they reside in program memory.

author: ecat
author_rank: Frequent Contributor
timestamp: May 02, 2013, 05:36:05 am
content: Quote from: TerminalJack505 on May 02, 2013, 05:14:45 amI was just looking at this.http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zipFrom what I can see Ithinkthe strings are stored in flash (program memory) by default and MenuItem references them from there - though this could still be a problem. If they are stored in ram then TerminalJack505 is spot on.Each MenuItem also defines 5 pointers stored in ramMenuItem *before;MenuItem *right;MenuItem *after;MenuItem *left;MenuItem *back;Assuming 2 bytes per pointer = 10 bytes.There are 62 MenuItems so, 620 bytes. The standard Arduino UNO has 2kB of ram.Which ever way the menu system works, get rid of some MenuItems... How many? lol, Start with just 3 or 4 (or, 1 or 2). Then add them back in to see if and when the code goes silly.Better still, try to find out how the Arduino environment informs you of flash used and ram used then check these numbers against the cpu on your Arduino board.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 12:36:03 pm
content: I tried moving the "buttonEnterState" like you said, still no show.I tried taking off some of the menu, and that fixes it. if I remove two it works fine. If I remove one, it doesn't work at all.So I guess Mr.TerminalJack505 is right, that kinda makes sense to me because I was fighting this problem the whole way along with my project, I found removing serial helped. and where I"m at now, there is 1 line of code I have commented offQuoteif I comment that out the whole thing works, if I don't, it don't.So I guess I have to work with a smaller menu? or is there a way to fix this without making it TOOOO much smaller?Thank you gentlemen for your help!

author: ecat
author_rank: Frequent Contributor
timestamp: May 02, 2013, 12:47:02 pm
content: If it is just a string length problem then simply reducing the length of the names will help, instead of "Item2Sub1Sub1" try "I2S1S1" or "I2.1.1", the long string is 13 characters = 13 bytes + 1 byte (for the string terminator) and the short version shown here is 6 characters = 6 bytes + 1 byte. So, if you shorten the strings for all of the menu items you save a considerable amount of storage space.If shortening the strings doesn't help and you still need to chop a menu item then it looks like the strings are stored in program memory by default and you may need some work on the Menu library to implement your full menu structure.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 01:22:30 pm
content: Looks like you were right again, itn's now "I2S1S2" and it's working fine with nothing commented off.Question,as you can see here:QuoteI use the menu name to display on the LCD, So can I do the code below without any negitive effects?QuoteI could also use a if statement, but that wouldn't be my favorite choice.

author: ecat
author_rank: Frequent Contributor
timestamp: May 02, 2013, 01:51:33 pm
content: ...Only one way to find out1) You know how many characters you used when you had problems, just count them all up and 1 extra for each full string.2) You know how many menu items you had to remove to make the code work, you can add up how many characters these entries used.So, the total from 1) minus the total from 2) gives you a rough idea of the maximum number characters you can use. Whatever you decide to name your items just make sure the total number of characters is less than your safe total and you should be good.Item1Sub1 = MenuItem("Item1Sub1"); is 9 characters + 1 = 10 bytes in totalItem1Sub1 = MenuItem("Main"); is 4 characters + 1 = 5 bytes in total^Name^ ^StringJust keep track as you go. Also notice I've used Item1Sub1 as the name for the MenuItem variable (class instance to be pedantic), the character count rule we're describing here applies only to the characters in the strings "I'm a string", outside of the quotes you can use any names you wish (subject to whatever the Arduino language restrictions may be).It's worth remembering that any additional functionality you need to add to finish your program will require additional memory and at some point going forward you may find your program breaks again. It maybe worth while thinking of ways to abbreviate your strings..."1 EEPROM TEST" Not much you can do about this ..."Set BackLight" "Set Light" maybe?"Display Time" "Disp Time" ?It's up to you if you spend the time now or wait and see if problems occur and then go on an abbreviation spree.

author: cowboy303
author_rank: Regular Contributor
timestamp: May 02, 2013, 11:39:23 pm
content: Well I think I'm just going to keep in mind that I'm a little short on ram, and if I have problems then I'll go on a abbreviation spree.I really didn't understand how the whole thing worked, so thank you for helping, and useing such easy-to-understand examples, I know it takes a bit more time to do it that way.

author: ecat
author_rank: Frequent Contributor
timestamp: May 03, 2013, 01:00:30 am
content: You're welcome.And thanks to the prompting from TerminalJack505 I learned that Arduino stores constant strings in RAMA few links I found:Arduino and Memory - the bit at the bottom of the page, the F() syntax, may be worth investigatinghttp://playground.arduino.cc/Learning/MemoryMemory and full map file from Arduino .elf filehttp://arduino.cc/forum/index.php?topic=72948.0Memory usage if you use Arduino for Visual Chttp://www.visualmicro.com/post/2012/08/24/Arduino-Memory-Usage-and-Disassembly-Reporting.aspxAdvanced Coding and Memory Handlinghttp://www.oreilly.de/catalog/arduinockbkger/Arduino_Kochbuch_englKap_17.pdf

author: TerminalJack505
author_rank: Super Contributor
timestamp: May 03, 2013, 09:06:26 am
content: For grins I played around with the code to see what it would take to put the strings into program memory (aka flash.) I've attached the changes in case you ever need that particular functionality. In your case, since your menus are so big, it will save a lot of SRAM. And SRAM can be a scarce resource.I changed the library source and the example application to use strings from flash. All of the menu names have to be stored in flash. That is, you can't mix and match with some in flash and others in data memory.You'll notice in the code there are a couple of ways to get at the string when it is in flash memory. The Serial object can deal directly with strings in flash, for example. If it is just a string literal then all you have to do is use theF()macro like so:Code:Serial.println(F("Starting navigation:\r\nUp: w Down: s Left: a Right: d Use: e"));If a char * points to a string in flash then you use thefromFlash()inline function defined in the example code to tell the Serial object it's in flash:Code:Serial.println(fromFlash(used.item.getName()));There's also an function calledgetItemNameFromFlash()that shows how to copy a string from flash to data memory.Code:inline char * getItemNameFromFlash(const MenuItem & item, char * copyToBuffer, size_t bufferSize){strncpy_P(copyToBuffer, item.getName(), bufferSize);copyToBuffer[bufferSize - 1] = '\0'; // Make sure the buffer is null-terminated.return copyToBuffer;}As you can see it is pretty much just a wrapper for thestrncpy_P()AVR Lib C function. Consult the AVR Lib C manual for more information.Finally, this is how you declare the MenuItem objects so that the menu name resides in flash:Code:char itemNameSettings[] PROGMEM = "Settings";char itemNamePin[] PROGMEM = "Pin";char itemNameDebug[] PROGMEM = "Debug";char itemNameOptions[] PROGMEM = "Options";char itemNameDelay[] PROGMEM = "Delay";char itemName100ms[] PROGMEM = "100 ms";char itemName200ms[] PROGMEM = "200 ms";char itemName300ms[] PROGMEM = "300 ms";char itemName400ms[] PROGMEM = "400 ms";//this controls the menu backend and the event generationMenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);//beneath is list of menu items needed to build the menuMenuItem settings = MenuItem(itemNameSettings);MenuItem pin = MenuItem(itemNamePin);MenuItem debug = MenuItem(itemNameDebug);MenuItem options = MenuItem(itemNameOptions);MenuItem setDelay = MenuItem(itemNameDelay,'D');MenuItem d100 = MenuItem(itemName100ms);MenuItem d200 = MenuItem(itemName200ms);MenuItem d300 = MenuItem(itemName300ms);MenuItem d400 = MenuItem(itemName400ms);

author: cowboy303
author_rank: Regular Contributor
timestamp: May 05, 2013, 06:54:12 pm
content: Thank you Mr.TerminalJack505! Your code/library are working perfectly! I made a function to control the LCD bargraph, and like all my code I write it wasn't working at first. by the end of troubleshooting, there was Serial.println stuff EVERYWHERE, and they were pretty long too, like "blocks is not oldBlocks". and it never froze up!and something I noticed on my old sketch that was having problems,Quotethat's 18 bytes (thank you ecat), and what's worse,Quotesooooo yeah, I was hard on it.Thank you Gentlemen for heading me in the right direction. it's really nice knowing that if I have a problem that I can't figure out, that there are gentlemen like you guys that are willing to help.

author: tom1147
author_rank: Newbie
timestamp: January 20, 2015, 09:39:45 pm
content: can you tell me how to post code on this site thanks tom

author: electr_peter
author_rank: Unknown
timestamp: January 20, 2015, 10:06:53 pm
content: Select "#" symbol when writing a commentCode:2 + 2 == 4

author: tom1147
author_rank: Newbie
timestamp: January 21, 2015, 01:28:16 am
content: hello again here is my arduino file having trouble with menuUseEvent if (used.item==item1);want to run engineID() under loop aand save 4 digit number can someone let me have somedirection new to this if you cant figuare out what i am trying to do please contack me thanks tomcann't upload my arduion file so you can email me at tamacdonald@cfl.rr.com i will send it to youas an attachments save time retypeing code thanks tom any help

author: TerminalJack505
author_rank: Super Contributor
timestamp: January 21, 2015, 01:53:41 am
content: To post your code you should be able to attach a zip file to a post by clicking "Attachments and other options" beneath the edit box and then clicking the "Browse..." button.If this isn't available then it might be because you don't have enough posts to the forum. It seems like I remember that users had certain limitations until they've made at least 5 posts.

author: tom1147
author_rank: Newbie
timestamp: January 22, 2015, 01:02:04 pm
content: stuck how to i get from menuUseEvent item1to run fuction tom() which is an array number,then go back to read keypad() fuction which is the menu scroll up and downsorry to such a pain new at this do not understand how to put things togetherthanks tom1147