Thursday, December 23, 2010

 

The CUSB-36R Program For Flashing The Lights

The program that runs the christmas lights has several modes which you can switch between using the push button switch attached to P10.

The default is the music synchronising sequence.

This basically works using 3 equally sized arrays of integers.

Data1 - each bit controls one chanel P40-P47. When a bit is on the light will be on. 0 - all off, 255 - all on.
Data2 - each bit controls one chanel P4-P7, P16, P17, P22, P23
Data3 - specifies the number of miliseconds before the next item is processed.
DataLen - specified the number of items in the 3 arrays.

Once done it automatically starts again.

The program also has the logic which allows a switch attached to P11 and P12 to stop and restart the sequence. This is done by closing the P11 wires which puts the program in a suspended state until P12 is closed at which time it goes back to the start and plays again. As we will see later I have attached P11 and P12 to relays controlled by the Velleman K8055 to allow the light sequence to be synched with music. Whenever the music restarts the box is reset by these two relays.

The second mode loops through displaying each chanel in turn.

The remaining modes switch on an hold one chanel at a time.

The circuit is synchronous so you have to hold in the P10 button until the current sleep is completed before the program will sense it and change to the next mode.

Realistically I leave it in default mode unless I am troubleshooting something.

So here is the code. It is reasonably well commented.






' The device specified
Const Device = CB280

' milliseconds to sleep for
Dim sleep As Integer
sleep = 500

Dim mode As Integer
mode = 1
' 0 - Default cycle all lights 2 at a time
Dim count1 As Byte
count1 = 0
' 1 - Play the xmas tunes
Dim mode1current As Integer
mode1current = 0
' 2-17 - test 1 channel at a time

'
' Insert your light data here
'

