Initial commit of Xmas tree
[pi-xmas.git] / xmas_tree / example_5.py
1 import tree
2 import random
3
4 # Some constants to identify each LED
5 L0 = 1
6 L1 = 2
7 L2 = 4
8 L3 = 8
9 L4 = 16
10 L5 = 32
11 L6 = 64
12 ALL = 1+2+4+8+16+32+64
13 NO_LEDS = 0
14
15 tree.setup() # you must always call setup() first!
16
17 # Two ways of randomly illuminating LEDs (they do the
18 # same thing but Way 1 is easier to understand whilst
19 # Way 2 is a shorter piece of code).
20
21 # Way 1
22 for i in range(100): # repeat 100 times
23 random_led = random.randint(0, 6)
24 if (random_led == 0): tree.leds_on_and_wait(L0, 0.2) # D0 on for 0.2s
25 elif (random_led == 1): tree.leds_on_and_wait(L1, 0.2) # D1 on for 0.2s
26 elif (random_led == 2): tree.leds_on_and_wait(L2, 0.2) # D2 on for 0.2s
27 elif (random_led == 3): tree.leds_on_and_wait(L3, 0.2) # D3 on for 0.2s
28 elif (random_led == 4): tree.leds_on_and_wait(L4, 0.2) # D4 on for 0.2s
29 elif (random_led == 5): tree.leds_on_and_wait(L5, 0.2) # D5 on for 0.2s
30 elif (random_led == 6): tree.leds_on_and_wait(L6, 0.2) # D6 on for 0.2s
31
32 # Way 2
33 for i in range(100): # repeat 100 times
34 random_led = random.randint(0, 6)
35 tree.leds_on_and_wait(1<<random_led, 0.5) # randomly selected LED on for 0.2s
36
37
38 tree.all_leds_off() # extinguish all LEDs
39
40 # All done!
41 tree.cleanup() # call cleanup() at the end
42