JenTown Railroad

An N Scale Model Railroad

Banner Picture

Lighting

I could have just stuck some lights along the street and in the buildings and it would have looked really great. But I didn't do that. No, I did way better than that. I found a board that connects to a USB port on your computer and allows you to control each individual light programmatically. The board is called a Phidget LED-64 as each board can control 64 individual lights. I must say that this has been the most fun I have had so far with this entire project. This board is fantastic! So far I have been able to achieve three different effects with these lights and I have just scratched the surface of what is possible using this board.



First, a few caveats. The board will drive 64 LEDs yes but all 64 LEDs must use the same input voltage and amperage. The board can output 1.5V, 2.75V, 3.9V or 5.0V at 20, 40, 60 or 80 mA but all lights get the same input power level. You cannot mix 5.0 volt lights with 2.75 volt lights on the same board and expect to get optimal results or even passable results. Second, although there is a "demonstrator" program available for download that will allow you to power the lights, set the voltage, current limit and brightness, to get the best results from this board, you must know how to write code. The code is easy to understand and not difficult at all but you must have access to a compiler to have much fun with this board.

With all that out of the way...

The board is made by Phidgets, Inc. and can be found here. As you can see from the photos below, this is an extremely well built board.



Looking at the right-hand side of the picture, for input power the board will accept either a pair of wires providing 12 volt DC from your own power source or a plug from the included wall wart. Output power to the LEDs comes from the two arrays of plugs on either edge of the board - no soldering! This makes for a very easy to use board that even a beginner like me can use.



Here's the contents of the Phidget LED-64 box. The board, a wall wart and all the needed connector wires to power the LEDs. It's all there, everything you need to get started.



This seemed like the logical way to lay out the wiring needed for this board. I think there are only so many ways one can individually wire 64 LEDs. Each LED has a lead for the anode and a lead for the cathode. The above photo shows that so far, I have wired output to 2 of the 64 available connectors but not yet attached any LED lights. Oh, this is going to take a while.



A couple of days later and I'm making slow progress.



Here all the output leads from the board are attached to connectors. There still are no LEDs connected and the board has yet to be installed in the cabinet under the layout. Each of the 128 leads had to be cut to length, the insulation stripped and the wire ends tinned. It took a bit of time.



Having no more room left on the walls of my cabinet, I elected to let the board hang down from the center of the layout. This is where most of the lights will be anyway, where the scenery is, in the middle of the layout. I tried using hinges to allow the board to be easily accessed from either side and allow the board to be folded up out of the way to reach to components behind it. I'm not so certain this worked out as well as I had hoped. Maybe I just haven't done it correctly. I may need to use a bigger hinge or mount the hinges differently.



Most of the lights will be between those two 1x4 boards that stabilze the layout. That should keep the wiring to a a minimum but it's still going to be a mess.



Oh yeah. That's a mess. That's with only 24 lights wired. This is going to get bad. Granted, trimming the excess wire and placing a few zip ties will help but it'll still be messy I'm afraid.







Lighting Software

This is the database that I created to hold the individual light parameters. Each light has a Key#, some description, 3 locator fields that I plan to use later and 2 brightness values. The field called Brightness is to store the current brightness value. This is the field that would be changed normally. The additional Adjustment value is there should the lamp need a base level adjustment to make it match the other lights. The Sequence field is used to order the lamps for the Chaser effect and the Paired field is used for the Runway effect. With these database values, performing the effects is simply a matter of invoking the correct index and iterating through the database changing the Brightness values to zero for a short while then restoring that value to the value the lamp previously held.


This is what the UI of my program that uses that data to control the lights looks like. I still need to add alot of things to this program but this is a start and it can work the lights. It's a good proof of concept. The top half of the form has properties that apply to the board and all lamps connected to that board. The lower part of the form is meant to show properties that apply to an individual lamp only. This part of the program still needs some work.

Phidgets supplies not only the driver file needed to access the board but also the needed code library files for many popular coding environments. The following code is written in Delphi. For Delphi, just download the appropriate files from Phidgets, unzip them, install the ActiveX component into your Delphi environment, drop the PhidgetLED ActiveX control onto your form, add the Phidget21COM_TLB file to your project and start accessing the API. It's just that easy. Well, in their library file I did move one procedure from Protected to Public but other than that it all worked just like it's supposed to.

