Monday, May 27, 2013

A LED Chaser Display using SIMPL

My last post described how a LED chaser display was one of my first forays into physical computing about 30 years ago.

My recent experiments with SIMPL, have shown that it is a perfect programming tool for simple interactions with physical computing devices - so I decided to recreate a simple LED chaser display, using SIMPL.

LED chaser displays require a bunch of LEDs which you can address individually. You also need to write code that provides basic loop structures and the means to control timing.  SIMPL, which uses the Txtzyme interpreter is ideal for this task.

Txtzyme uses 1o to set a port pin high and 0o to set it low.  The port pin has to be pre-specified using the d command.  This all becomes a little unwieldy, so lets define a couple of new words to make it easier to type.

We will use H and L to set the port pins high and low respectively.  LEDs are currently fitted to digital pins 2 to 9.

Now the definitions:

:Hd1o     - sets the defined pin high

:Ld0o     - sets the defined pin low

So typing 2H will turn on the LED on pin 2, and 2L will turn it off again. This simplifies the control of outputs to two very memorable commands H and L.

Now we need to generate a loop which turns all the LEDs on in sequence. Because of the way the loop counter works in SIMPL, descending loops are easier as the loop counter k can be accessed to control the loop.

This performs a down count

10{kd1o100mkd0o}

10 - perform the loop 10 times
kd1o  - move the loop count k into d to select the pin and set it high
100m - wait 100mS
kd0o  - move the loop count k into d to select the pin and set it low

We could now substitute H for d1o and L for d0o giving the construct

10{kH100mkL}

To make an upward counting loop is a little harder and it uses the l instruction (l as in little L).

l increments the y variable each time it is executed, and the word @ allows us to move y into x so that we can print it out.

So to make a loop that counts up from 1 to 10 we first have to set y to zero using 0! (store 0 in y)

0!10{l@p}  will print out the numbers from 1 to 10.

The construct l@p increments y, moves y into x and prints it out.  To turn this into a LED chaser we need to add the H and L words to control the LEDs and a short delay to control the speed:

We start with the loop - load y with a count of 1.  The loop will start at 2, and run to 9

1!9{l@pd1o100m@d0o}

We can now replace d1o and d0o with H and L

1!9{l@H100m@L}

Remember that @ gets the current value of the increasing loop count y and puts it into x.




No comments: