-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrobit_pov_motor.py
64 lines (51 loc) · 1.84 KB
/
microbit_pov_motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
microbit_pov_motor.py
https://www.instructables.com/Microbit-Persistence-of-Vision-Daisy
Part of "micro:bit Persistence-of-Vision daisy"
Copyright 2021 Pavlos Iliopoulos, techprolet.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
"""
from microbit import *
startPeriod = 10000
targetPeriod = 1000
pwm_value = 511
speedChangeWaitTime = 1
# accelerate motor till we reach the desired speed
def MotorTurn():
global startPeriod
global targetPeriod
global speedChangeWaitTime
global pwm_value
thisPeriod = startPeriod
pin1.write_analog(pwm_value)
for i in range(targetPeriod, startPeriod):
pin1.set_analog_period_microseconds(thisPeriod)
sleep(speedChangeWaitTime)
thisPeriod -= 1
# decelerate motor before stopping completely
def MotorStop():
global startPeriod
global targetPeriod
global speedChangeWaitTime
thisPeriod = targetPeriod
for i in range(targetPeriod, startPeriod):
pin1.set_analog_period_microseconds(thisPeriod)
sleep(speedChangeWaitTime)
thisPeriod += 1
pin1.write_digital(0)
# button B starts motor, button A stops it
while True:
if button_b.is_pressed():
MotorTurn()
if button_a.is_pressed():
MotorStop()
sleep(1)