Monday, 19 October 2009

The Light Meter

Hi everyone!
Here I'm going to show you how I leared how to make a simple light meter with a 7-segment LED display, a photoresister, a capacitor and resistors.
On the way I learned some programming tricks that saves time and prevents typing error.  I learned how to use subroutines.  A DO...LOOP is usually called the main routine because all the main activity happens in it.
PBASIC has some commands that can be used to make the program jump out of the main routine, do a job, and then return right back to the same spot in the main routine.  This will allow me to keep each segment of code that does a particular job somewhere other than the main routine.  This process is called executing a subroutine.
Here's an example -

DO
   GOSUB Subroutine_Name
   DEBUG "Next command"
LOOP

       Subroutine_Name:
          DEBUG "This is a subroutine..."
          PAUSE 3000
       RETURN


The command GOSUB Subroutine_Name causes the program to jump to the Subroutine_Name: label.  When the program gets to that label, it keeps running and exicuting commands untill it gets to a RETURN statement.  Then the program goes back to the command that comes after the GOSUB command.

Now I'm going to show you how I programmed the light meter using subroutines.

Here is the programming text -




This is how it works -
Variable declaration:
I think its always best to declair variables (and constants) at the beggining of the program.

Initializing:
This is the section where things have to be done once at the beggining of the program.

Get_RC_Time:
This takes the RC time measurement on the photoresister circuit.  This subroutine has a PAUSE command that charges up the capacitor and the time period to charge up the capacitor is very small.  The RCTIME command sets the value of the time variable. This value is then used by the second subroutine.

Delay:
All this subroutine does is contain a PAUSE.

Update_Display:
The LOOKUP command in this subroutine contains a table with six bit patterns that are used to create the circular pattern on the display.  By adding one to the index variable each time the subroutine is called, it causes the next bit patern in the sequence to get placed in OUTH.


I could change the speed of the light spinning by either changing the programmin or replacing the capacitor with a smaller one.

No comments:

Post a Comment