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.
/*plt1d_elegant 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.... writeLEDs(0,0,0); // Next set the state of boItWasOn according to the state // of things at the outset.... boItWasOn=boSw0(); } void loop() { waitForChange();//Watches Sw0; changes boItWasOn and boItIsOn writeNumber(bCount); delay(300); bCount++; 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) {boReturn=true;} else {boReturn=false;}; 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) */ digitalWrite(ledPin2,b2); digitalWrite(ledPin1,b1); digitalWrite(ledPin0,b0); } void waitForChange() { //Bad programming... reference made to //global variables. //Especially bad: Changing their values. do { boItIsOn=boSw0(); } while (not(boItWasOn xor boItIsOn)); boItWasOn=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;} }; }
|
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....