HOME - - - - - - - - - Other material for programmers - - - - - - - - - Arduino Tutorial Table of Contents

Arduino Programming: Elegant version

This is an elegant version of the program developed during one of my Arduino programming tutorials.

The original was easier for a novice to understand. It brought out some didactic points. But it wasn't very pleasing to my programming soul. An Arduino running the following will perform exactly as one running the program from the tutorial.

I hope you'll cast your eye over the following, and enjoy unraveling bits.... but don't worry about anything that baffles you. All will be made clear in the course of the tutorials.

Elegant version of program for pltd.htm
ver 13 Feb 08
Needs three LEDs, one switch

This version will not attempt to be beginner
transparent... it will instead try to do
the job "cleverly"... without straying into
the world of "too clever"... I hope!

"Counts" 0, 1, 2, 3,... 7, and then loops
back to 0. Count advances each time input
on Sw0 changes. Displays "count" on LEDs
in binary.

boolean boItWasOn;
boolean boItIsOn;

#define ledPin0 13 /* (No ; after #define) Configures program to
    run on hardware with an LED on digital line 13, which
    makes sense as there's already one on that line, on the
    Diecimila itself. You can also have a "bigger, better"
    LED attached. Just be sure to connect it through a suitable
    resistor to the ground line.
 You can move the resistors, have them driven by other lines.
    If you do, JUST change these DEFINEs to reflect your
    hardware choices */
#define ledPin1 12
#define ledPin2 11

#define Sw0 8 /* Configure the program to look at digital
  line 8 when asked to read "Sw0"... "SWitch zero"
  Wire it for closed pulls line low (i.e. to 0v)*/

  byte bCount=1;

void setup()
/*Start, second block of things for tkbOutput*/
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

//Establish initial LED states... both off....

// Next set the state of boItWasOn according to the state
//   of things at the outset....


void loop()
waitForChange();//Watches Sw0; changes boItWasOn and boItIsOn
if (bCount==8){bCount=0;};
//N.B. "if (bCount=8) {bCount..." compiles... and REALLY SCREWS THINGS UP!

boolean boSw0()
/* The remmed out material is equivalent to the remaining
single line... longer, but more clear! If you are happy taking
the obscure shortcut, there is no need to "package" it in the
function, of course.

boolean boReturn;
if (digitalRead(Sw0)==1)
return boReturn;*/

return digitalRead(Sw0);//More strictly typed languages wouldn't allow this.

void writeLEDs(byte b2, byte b1, byte b0)
/*Anyone know a way to do that without saying "byte" THREE times?
(Nothing "clever", thank you... just a quick way to declare three
type-byte parameters) */

void waitForChange()
//Bad programming... reference made to
//global variables.
//Especially bad: Changing their values.

do {
while (not(boItWasOn xor boItIsOn));

void writeNumber(byte bToWrite)
switch (bToWrite)
  /*Annoying "feature" of SWITCH- without the BREAKS,
  if you enter this function with bToWrite equal to,
  say, 3, then ALL of the clauses between the one
  for 3 and the end will execute... even though
  bToWrite is NOT equal to 4, 5, 6, etc at the time.*/
  case 0:{writeLEDs(0,0,0);break;}//CANNOT get rid of breaks.
  case 1:{writeLEDs(0,0,1);break;}
  case 2:{writeLEDs(0,1,0);break;}
  case 3:{writeLEDs(0,1,1);break;}
  case 4:{writeLEDs(1,0,0);break;}
  case 5:{writeLEDs(1,0,1);break;}
  case 6:{writeLEDs(1,1,0);break;}
  case 7:{writeLEDs(1,1,1);break;}

Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web 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"....

You can also search this site without using forms.
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.

Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)

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

Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....