Const Byte DATA1 = ( 0, 0, 36, 0, 36, 0, 36, 0, 36, 0, 36, 0, 36, 109, 0, 146, 36, 73, 146, 36, 73, 128, 64, 32, 64, 128, 0, 16, 8,
4, 8, 16, 2, 1, 0, 1, 2, 16, 8, 4, 8, 219, 146, 36, 73, 146, 36, 73, 192, 24, 3, 0, 73, 96, 12, 1, 160, 20, 2, 192, 20, 1, 160, 12,
3, 96, 24, 0, 128, 64, 32, 16, 8, 4, 2, 1, 0, 16, 8, 4, 0, 128, 64, 32, 16, 8, 4, 2, 1, 0, 16, 8, 146, 73, 36, 128, 144, 18, 2, 146,
0, 64, 72, 9, 1, 73, 0, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 206, 0, 146, 36, 73, 146, 36, 73, 128, 64, 32, 64,
128, 0, 16, 8, 4, 8, 16, 2, 1, 0, 1, 2, 16, 8, 4, 8, 213, 146, 36, 73, 146, 36, 73, 128, 64, 32, 64, 128, 0, 16, 8, 4, 8, 16, 2,
1, 0, 1, 2, 16, 8, 4, 8, 0, 128, 64, 32, 16, 8, 4, 2, 1, 0, 16, 8, 4, 0, 128, 64, 32, 16, 8, 4, 2, 1, 0, 16, 8, 146, 73, 36, 128,
144, 18, 2, 146, 0, 64, 72, 9, 1, 73, 0, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 206, 36, 146, 36, 73, 146, 36,
73, 128, 64, 32, 64, 128, 0, 16, 8, 4, 8, 16, 2, 1, 0, 1, 2, 16, 8, 4, 8, 73, 146, 36, 73, 146, 36, 73, 128, 64, 32, 64, 128, 0,
16, 8, 4, 8, 16, 2, 1, 0, 182, 219, 109, 182, 219, 109, 0, 109, 0, 0, 128, 144, 146, 147, 152, 219, 36, 0, 8, 65, 8, 65, 65, 32,
4, 0, 146, 109, 0, 36, 146, 73, 0, 73, 219, 16, 130, 16, 130, 130, 64, 8, 1, 36, 0, 64, 8, 12, 2, 18, 146, 73, 4, 32, 4, 32, 32,
128, 16, 2, 73, 109, 0, 36, 146, 109, 219, 216, 192, 2, 1, 2, 8, 4, 8, 128, 64, 32, 0, 64, 8, 12, 2, 18, 146, 73, 4, 32, 4, 32, 32,
128, 16, 2, 73, 128, 32, 16, 4, 2, 0, 1, 65, 109, 0, 36, 146, 73, 0, 73, 219, 16, 130, 16, 130, 130, 64, 8, 1, 36, 0, 64, 8, 12,
2, 18, 146, 73, 4, 32, 4, 32, 32, 128, 16, 2, 73, 109, 0, 36, 146, 109, 219, 216, 192, 2, 1, 2, 8, 4, 8, 128, 64, 32, 0, 64, 8, 12,
2, 18, 146, 73, 4, 32, 4, 32, 32, 128, 16, 2, 73, 255, 219, 109, 219, 109, 0, 128, 16, 2, 0, 128, 0, 16, 0, 2, 0, 64, 0, 8, 0, 1,
0, 32, 0, 4, 0, 0, 0, 0, 128, 16, 2, 64, 8, 1, 32, 4, 0, 128, 16, 2, 64, 8, 1, 32, 4, 0, 128, 16, 2, 64, 8, 1, 0, 128, 136, 136,
64, 68, 70, 32, 48, 49, 136, 0, 70, 0, 49, 0, 136, 0, 70, 0, 49, 146, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32,
0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 146, 0, 146, 0, 146, 0, 146, 0, 0, 146, 0, 146, 0, 146, 73, 0, 146,
0, 146, 0, 146, 0, 146, 0, 146, 128, 136, 136, 64, 68, 70, 32, 48, 49, 146, 73, 36, 146, 73, 36, 146, 73, 36, 73, 128, 136, 136,
64, 68, 70, 32, 48, 0, 146, 0, 73, 0, 36, 0, 219, 136, 0, 70, 0, 49, 0, 136, 0, 70, 0, 49, 146, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65,
0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 146, 0, 128, 136, 136, 64, 68, 70, 32, 48,
49, 146, 73, 36, 146, 73, 36, 146, 73, 36, 73, 128, 136, 136, 64, 68, 70, 32, 48, 0, 146, 0, 73, 0, 36, 0, 219, 136, 0, 70, 0, 49,
0, 136, 0, 70, 0, 49, 146, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32,
0, 16, 0, 65, 0, 4, 146, 0, 128, 136, 136, 64, 68, 70, 32, 48, 49, 146, 73, 36, 146, 73, 36, 146, 73, 36, 73, 128, 136, 136, 64,
68, 70, 32, 48, 0, 146, 0, 73, 0, 36, 0, 219, 136, 0, 70, 0, 49, 0, 136, 0, 70, 0, 49, 146, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0,
4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 146, 0, 128, 136, 136, 64, 68, 70, 32, 48, 49,
146, 73, 36, 146, 73, 36, 146, 73, 36, 73, 128, 136, 136, 64, 68, 70, 32, 48, 0, 146, 0, 73, 0, 36, 0, 219, 136, 0, 70, 0, 49, 0,
136, 0, 70, 0, 49, 146, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0, 16, 0, 65, 0, 4, 0, 130, 0, 8, 0, 32, 0,
16, 0, 65, 0, 4, 146, 0, 128, 136, 136, 64, 68, 70, 32, 48, 49, 146, 73, 36, 146, 73, 36, 146, 73, 36, 73, 128, 136, 136, 64, 68,
70, 32, 48, 0, 146, 0, 73, 0, 36, 0, 219, 0, 219, 0, 219, 0, 219, 0, 0)
Const Byte DATA2 = ( 0, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 164, 18, 72, 200, 72, 20, 148, 20, 40, 40, 40, 40, 40, 0,
18, 18, 18, 18, 18, 18, 18, 146, 18, 18, 18, 18, 18, 18, 40, 68, 196, 68, 40, 168, 40, 18, 18, 18, 18, 18, 72, 72, 200, 72, 72, 200,
72, 72, 200, 72, 72, 72, 72, 72, 0, 72, 34, 20, 72, 34, 20, 72, 34, 148, 72, 34, 20, 0, 72, 34, 20, 72, 34, 0, 72, 34, 148, 72, 34,
64, 8, 192, 40, 40, 40, 40, 40, 0, 68, 68, 68, 68, 68, 0, 40, 40, 168, 40, 40, 168, 40, 40, 168, 40, 40, 168, 40, 168, 66, 66, 194,
66, 40, 168, 40, 20, 20, 20, 20, 20, 0, 72, 36, 18, 72, 36, 18, 72, 164, 18, 72, 36, 18, 72, 36, 168, 72, 200, 72, 34, 162, 34, 20,
20, 20, 20, 20, 0, 72, 72, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 0, 40, 40, 40, 40, 40, 24, 24, 24, 152, 24, 24, 24, 0,
72, 72, 72, 72, 72, 0, 40, 40, 168, 40, 40, 16, 72, 160, 24, 24, 24, 24, 24, 0, 72, 72, 72, 72, 72, 0, 40, 40, 168, 40, 40, 168,
40, 40, 168, 40, 40, 168, 40, 168, 152, 24, 152, 24, 24, 152, 24, 24, 24, 24, 24, 24, 0, 72, 72, 72, 72, 72, 72, 72, 200, 72, 72,
72, 72, 72, 72, 40, 24, 152, 24, 24, 152, 24, 24, 24, 24, 24, 24, 0, 68, 36, 20, 68, 36, 20, 68, 164, 148, 68, 164, 148, 68, 164,
18, 200, 0, 0, 72, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 72, 72, 200, 72, 162, 34, 162, 34, 34, 34, 34, 34, 72, 34, 20,
72, 34, 20, 72, 34, 148, 72, 72, 72, 72, 72, 72, 72, 72, 72, 162, 20, 200, 162, 20, 72, 34, 20, 162, 34, 162, 34, 162, 34, 34, 34,
72, 34, 20, 72, 34, 20, 72, 34, 20, 20, 20, 20, 20, 20, 20, 20, 20, 72, 162, 20, 200, 162, 20, 72, 34, 20, 72, 72, 72, 72, 72, 200,
72, 72, 162, 34, 162, 34, 34, 34, 34, 34, 72, 34, 20, 72, 34, 20, 72, 34, 148, 20, 20, 20, 20, 20, 20, 20, 20, 72, 162, 20, 200,
162, 20, 72, 34, 20, 200, 72, 200, 72, 200, 72, 72, 72, 72, 34, 20, 72, 34, 20, 72, 34, 20, 36, 36, 36, 36, 36, 36, 36, 36, 72, 162,
20, 200, 162, 20, 72, 34, 20, 200, 34, 148, 72, 162, 20, 72, 34, 20, 0, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 200, 72, 72, 34, 34, 34, 34, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 34, 20, 20, 148,
20, 20, 20, 20, 20, 20, 200, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 20, 20,
20, 20, 20, 148, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 148, 20, 20, 20, 20, 20, 20, 20, 0, 72, 0, 72, 0, 72, 0, 0, 72, 0, 72,
0, 72, 0, 0, 72, 0, 72, 0, 72, 0, 72, 0, 72, 34, 34, 162, 34, 20, 20, 20, 20, 20, 72, 72, 200, 72, 72, 200, 72, 72, 200, 72, 34,
34, 162, 34, 20, 20, 20, 20, 20, 0, 72, 72, 72, 200, 72, 72, 162, 34, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 20, 20, 20, 20,
148, 20, 20, 20, 20, 20, 20, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 72, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 34,
0, 20, 20, 148, 20, 72, 72, 72, 72, 72, 34, 34, 162, 34, 34, 162, 34, 34, 162, 34, 20, 20, 148, 20, 72, 72, 72, 72, 72, 72, 34, 34,
34, 162, 34, 34, 148, 20, 20, 20, 20, 20, 200, 72, 72, 72, 72, 72, 0, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 0, 20, 20, 20, 20,
20, 148, 20, 0, 72, 72, 72, 72, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 0, 0, 34, 34, 162, 34, 20, 20, 20, 20, 20, 72, 72,
200, 72, 72, 200, 72, 72, 200, 72, 34, 34, 162, 34, 20, 20, 20, 20, 20, 0, 72, 72, 72, 200, 72, 72, 162, 34, 34, 34, 34, 34, 162,
34, 34, 34, 34, 34, 34, 20, 20, 20, 20, 148, 20, 20, 20, 20, 20, 0, 72, 72, 72, 72, 72, 200, 72, 72, 72, 72, 72, 72, 72, 34, 34,
34, 34, 162, 34, 34, 34, 34, 34, 34, 34, 0, 20, 20, 148, 20, 72, 72, 72, 72, 72, 34, 34, 162, 34, 34, 162, 34, 34, 162, 34, 20, 20,
148, 20, 72, 72, 72, 72, 72, 0, 34, 34, 34, 162, 34, 34, 148, 20, 20, 20, 20, 20, 148, 20, 20, 20, 20, 20, 20, 72, 72, 72, 72, 200,
72, 72, 72, 72, 72, 72, 34, 34, 34, 34, 34, 162, 34, 34, 34, 34, 34, 34, 34, 34, 20, 20, 20, 148, 20, 20, 20, 20, 20, 20, 20, 0,
72, 72, 200, 72, 34, 34, 34, 34, 34, 20, 20, 148, 20, 20, 148, 20, 20, 148, 20, 72, 72, 200, 72, 34, 34, 34, 34, 34, 0, 20, 20, 20,
200, 34, 20, 72, 72, 72, 72, 72, 72, 72, 72)
Const Integer DATA3 = ( 250, 370, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 155, 155, 310, 310, 620, 310, 310, 620, 310,
310, 465, 155, 930, 310, 310, 310, 465, 155, 310, 310, 310, 155, 155, 310, 310, 310, 310, 620, 310, 310, 310, 620, 310, 310, 620,
310, 310, 465, 155, 930, 310, 310, 310, 465, 155, 310, 310, 310, 310, 310, 310, 310, 310, 310, 620, 310, 310, 310, 310, 930, 155,
155, 310, 310, 310, 310, 930, 310, 310, 310, 310, 310, 930, 310, 310, 310, 310, 310, 465, 155, 155, 155, 310, 310, 310, 310, 930,
310, 310, 310, 310, 310, 930, 155, 155, 310, 310, 310, 310, 310, 310, 465, 155, 310, 310, 310, 310, 620, 310, 310, 310, 620, 310,
310, 620, 310, 310, 465, 155, 930, 310, 310, 310, 310, 465, 155, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 310, 620,
310, 310, 620, 310, 310, 465, 155, 930, 310, 310, 310, 465, 155, 310, 310, 310, 155, 155, 310, 310, 310, 310, 620, 310, 310, 310,
310, 310, 930, 155, 155, 310, 310, 310, 310, 930, 310, 310, 310, 310, 310, 930, 310, 310, 310, 310, 310, 620, 155, 155, 155, 310,
310, 310, 310, 930, 310, 310, 310, 310, 310, 930, 155, 155, 310, 310, 310, 310, 310, 310, 465, 155, 310, 310, 310, 310, 620, 310,
310, 310, 620, 310, 310, 620, 310, 310, 465, 155, 930, 310, 310, 310, 310, 465, 155, 310, 310, 310, 310, 310, 310, 310, 310, 310,
310, 310, 310, 620, 310, 310, 620, 310, 310, 465, 155, 930, 310, 310, 310, 310, 465, 310, 620, 620, 620, 620, 620, 620, 620, 620,
2480, 155, 155, 8510, 457, 442, 147, 295, 295, 295, 295, 295, 295, 147, 147, 147, 147, 442, 147, 295, 295, 590, 442, 147, 295, 295,
295, 295, 295, 295, 147, 147, 147, 147, 442, 147, 295, 295, 590, 442, 147, 295, 295, 295, 295, 295, 295, 147, 147, 147, 147, 442,
147, 295, 295, 590, 442, 147, 295, 295, 442, 147, 295, 295, 147, 147, 295, 147, 147, 295, 295, 295, 590, 442, 147, 295, 295, 295,
295, 295, 295, 147, 147, 147, 147, 442, 147, 295, 295, 590, 147, 295, 147, 147, 295, 295, 295, 590, 442, 147, 295, 295, 295, 295,
295, 295, 147, 147, 147, 147, 442, 147, 295, 295, 590, 442, 147, 295, 295, 295, 295, 295, 295, 147, 147, 147, 147, 442, 147, 295,
295, 590, 442, 147, 295, 295, 442, 147, 295, 295, 147, 147, 295, 147, 147, 295, 295, 295, 590, 442, 147, 295, 295, 295, 295, 295,
295, 147, 147, 147, 147, 442, 147, 295, 295, 590, 295, 295, 295, 295, 885, 295, 590, 590, 1180, 4500, 1027, 1027, 1027, 1027, 1027,
1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1027, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018,
1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 1018, 461, 461, 231, 692, 461, 231,
231, 231, 692, 231, 231, 231, 231, 231, 231, 231, 231, 231, 231, 115, 346, 922, 231, 231, 346, 115, 461, 231, 231, 461, 231, 231,
692, 231, 231, 231, 231, 231, 231, 231, 461, 231, 231, 231, 231, 922, 231, 231, 231, 231, 461, 115, 346, 231, 231, 231, 231, 692,
231, 231, 231, 231, 231, 231, 461, 231, 231, 231, 231, 231, 922, 231, 231, 231, 231, 461, 461, 231, 231, 231, 231, 922, 461, 461,
231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461, 231, 231, 461, 461, 922, 461, 461, 231, 692, 461, 231, 231, 231, 692,
231, 231, 231, 231, 461, 461, 1845, 231, 231, 231, 231, 231, 231, 231, 231, 231, 231, 115, 346, 922, 231, 231, 346, 115, 461, 231,
231, 461, 231, 231, 692, 231, 231, 231, 231, 231, 231, 231, 461, 231, 231, 231, 231, 922, 231, 231, 231, 231, 461, 115, 346, 231,
231, 231, 231, 692, 231, 461, 461, 231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461, 231, 231, 461, 461, 922, 461, 461,
231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461, 461, 1845, 231, 231, 231, 231, 231, 231, 231, 231, 231, 231, 115, 346,
922, 231, 231, 346, 115, 461, 231, 231, 461, 231, 231, 692, 231, 231, 231, 231, 231, 231, 231, 461, 231, 231, 231, 231, 922, 231,
231, 231, 231, 461, 115, 346, 231, 231, 231, 231, 692, 231, 461, 461, 231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461,
231, 231, 461, 461, 922, 461, 461, 231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461, 461, 1845, 231, 231, 231, 231, 231,
231, 231, 231, 231, 231, 115, 346, 922, 231, 231, 346, 115, 461, 231, 231, 461, 231, 231, 692, 231, 231, 231, 231, 231, 231, 231,
461, 231, 231, 231, 231, 922, 231, 231, 231, 231, 461, 115, 346, 231, 231, 231, 231, 692, 231, 461, 461, 231, 692, 461, 231, 231,
231, 692, 231, 231, 231, 231, 461, 231, 231, 461, 461, 922, 461, 461, 231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461,
461, 1845, 231, 231, 231, 231, 231, 231, 231, 231, 231, 231, 115, 346, 922, 231, 231, 346, 115, 461, 231, 231, 461, 231, 231, 692,
231, 231, 231, 231, 231, 231, 231, 461, 231, 231, 231, 231, 922, 231, 231, 231, 231, 461, 115, 346, 231, 231, 231, 231, 692, 231,
461, 461, 231, 692, 461, 231, 231, 231, 692, 231, 231, 231, 231, 461, 231, 231, 461, 461, 922, 461, 461, 231, 692, 461, 231, 231,
231, 692, 231, 231, 231, 231, 461, 461, 1845, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 2963)
Const DATALEN = 967

