Delicious Bookmark this on Delicious      HOME > > ARDUINO LANGUAGE COURSE  t.o.c.

An example of Arduino programming: LED flasher

This is one of a collection of pages which, together, attempt to show you "everything" about the Arduino's programming language.

There is a page for you with more information about the project in general, and the way these pages are organized, if you want that.

Please visit my page about power browsing notes sometime.

This page, and the software it references, ©TK Boyd, 1/2010.

Just a bit of fun: LED Flasher... variable rate

You can skip this one, it doesn't introduce anything new or important... but I hope you will find what is here fun, and good exercise for your code-reading skills.

In earlier tutorials, we developed a few things which are recombined in this one to produce a little toy.

We're going to have 3 LEDs (you could make it more) flashing in sequence. The rate of the sequence will be controlled by a potentiometer. The "map" command is used to convert the raw potentiometer reading to a number suitable for the delay between the steps of the sequence.

While these essays may help you with what is and is not allowed in Arduino programming, they don't do enough to teach you the art of programming. Maybe the notes below will help you towards getting results more quickly.

In creating even something as simple as the following, even after the "ingredients" were already done, I still had trouble. Minor trouble, but that was because of the years I've struggled, and the stepwise approach I took.

Building the LED flasher

I started with a tiny program which simply winked the LEDs in two states.

I extended that to give the flashing-in-sequence I wanted.

Up until now, I was just using a constant value for delayDuration.

Then I introduced the potentiometer, at first using the output from it directly, to be sure that it was being read properly. Of course when it was returning zero, the LEDs flashed so quickly that they appeared to be on all the time, just a bit dim. And when it was returning 1023, it was a little over a second between each step in the sequence... but I could see that lots of parts of the program were working. I built it up bit by bit, got each bit working before going on to the next thing.

It was at the very end that I changed....

delayDuration=analogRead(bPinPotIsOn);

... to....

intFrmPot=analogRead(bPinPotIsOn);
delayDuration=map(intFrmPot,0,1023,10,200);

... which gave me a little trouble. (I had the arguments in the wrong order. Oh- and I forgot at first to include the word "map"!)

Hence the lines for debugging which you see remmed out in the program below. With them, I was able to "look at" the numbers I was getting in intFrmPot and delayDuration, and deduce errors, and finally get to the solution.

/*FEAa1CountWithShowOn
ver 3 Jan 10

Requires 3 LEDs
*/

const byte LED0=11;//LED for LS bit connected here
const byte LED1=12;//LED connected here
const byte LED2=13;//LED for MS bit connected here

const byte bPinPotIsOn=0;//Connect wiper of potentiometer to this pin
//                    "0" for pin "A0"

word delayDuration;
int intFrmPot;
//(One variable could be used, but this is
//   more clear at little cost.)

void setup()//"setup" always present
{
   pinMode(LED0,OUTPUT);
   pinMode(LED1,OUTPUT);
   pinMode(LED2,OUTPUT);
   //Serial.begin(9600);//for debug work
}

void loop()//This function always present
{
   intFrmPot=analogRead(bPinPotIsOn);
   /*Now "massage" raw "From Potentiometer" number.
   It starts as something from 0-1023, but I want it
   converted (mapped) to the range 10-200, so I use....*/
   //Serial.println(intFrmPot);//for debug
   delayDuration=map(intFrmPot,0,1023,10,200);
   //Serial.println(delayDuration);//for debug
   //Serial.println("-");//for debug
   setLEDs(0,0,1);
   delay(delayDuration);
   setLEDs(0,1,0);
   delay(delayDuration);
   setLEDs(1,0,0);
   delay(delayDuration);
   setLEDs(0,1,0);
   delay(delayDuration);
   //delay(400);//for debug work
}

void setLEDs(byte bL2, byte bL1, byte bL0)
{
   if (bL0==0) digitalWrite(LED0,LOW);
               else  digitalWrite(LED0,HIGH);
   if (bL1==0) digitalWrite(LED1,LOW);
               else  digitalWrite(LED1,HIGH);
   if (bL2==0) digitalWrite(LED2,LOW);
               else  digitalWrite(LED2,HIGH);
 }



   Search this site or the web      powered by FreeFind

Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

SPELL your search term properly. When I review search logs, it is amazing how many people ask the engine to search for something meaningless.


Why does this site cause a script to run? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out their site. And if there are Google ads on the page, they are run with scripts, too.


Click here to return to Arduino COURSE table of contents.
Click here to go to the author's home page.

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