151
Chapter 13 Motor & Driver
64
65
66
67
68
69
p r int
(
'Program is starting ... '
)
setup
()
t r y
:
loop
( )
e x cept
KeyboardInterrupt
:
destroy
()
We have been familiar with reading ADC value. So, let’s learn directly subfunction
d e f
motor
(
ADC
)
: first,
compare ADC value with 128 (value corresponding to midpoint). When the current ADC value is higher,
motoRPin1 outputs high level and motoRPin2 output low level to control motor to run with forward rotation
direction. When the current ADC value is lower, motoRPin1 outputs low level and motoRPin2 outputs high
level to control run with reversed direction. When the ADC value is equal to 128, make motoRPin1 and
motoRPin2 output low level, then the motor stops. And then determine PWM duty cycle according to the
difference between ADC value and 128. Because the absolute difference value stays within 0-128. We need
to use the map () subfunction mapping the difference value to range of 0-100. Finally print out the duty cycle.
d e f
motor
(
ADC
):
value
=
ADC
-
128
i f
(
value
>
0
) :
GPIO
.
output
(
motoRPin1
,
GPIO
.
HIGH
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
LOW
)
p r int
(
'Turn Forward...'
)
e l if
(
value
<
0
) :
GPIO
.
output
(
motoRPin1
,
GPIO
.
LOW
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
HIGH
)
p r int
(
'Turn Backward...'
)
e l se
:
GPIO
.
output
(
motoRPin1
,
GPIO
.
LOW
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
LOW
)
p r int
(
'Motor Stop...'
)
p
.
start
(
mapNUM
(
abs
(
value
),
0
,
128
,
0
,
100
))
p r int
(
'The PWM duty cycle is %d%%\n'
%(
abs
(
value
)*
100
/
127
))
#print PMW duty cycle.
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...