'
' End inserted light data
'

' loop forever
Do
' If P10 is pressed then we need to change modes

If In(10) = 1 Then

' increment mode
mode = mode + 1

' If we are greater than mode 17 then loop back to zero
If mode > 17 Then
mode = 0
End If

' reset mode variables to their default
mode1current = 0
count1 = 1

' turn off all the lights
Clear

' reset the sleep timer
sleep = 500

' for modes 2-17 just set the right channel on
If mode = 2 Then
Clear
Out 7, 1
Elseif mode = 3 Then
Clear
Out 6, 1
Elseif mode = 4 Then
Clear
Out 5, 1
Elseif mode = 5 Then
Clear
Out 4, 1
Elseif mode = 6 Then
Clear
Out 23, 1
Elseif mode = 7 Then
Clear
Out 22, 1
Elseif mode = 8 Then
Clear
Out 17, 1
Elseif mode = 9 Then
Clear
Out 16, 1
Elseif mode = 10 Then
Clear
Out 40, 1
Elseif mode = 11 Then
Clear
Out 41, 1
Elseif mode = 12 Then
Clear
Out 42, 1
Elseif mode = 13 Then
Clear
Out 43, 1
Elseif mode = 14 Then
Clear
Out 44, 1
Elseif mode = 15 Then
Clear
Out 45, 1
Elseif mode = 16 Then
Clear
Out 46, 1
Elseif mode = 17 Then
Clear
Out 47, 1
End If

