HOME  >>  ARDUINO HOW TO MENU >> 

A simple "skeleton" control structure

Suitable for many projects

In this tutorial, I am going to show you a simple control structure you might want to employ for some projects.

I used it again in January 2015, when playing with some WS2812 smart LEDs... lots of fun... but you could use it in lots of circumstances.

Confession: I haven't tried the following code! If there are little errors, consider them "exercises for the student". I hope the overall "genius" of the control structure I am going to describe will be clear.

Imagine that you had an Arduino with three momentary switches on three inputs. Button down, input reads High, not down, low. So, if you take the three buttons together, you can have 0 (up/up/up) to 7 (on/on/on) "on" the inputs... and the 6 numbers in between, also, of course.

It would be easy to write a function which would return the number "showing" on the inputs. We'll assume one called "bIPsSay", for "inputs say".

Further imagine that you have a fancy three color LED attached to an output pin, and that you only have to "say" MakeLED(255,128,0) to make that LED shine with full red (255), half green (128) and no blue. Change the numbers, change the color and brightness.

NOW assume you want to "show off" this wonderful device. So you write the following.... (which would need a setup() subroutine, would need "bIPsSay()" set up, etc... we'll assume those things done.)

void loop
{
if (bIPsSay()==0)
  {
  MakeLED(0,255,0);//green
  delay(300);
  MakeLED(0,0,0);
  delay(80);
  }
if (bIPsSay()==1)
  {
  MakeLED(255,0,0);//red
  delay(100);
  MakeLED(0,0,0);
  delay(80);
  MakeLED(255,0,0);//red
  delay(100);
  MakeLED(0,0,0);
  delay(80);
  MakeLED(255,0,0);//red
  delay(100);
  MakeLED(0,0,0);
  delay(80);
  }
};//end of "loop()

That's pretty crudely done, but will serve our purposes. You should be able to see that if no buttons are pressed, the tri-color LED winks gently in green, but if the first button is pressed, the tri-color LED winks red, rapidly.

But. There's already a problem. If you press the first button briefly, you will still get three "rounds" of "red on"/ "red off"... even though you stopped asking for that a little time ago.

In this particular program, that probably would be no big deal. But: Suppose that what the computer does when you press the first button goes on for, say, ten seconds. Surely, it should stop doing the "button 1 pressed" behavior just about as soon as the button is released? And the current program doesn't do that.

The current program is giving an example of "blocking". Once the "button 1" "show" has started, the input buttons are "blocked"... the program doesn't "see" the change of the button state. But we can fix that!...

Consider the following....

void loop
{
if (bIPsSay()==0)
  {
  MakeLED(0,255,0);//green
  delay(300);
  MakeLED(0,0,0);
  delay(80);
  }
if (bIPsSay()==1)
  {
  MakeLED(255,0,0);//red
  delay(100);
    if (bIPsSay()==1)
      {
       MakeLED(0,0,0);
       delay(80);
       }
    if (bIPsSay()==1)
      {
       MakeLED(255,0,0);//red
       delay(100);
       }
    if (bIPsSay()==1)
      {
       MakeLED(0,0,0);
       delay(80);
       }
    if (bIPsSay()==1)
      {
       MakeLED(255,0,0);//red
       delay(100);
       }
    if (bIPsSay()==1)
      {
       MakeLED(0,0,0);
       delay(80);
       }
  }//end of first "if (bIPsSay()==1)..."
};//end of "loop()

That's crude, and you wouldn't need so many secondary "if (bIPsSay()==1)..."s... but I hope the way you can achieve a "menu" structure, and have it without blocking is clear to you now.

There would, of course, be other blocks...

if (bIPsSay()==2)
  {...
  }
if (bIPsSay()==3)
  {...
  }

... et cetera.

I couldn't stand it!

I came across this page seven years after my previous visit... and couldn't stand leaving that there.

Yes. What's above "will work". And maybe the following is "too subtle" for some of the people who will find the rest of the page useful. (Don't worry, if that's the case for you... but TRY to grasp it. It REALLY IS "the way to go".)

The following is (almost) a replacement for a chunk of what is above. (Sorry... there's a little blemish... you might find it useful to find that, fix it. It WILL "work"... ALMOST as the original did.)

Apologies for not explaining well here. I will point one thing out: Though it may seem odd to start bDoNext at 5, and count DOWN, doing it that way is Just Better.

Your could start it, and count up, but if you did, you'd have...

until ((DoNext>5)||(bIPsSay()!=1))

at the end. Any time you changed the number of items in the "do it" list, you's have to remember to change the number in the "until". Yes, doing it the other way means that the thing you set bDoNext to at the start has to change every time, but that is NEAR where other things are happening, and it is harder to overlook.

[IMG: aht1simp_alt_code_lp1210196.png should appear here]

Same but different

In July 2022, I wrote a much bigger tutorial about this trick. It was for a different language, and a different computer, but might be interesting anyway. (It is in Lazarus, a variety of Pascal, for a Windows machine. If you want to look at it, fetch the .zip and open th e.pas file in a text editor.)

Concluding remarks

I hope that was clear? Useful? Let me know of anything you found poorly explained, and I will try to get this edited before the next reader suffers. Facebook "Like"s much appreciated. No point in writing these pages, if no one can find them.

If you use subroutines, as you should for all but the very, very simplest work, you can make the above look a lot better... but the basic idea will remain.




To search THIS site.... (Go to my other sites, below, and use their search buttons if you want to search them.)
Click this to search this site without using forms.

powered by FreeFind
Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have two other sites, and that this search will not include them. They have their own search buttons.
My site at Arunet.
My Sheepdog Software pages, another of this page's editor's sites.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.




Here is how you can contact this page's editor. This page, and the software it references, ©TK Boyd, 1/2010.

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org