PROJECTS micro:bit

# Smart Fan Control System with micro:bit

DFRobot Mar 27 2018 1349

### Implementation

#### Python Code of Demo 1

```from microbit import *
mode = [700, 900, 1023]
while True:
for speed in mode:
print(speed)
pin8.write_analog(speed)
sleep(1000) ```

#### Python Code of Demo 2

```from microbit import *
fan_switch = False
while True:
fan_switch = not fan_switch
print('ON' if fan_switch else 'OFF')
pin8.write_digital(int(fan_switch))
sleep(100) ```

#### Python Code of Demo 3

```from microbit import *
fan_switch = False
fan_mode = ["slow", "medium","fast"]
fan_power = [700, 900, 1023]
def fan_mode_switch(value):
interval = int(1023/3)
for i in range(3):
if value >= i * interval and value < (i+1)*interval:
break
return fan_mode[i], fan_power[i]

previous_mode, power = fan_mode_switch(pin0.read_analog())

while True:
fan_switch = not fan_switch
print('Fan status: ' + ('ON' if fan_switch else 'OFF'))
sleep(100)
if fan_switch:
mode, power = fan_mode_switch(pin0.read_analog())
if mode is not previous_mode:
previous_mode = mode
print("Fan mode: ", mode)
pin8.write_analog(power)
else:
previous_mode = 'OFF'
pin8.write_analog(0)
sleep(300) ```

#### Python Code of Demo 4

```from microbit import *
fan_switch = False
fan_mode = ["slow", "medium","fast"]
fan_power = [700, 900, 1023]
def fan_mode_switch(value):
interval = int(1023/3)
for i in range(3):
if value >= i * interval and value < (i+1)*interval:
break
return fan_mode[i], fan_power[i]

previous_mode, power = fan_mode_switch(pin0.read_analog())

while True:
fan_switch = not fan_switch
print('Fan status: ' + ('ON' if fan_switch else 'OFF'))
sleep(100)
if fan_switch:
mode, power = fan_mode_switch(pin0.read_analog())
if mode is not previous_mode:
previous_mode = mode
print("Fan mode: ", mode)
pin8.write_analog(power)
fan_switch = True
print('Fan status: ' + 'ON')
else:
previous_mode = 'OFF'
pin8.write_analog(0)
sleep(300) ```