Once the hardware is all in place and the board is connected to the computer our program must first "Open" the board and "Attach" to it. This is done with the API call "Open" and then a call to "WaitForAttachment". I put the calls in the OnCreate event handler for the Form like so.

procedure TfmLights.FormCreate(Sender: TObject);
begin
   LEDBoard1.EnableLogging(6,'testlog.txt');
   LEDBoard1.Open(-1);
   LEDBoard1.WaitForAttachment(3000);
end;

After the board has Attached, we can start to do things with it. Here I get a few informational items from the board and set some initial parameters in the OnAttach event handler for the board. At the end, we make a call to the SetAllBrightness procedure which turns on the lights. That procedure is shown further below and is also called by the BrightnesTrackBar.OnChange event.

procedure TfmLights.LEDBoard1Attach(Sender: TObject);
begin
   BoardAttachedLabel.Caption := 'Attached';
   BoardAttachedLabel.Font.Color := clGreen;
   BoardNameLabel.Caption := LEDBoard1.DeviceName;
   BoardVersionLabel.Caption := LEDBoard1.LibraryVersion;
   NumberLEDsLabel.Caption := IntToSTr(LEDBoard1.NumLEDs);
   LEDBoard1.CurrentLimit := PHIDGETCOM_LED_CURRENT_LIMIT_60mA;
   LEDBoard1.Voltage := PHIDGETCOM_LED_VOLTAGE_5_0V;
   BrightnessTrackBar.Position := LightsTable.FieldByName('Brightness').AsInteger;
   AllBrightnessTrackBar.Position := LightsTable.FieldByName('Brightness').AsInteger;
   SetAllBrightness;
end;

Here is the code that shows how the program goes about changing the brightness levels of the lamps. Note that I am not storing these new brightness values just yet, these are only temporary changes to the brightness levels of all lamps.

procedure TfmLights.AllBrightnessTrackBarChange(Sender: TObject);
begin
   SetAllBrightness;
end;

procedure TfmLights.SetAllBrightness;
var
   a: Integer;

begin
   for a := 0 to 63 do
      LEDBoard1.Set_DiscreteLED(a,AllBrightnessTrackBar.Position);
end;

To implement the Chaser Effect, all I have to do is invoke the correct secondary index, move to the first record in the database, set the lamp to zero brightness, wait for a bit, set it back to what it was and then move on to the next record in the database, i.e. lamp. It's dead-easy. If you are thinking this should be done with a Timer or an additional thread, I think you are right. But this works and is easy to understand if anyone reading this is just trying to get things going.

procedure TfmLights.ChaserButtonClick(Sender: TObject);
var
   OldIndexName: String;

begin
   OldIndexName := LightsTable.IndexName;
   LightsTable.IndexName := 'By Sequence';
   while (ChaserButton.Down) do
   begin
      LightsTable.First;
      while (not LightsTable.EOF) do
      begin
         if (LightsTable.FieldByName('Connected').AsBoolean) and (ChaserButton.Down) then
         begin
            LEDBoard1.Set_DiscreteLED(LightsTable.FieldByName('IndexNumber').AsInteger,0);
            Sleep(85);
            LEDBoard1.Set_DiscreteLED(LightsTable.FieldByName('IndexNumber').AsInteger,BrightnessTrackBar.Position);
         end;
         LightsTable.Next;
         Application.ProcessMessages;
      end;   
   end;
   ChaserButton.Down := FALSE;
   LightsTable.IndexName := OldIndexName;
   LightsTable.First;
end;

At the top of this page, I said that this was the most fun I have had yet with this entire project and it's true. The high quality of the board itself, having all the needed kit in the box, software with oodles of examples easy and ready to download and the simple fact that the software just worked, made this part of the project very enjoyable. Add to all that the absolutely crazy things you can achieve with this setup and there you have a bunch of fun. Thanks and a salute to the fine folks at Phidgets for making a great product that is fun to setup and use.



previous page | next page


Links: Free Website Templates | Themes for WordPress