' wait for the user to release P10 otherwise we might jump a mode
Do While In(10) = 1
Wait 10
Loop

End If

' if we are in mode 0 - turn on 1 outlet from each group of 8
If mode = 0 Then

' set the lights
PopulateOutbytes count1, count1

' prepare our next lights by shifting our set bit one to the right
count1 = count1 * 2

' if all would be off or we have gone beyond 8 bits reset to 1
If count1 = 0 Or count1 > 128 Then
count1 = 1
End If

' if we are in mode 1 - our main christmas light routine
Elseif mode = 1 Then

' if P11 circuit is closed then we need to stop and get ready to restart. We stay in this state until P12 is closed.
If In(11) = 1 Then
' Turn off all the lights
Clear

' go back to the beginning of the array
mode1current = 0

' we expect P12 should be open right now so wait until it is
Do While In(12) = 0
Wait 10
Loop

' now wait until it is closed
Do While In(12) = 1
Wait 10
Loop

' now we can continue
End If

' as long as we have not finished our arrays
If mode1current <>
' turn on the appropriate lights
PopulateOutbytes DATA1(mode1current), DATA2(mode1current)

' get the sleep time ... ie how long to leave the lights on for
sleep = DATA3(mode1current)

' move to the next array entry
mode1current = mode1current + 1

