Light animations - 1-dimensional, Chaser


A running light can easily be generated using the ROL- or ROR-instruction or in high-level languages using the >> or << operators.

This is what the ROL instruction of the AVR controller does:
C
b7b6b5b4 b3b2b1b0
C

Rotate instructions which do not rotate through the Carry bit do not exist in the AVR world.

There is a nice little module containing six LEDs which fits perfectly in the headers of the Arduino Port-B and Port-D (different colors available). If you want to use two of them you have to program Pin D7 to work as a ground pin and you are getting problems using TX.

Unfortunately, there is no easy way to rotate more than 8 bits. Well, you could test the most significant bit and copy it to the least significant one but this needs a lot of instructions of code.

In this project we use a long integer variable named image to store and handle the image of the running light LEDs connected to the output port pins.

byte 3byte 2byte 1byte 0
copy of byte 1pins 8 - 13pins 1 - 6copy of byte 2

Actually, we use only the two middle bytes of the long variable. To perform the repeating sequence, we copy byte 2 to byte 0 respectively byte 1 to byte 3 before executing the shift operation, so the bit which went off the frame was fed in from the other side. This can be done misusing type-casting the long variable to an array of bytes:

((byte*) (&image)) [0] = ((byte*) (&image)) [2];

Though looking like a very large command it is just a single copy of one byte:

 808:	80 91 83 01 	lds	r24, 0x0183	; 0x800183 
 80c:	80 93 81 01 	sts	0x0181, r24	; 0x800181 

Now, let's have a closer look at the hardware:

   
GND1312111098 76543210

Pin-7 has to be set to LOW (=GND), and the TX-pin should not be used for Serial transmitting while the light is running.

The pins 8 to 13 are mapped to the lower bits of byte 2, and the pins 1 to 6 are mapped to the hight bits of byte 1. Of course, there is a gap between the two modules. If the light is running at a fast speed, nobody will notice it.


The way we do it is strongly related to the way the compiler handles multi-byte data (big Endian). If you want to port this code to a little Endian (Intel) system you have to modify it.


contact: nji(at)gmx.de