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

Arduino program development: extended and explained - Pt.1

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.

========

THIS IS STILL IN A "raw" STATE

Come back later if you don't like typos, etc.

The code WORKED... but in displaying it as HTML

distortions may have crept in

========

This is one page in a series which track the deveopment of an Arduino based Access Control device (electronic lock).

============

This the first of a long series of tutorials, but they are, I hope!, worth slogging through. It introduces a number of the Arduino's features. It is also more typical of the design work you will do. The other tutorials tended to to "little snippets" to make them digestable.

By the end of these tutorials, we will have developed an "electronic combination lock". You can just skip to "the answer" if you want the device without the learning experience you could have along the way to getting it.

Don't worry... there's nothing fancy to buy to complete the tutorial... we will be using an LED to "stand in" for the electro-mechanical striker plate which would be required if you wanted to set this access control solution up for real.

This electronic combination lock would be fitted outside a locked door. LEDs would present the user with a "challenge". Based on what the LEDs are showing, the user presses one or more buttons. If the user presses the RIGHT buttons, the door would, electro-mechanically, be unlocked for a period of time. Note that "the right buttons" is not the same from one time to the next.

We'll be going through the above again. For now, don't worry too much about the details, press on.

"Electro-mechanical strikeplate": This is something you buy at a good hardware store. It is fitted to the door jamb. The tongue of the lock engages with the strikeplate. When the strikeplate is supplied with electricity, usually 12v, the door can be pushed (or pulled, if it is hung that way!) open. If there is a problem, or if the user prefers, the door can be opened with a key instead. You may have seen these strikeplates fitted in a way that makes them "buzz" when activated. That "buzz" is not required, don't let it put you off.

An Arduino cannot DIRECTLY operate something like the strikeplate. As I should have discussed earlier, , while an Arduino can handle turning LEDs on and off quite happily, things that operate on different voltages, or which require high currents have to be controlled indirectly, through transistors, relays, etc. There is a little more on this at ;my general (not Arduino specific) page on controlling external devices. There's also a useful ;page at the Arduino site about controlling relays. DON'T SKIP THE DIODE!

For the purposes of this tutorial, as I said, an LED will be connected to the Arduino in place of the strikeplace. When the LED is on, we will consider the "door" unlocked. If the project were taken forward to real access control duties, no changes in the Arduino would be necessary... the output driving the LED would just be connected to the electronics to drive the strikeplate.

---

"The access control device would present the user with some LEDs"... in the tutorial, we are going to have two LEDs. In a real-world application, you would use more. With two LEDs, there are four possible combinations, or codes, (expressed in ons and offs) to present to the user. With three LEDs, there are eight.

---

"If the user presses the RIGHT buttons, the door is unlocked."

There will be one button for each LED. In the early stages of development, we will make "the rule" be "to open the door, if an LED is on, press the coresponding button, and if the LED is off don't press its button." Not the mose secure "lock" in the world! But we will get that much working, and then move on to something more subtle.

---

"...door is unlocked for a period of time." The door will only be unlocked for a short while. Users won't have to do anything about re-locking the door after pressing the right buttons to open the door.

---

A detail:

In detail, using the solution will work like this:

The user will come up to the door, and look at what LEDs are lit. He or she will then start pressing whatever buttons need to be pressed. At the moment the user presses the first button a timer will start running. After a time delay that can be set by the programmer, about 5 seconds will probably be about right, the system will look at what buttons are pressed at that time. If the right buttons are pressed, the door will open. While it is open, the LEDs will do a slow flash, to give visual confirmation that the door is supposed to be unlocked. If at the moment the system looks at the buttons, the combination is NOT right, the LEDs will do a rapid flash for a while, and then the user will be presented with a new pattern to interpret, in other words, the user will be able to try again.

It wouldn't be too hard to make the system give users a 5 second delay before a retry was possible after the first failed attempt to enter a code, and a 10 second delay after the second failed attempt (in a row), 20 seconds delay after the third, 40 second delay after the fourth, etc. This to thwart the "trial and error" "burglar".

You might prefer to replace the momentary switches we've used for most tutorials with toggle switches for this exercise. And it might be an idea to use an LED of a different color for the "lock" LED.

=== So! Time to get started!

You might think I'm going mad with functions for this, functions for that in this tutorial. Not at all. Using functions to "wrap up" things is my normal working practice, and it makes my code Good: easy to work with, etc.

My Arduino for this is going to have two switches, the faithful "green" and "yellow" used so often in these tutorials (although, as I said, you might want to use toggle switches for this), and three LEDs: One to stand in for the lock, and two more called "1" and "0". "1" will be the left-hand-most LED, and will be the "most significant bit", if you understand that, and if you don't, it doesn't matter!