' a zero sleep is invalid ... default to 1/2 a second
If sleep = 0 Then
sleep = 500
End If

Else

' we have reched the end of the array ... loop back to the beginning after 1/2 a second

' turn the lights off
Clear

' move to the start
mode1current = 0

' sleep for 1/2 a second
sleep = 500

End If

End If

' if for some reason P11 is closed then jump to mode 1 and handle the reset event as normal.
If In(11) = 1 Then
mode = 1
' if P10 is closed then skip the sleep so we go straight to mode switch processing
Elseif In(10) = 1 Then
Else
' sleep the right amount of time
Wait sleep
End If

Loop

' This function takes the two bytes and turns the appropriate circuits on and off
Sub PopulateOutbytes(b1 As Byte, b2 As Byte)

' These are the ports mapped to ByteOuts on the CUSB. The doco here is awful but this works
'0 - 0-7
'1 - 8-15 (input only)
'2 - 16-23
'3 - 24-31
'4 - 32-39
'5 - 40-47

' this is how the bytes are mapped to the Ports
' port 5 - b1
' BIT 128 64 32 16 8 4 2 1
' PORT 47 46 45 44 43 42 41 40

' port 0 - b2
' BIT 128 64 32 16 8 4 2 1
' PORT 7 6 5 4 xx xx xx xx

