crwdns290712:0crwdne290712:0
crwdns290714:0crwdne290714:0
crwdns290716:0crwdne290716:0 crwdns290718:0crwdne290718:0
crwdns290720:0crwdne290720:0
crwdns290724:0crwdne290724:0
crwdns290728:0crwdne290728:0
crwdns290730:0crwdne290730:0
- crwdns290732:0crwdne290732:0
- crwdns290734:0crwdne290734:0
- crwdns290736:0crwdne290736:0
crwdns290738:0crwdne290738:0
crwdns290740:0crwdne290740:0 crwdns290742:0crwdne290742:0
crwdns290744:0crwdne290744:0
- crwdns290746:0crwdne290746:0 crwdns290748:0crwdne290748:0
- crwdns290750:0crwdne290750:0 crwdns290752:0crwdne290752:0
- crwdns290754:0crwdne290754:0
crwdns290756:0crwdne290756:0
crwdns290758:0crwdne290758:0
crwdns290760:0crwdne290760:0 crwdns290762:0crwdne290762:0
crwdns290764:0crwdne290764:0 crwdns290766:0crwdne290766:0
crwdns290768:0crwdne290768:0 crwdns290770:0crwdne290770:0
crwdns290772:0crwdne290772:0
crwdns290774:0crwdne290774:0
crwdns290776:0crwdne290776:0
crwdns290778:0crwdne290778:0
- crwdns290780:0crwdne290780:0
- crwdns290782:0crwdne290782:0 crwdns290784:0crwdne290784:0 crwdns290786:0crwdne290786:0
- crwdns290788:0crwdne290788:0 crwdns290790:0crwdne290790:0 crwdns290792:0crwdne290792:0
- crwdns290794:0crwdne290794:0
- crwdns290796:0crwdne290796:0
crwdns290798:0crwdne290798:0
- crwdns290800:0crwdne290800:0
- crwdns290802:0crwdne290802:0
- crwdns290804:0crwdne290804:0
crwdns290806:0crwdne290806:0
1from microbit import *
2display.show('L')
3
4LIGHT = 114 # <<< Enter your 'measured' reading here
5WATTS = 1000 # <<< Enter your bulb wattage here
6COSTPERKWH = 0.18 # <<< Enter unit electricity cost here
7
8HYSTERESIS = 8
9LIGHT -= (HYSTERESIS/2)
10DARK = LIGHT - HYSTERESIS
11ON_IMAGE = Image('99999:99999:99999:99999:99999')
12OFF_IMAGE = Image('00000:00000:00900:00000:00000')
13timing = False
14start_time = 0
15total_time = 0
16reading = display.read_light_level()
17sleep(1000)
18
19def calc_cost(m):
20 # Calculate cost of electricity for this number of mins
21 kw = WATTS / 1000.0 # answer as a decimal
22 hours = m / 60.0 # answer as a decimal
23 kwh = kw * hours
24 cost = kwh * COSTPERKWH
25 return cost # as pounds and pence
26
27def show_number(n):
28 # Scroll number as 3 decimal places
29 display.scroll("%.3f" % n)
30
31while True:
32 reading = display.read_light_level()
33 if reading < DARK:
34 if timing:
35 # it has just gone dark, update timer for 'on' time
36 end_time = running_time()
37 total_time += (end_time - start_time)
38 timing = False
39
40 elif reading >= LIGHT:
41 if not timing:
42 # it has just gone light, start the timer
43 start_time = running_time()
44 timing = True
45
46 if button_b.was_pressed():
47 # calculate and display cumulative cost in pounds and pence
48 minutes = total_time / 60000
49 if timing:
50 # adjust live for current ON time
51 minutes += (running_time() - start_time) / 60000
52 display.clear()
53 show_number(calc_cost(minutes))
54 sleep(500)
55
56 # update the display with the ON/OFF state
57 if timing:
58 display.show(ON_IMAGE)
59 else:
60 display.show(OFF_IMAGE)
61 sleep(1000)
62
63
crwdns290808:0crwdne290808:0
- crwdns290810:0crwdne290810:0
- crwdns290812:0crwdne290812:0
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.