Skip to content

crwdns255366:0crwdne255366:0

crwdns274122:0crwdne274122:0

crwdns255252:0crwdne255252:0 | Python | crwdns255562:0crwdne255562:0, crwdns255564:0crwdne255564:0, crwdns255566:0crwdne255566:0, crwdns255570:0crwdne255570:0 | crwdns258484:0crwdne258484:0, crwdns258490:0crwdne258490:0, crwdns258516:0crwdne258516:0, crwdns258616:0crwdne258616:0

crwdns258736:0crwdne258736:0

crwdns258738:0crwdne258738:0

crwdns258744:0crwdne258744:0

crwdns258752:0crwdne258752:0

  • crwdns258754:0crwdne258754:0
  • crwdns258756:0crwdne258756:0

crwdns258758:0crwdne258758:0

  • crwdns258762:0crwdne258762:0 crwdns258764:0crwdne258764:0
  • crwdns258766:0crwdne258766:0
  • crwdns258768:0crwdne258768:0
  • crwdns258770:0crwdne258770:0
  • crwdns258772:0crwdne258772:0

crwdns258774:0crwdne258774:0

1from microbit import *
2
3# function to read data file:
4def get_nv_data(name):
5    try:
6        with open(name) as f:
7            v = int(f.read())
8    except OSError:
9        v = temperature()
10        try:
11            with open(name, 'w') as f:
12                f.write(str(v))
13        except OSError:
14            display.scroll('Cannot create file %s' % name)
15
16    except ValueError:
17        display.scroll('invalid data in file %s' % name)
18        v = temperature()
19
20    return v
21
22# function to write data file:
23def set_nv_data(name, value):
24    try:
25        with open(name, 'w') as f:
26            f.write(str(value))
27    except OSError:
28        display.scroll('Cannot write to file %s' % name)
29
30min = get_nv_data('min.txt')
31max = get_nv_data('max.txt')
32
33while True:
34    currentTemp = temperature()
35    if currentTemp < min:
36        min = currentTemp
37        set_nv_data('min.txt', min)
38    if currentTemp > max:
39        max = currentTemp
40        set_nv_data('max.txt', max)
41    if accelerometer.was_gesture('shake'):
42        display.scroll(currentTemp)
43    if button_a.was_pressed():
44        display.scroll(get_nv_data('min.txt'))
45    if button_b.was_pressed():
46        display.scroll(get_nv_data('max.txt'))
47    if pin2.is_touched():
48        display.scroll('clearing data')
49        set_nv_data('min.txt', currentTemp)
50        set_nv_data('max.txt', currentTemp)
51

crwdns258776:0crwdne258776:0

crwdns258786:0crwdne258786:0

crwdns258814:0crwdne258814:0

  • crwdns258882:0crwdne258882:0
  • crwdns258884:0crwdne258884:0
crwdns258822:0crwdne258822:0