Skip to content

crwdns342183:0crwdne342183:0

crwdns312390:0crwdne312390:0

crwdns347529:0crwdne347529:0 | MakeCode, Python | crwdns255560:0crwdne255560:0, crwdns255566:0crwdne255566:0, crwdns255570:0crwdne255570:0 | crwdns258500:0crwdne258500:0, crwdns258502:0crwdne258502:0, crwdns258516:0crwdne258516:0, crwdns258616:0crwdne258616:0, crwdns273630:0crwdne273630:0

crwdns290902:0crwdne290902:0

crwdns290904:0crwdne290904:0

crwdns290906:0crwdne290906:0 crwdns290908:0crwdne290908:0

crwdns290910:0crwdne290910:0

crwdns290914:0crwdne290914:0

  • crwdns290916:0crwdne290916:0
  • crwdns290918:0crwdne290918:0
  • crwdns290920:0crwdne290920:0
  • crwdns290922:0crwdne290922:0
  • crwdns290924:0crwdne290924:0

crwdns290926:0crwdne290926:0

  • crwdns290928:0crwdne290928:0 crwdns290930:0crwdne290930:0
  • crwdns290932:0crwdne290932:0 crwdns290934:0crwdne290934:0
  • crwdns290936:0crwdne290936:0 crwdns290938:0crwdne290938:0 crwdns290940:0crwdne290940:0
  • crwdns290942:0crwdne290942:0

crwdns290944:0crwdne290944:0

  • crwdns290946:0crwdne290946:0 crwdns290948:0crwdne290948:0
  • crwdns290950:0crwdne290950:0 crwdns290952:0crwdne290952:0
  • crwdns290954:0crwdne290954:0 crwdns290956:0crwdne290956:0
  • crwdns290958:0crwdne290958:0 crwdns290960:0crwdne290960:0 crwdns290962:0crwdne290962:0

crwdns290964:0crwdne290964:0

  • crwdns290966:0crwdne290966:0
  • crwdns290968:0crwdne290968:0
  • crwdns290970:0crwdne290970:0

crwdns290972:0crwdne290972:0

1from microbit import *
2display.show('L')
3
4LIGHT = 100  # <<< Enter your 'measured' reading here
5
6HYSTERESIS = 8
7LIGHT -= (HYSTERESIS/2)
8DARK = LIGHT - HYSTERESIS
9ON_IMAGE = Image('99999:99999:99999:99999:99999')
10OFF_IMAGE = Image('00000:00000:00900:00000:00000')
11timing = False
12start_time = 0
13total_time = 0
14reading = display.read_light_level()
15sleep(1000)
16
17def show_number(n):
18    # Make number display the same as MakeCode
19    if len(str(n)) == 1:
20        display.show(n)
21    else:
22        display.scroll(n)
23
24while True:
25    reading = display.read_light_level()
26    if reading < DARK:
27        if timing:
28            # it has just gone dark, update timer for 'on' time
29            end_time = running_time()
30            total_time += (end_time - start_time)
31            timing = False
32        
33    elif reading >= LIGHT:
34        if not timing:
35            # it has just gone light, start the timer
36            start_time = running_time()
37            timing = True
38        
39    if button_b.was_pressed():
40        # calculate and display cumulative time in minutes
41        minutes = total_time / 60000
42        if timing:
43            # adjust live for current ON time
44            minutes += (running_time() - start_time) / 60000
45        display.clear()
46        show_number(round(minutes))  # whole numbers only
47        sleep(500)
48
49    # update the display with the ON/OFF state
50    if timing:
51        display.show(ON_IMAGE)
52    else:
53        display.show(OFF_IMAGE)
54    sleep(1000)
55
56        

crwdns290974:0crwdne290974:0

  • crwdns290976:0crwdne290976:0
  • crwdns290978:0crwdne290978:0 crwdns290980:0crwdne290980:0
  • crwdns290982:0crwdne290982:0 crwdns290984:0crwdne290984:0 crwdns290986:0crwdne290986:0