The two non-lock LEDs will be taken together as a 2 digit binary number. Also, eventually, the two switches will be considered together to be a way to input a 2 digit binary number. My yellow switch is to the left of my green switch... you should arrange yours the same way to follow this tutorial easily. I am going to name the switches Yel1 and Grn0 to help me stay clear on the fact that the yellow switch is supplying the most significant bit in the 2 digit number. ("bit" used in the narrow sense there). (N.B.: The yellow switch's name is "ywe-ee-ell-one" for YELlow, bit ONE. The similarity of the lower case "ell" and the digit "one" is not helpful, is it?!)

The names used for the constants associated with the pins the various LEDs and switches are connected to were not chosen at random, as you will see. Be sure that you use meaningful names in your own programming.

I'm going to start with the two messages-by-LED functions that will be called to say "You got code right" and "you got code wring". For now, these will be triggered by pressing the Grn0 switch or the Yel1 switch, respectively.

Note the fairly detailled information in the comments at the start of the code. Writing out what the program will do is always a good idea.... it focuses your mind. Always important to have a clear idea of where you are going before you set out.... and then to proceed to your destination by a carefully chosen incremental route. If you haven't heard of "top down design/ bottom up development", try to find an essay or two on that excellent system of project development.

/*FEAa1AccContStart: access control system-  start of development
ver 27 Dec 09

Requires 3 LEDs, two switches.
One of the LEDs is standing in for an electromechanical strikeplate

Two of the LEDs present a "challenge" to the user
If the user holds down the right combination on
the switches for a short while, the "lock" opens for
a time.

The lock doesn't open WHEN the user presses the right
combination on the switches... that would make trial and
error too easy. Rather, the software looks at the switches
at a pre-determined moment, and if they are properly
set at that moment, the door opens... or at least the "lock" LED turns
on for a set period.

The code in FEAa1AccContStart doesn't do all of that... it just
makes a start along the road to it.

*/

const byte LEDlock=13;//Pin where lock would be connected.
const byte LED1=12;//LED1 connected here (MS bit)
const byte LED0=11;//LED0 connected here (LS bit)

const byte bGrn0=4;//pin green switch is on
const byte bYel1=5;//pin yellow switch is on
// N.B.: That is "ywe-ee-ell-one" for YELlow, bit ONE

void setup()//"setup" always present
{
   pinMode(LEDlock,OUTPUT);
   pinMode(LED0,OUTPUT);
   pinMode(LED1,OUTPUT);

   pinMode(bGrn0,INPUT);
   pinMode(bYel1,INPUT);

   setLEDs(0,0);

}

void loop()//This function always present
{
  if (boSwitchPressed(bYel1))
     {SayFailed();};

  if (boSwitchPressed(bGrn0))
     {SaySuccessAndOpenDoorDuringMessage();};

}//end of function "loop"

boolean boSwitchPressed(byte bWhichSwitch)
//In this version, the program does NOT wait for the switch
//to be released.
  {
   boolean boAns=false;//What is in this will eventually be passed
     //back to the calling code. Assume switch NOT pressed,
     //for the moment.
   if (digitalRead(bWhichSwitch)==LOW)
     {
        delay(20);
        boAns=true;
     }//
     return boAns;
  }//end of boSwitchPressed

void SayFailed()
{
FlashLEDs(12,60);
}//end of SayFailed

void SaySuccessAndOpenDoorDuringMessage()
//That name looks silly... but it will appear only
//once, and the extra information will help you
//be clear about what is going on.

//The stuff to unlock the door WILL be part of this,
//.... later.
{
FlashLEDs(6,200);
}//end of SaySuccess...

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

void FlashLEDs(byte bReps,byte bRate)
//Call this a number of times to cause both LEDs to
//flash on-off-on-off. The number in bRate controls
//how fast the LEDs flash.
{
  for (int i=0;i<bReps;i++)
   {
      setLEDs(1,1);
      delay(bRate);
      setLEDs(0,0);
      delay(bRate);
   }//end of "for..."
}

A little side story for you...

The "Flash LEDs" went throught the following stages....

At first, when I wanted the LEDs to flash, I just put the following into the code....

const byte bFlashRate=60;
setLEDs(1,1);
delay(bFlashRate);
setLEDs(0,0);
delay(bFlashRate);
setLEDs(1,1);
delay(bFlashRate);
setLEDs(0,0);
delay(bFlashRate);
setLEDs(1,1);
delay(bFlashRate);
setLEDs(0,0);
delay(bFlashRate);

It was legal and easy to have two different "bFlashRate"s, so the rate of flashing could be different in the two places I wanted to flash the LEDs.

However, whenever you see code repeating, ask yourself "Should I do this better?" Here we were repeating on two levels!! (The two places where there were multiple....

setLEDs(1,1);
delay(bFlashRate);
setLEDs(0,0);
delay(bFlashRate);

... units, and the repetition of that unit in each place.)

So the first thing I did was to create

void FlashLEDs(byte bRate)
//Call this a number of times to cause both LEDs to
//flash on-off-on-off. The number in bRate controls
//how fast the LEDs flash.
{
  setLEDs(1,1);
  delay(bRate);
  setLEDs(0,0);
  delay(bRate);
};

That, called as follows, created the desired effect....

for (int i=0;i<5;i++){FlashLEDs(200);};

... however, even that wasn't as good as it could be.

When I want the LEDs to go on-off 6 times, lingering in each state for 200ms, I now just insert the following in my code....

FlashLEDs(6,200);

... because the FlashLEDs function is now....

void FlashLEDs(byte bReps,byte bRate)
//Call this a number of times to cause both LEDs to
//flash on-off-on-off. The number in bRate controls
//how fast the LEDs flash.
{
  for (int i=0;i<bReps;i++)
   {
      setLEDs(1,1);
      delay(bRate);
      setLEDs(0,0);
      delay(bRate);
   }//end of "for..."
}

That will to to get you started, I think. When you are ready, go on to part two.

Enjoy... while, please, remembering what I said about the pages about the development of the access control device being only in rough draft so far.




   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 WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org