' port 2 - b2
' BIT 128 64 32 16 8 4 2 1
' PORT 23 22 xx xx xx xx 17 16

' these are the bytes that will be sent to the three ports
Dim outbyte0 As Byte
Dim outbyte2 As Byte
Dim outbyte5 As Byte

' byte 5 is easy it is a direct copy of B1
outbyte5 = b1

' B2 is hard as some bits in byte 2 and byte 0 need to be set. This code does that
outbyte2 = 0
If b2 And 16 Then
outbyte2 = outbyte2 + 128
End If
If b2 And 32 Then
outbyte2 = outbyte2 + 64
End If
If b2 And 64 Then
outbyte2 = outbyte2 + 2
End If
If b2 And 128 Then
outbyte2 = outbyte2 + 1
End If

outbyte0 = 0
If b2 And 1 Then
outbyte0 = outbyte0 + 128
End If
If b2 And 2 Then
outbyte0 = outbyte0 + 64
End If
If b2 And 4 Then
outbyte0 = outbyte0 + 32
End If
If b2 And 8 Then
outbyte0 = outbyte0 + 16
End If

' Now we can set the light circuits
Byteout 5,outbyte5
Byteout 0,outbyte0
Byteout 2,outbyte2

End Sub

' Debugging routine used to flash a port for 1/5th second
Sub Ping(d As Integer)
Out d,1
Wait 200
Out d,0
End Sub

' Turn all the lights off
Sub Clear()
Byteout 5,0
Byteout 0,0
Byteout 2,0
End Sub





In my next post we will look at how I generated 967 items in the arrays without going totally insane.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?