147
Chapter 13 Motor & Driver
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
#define A0 p 0
#define A1 p 1
#define A2 p 2
#define A3 p 3
#define motoRPin1 2
// define the pin connected to L293D
#define motoRPin2 0
#define enablePin 3
// Map function: map the value from a range of mapping to another range .
long
map
(
long
value
,
long
fromLow
,
long
fromHigh
,
long
toLow
,
long
toHigh
){
return
(
toHigh
-
toLow
)*(
value
-
fromLow
)
/
(
fromHigh
-
fromLow
)
+
toLow
;
}
//motor function: determine the direction and speed of the motor according to the ADC
value to be input.
void
motor
(
int
ADC
){
int
value
=
ADC
-
128
;
if
(
value
>
0
){
digitalWrite
(
motoRPin1
,
HIGH
);
digitalWrite
(
motoRPin2
,
LOW
);
printf
(
"turn Forward...\n"
);
}
else
if
(
value
<
0
){
digitalWrite
(
motoRPin1
,
LOW
);
digitalWrite
(
motoRPin2
,
HIGH
);
printf
(
"turn Back...\n"
);
}
else
{
digitalWrite
(
motoRPin1
,
LOW
);
digitalWrite
(
motoRPin2
,
LOW
);
printf
(
"Motor Stop...\n"
);
}
softPwmWrite
(
enablePin
,
map
(
abs
(
value
),
0
,
128
,
0
,
100
));
printf
(
"The PWM duty cycle is %d%%\n"
,
abs
(
value
)*
100
/
127
);//print the PWM duty cycle.
}
int
main
(
void
){
int
value
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed,print message to screen
printf
(
"setup wiringPi failed !"
);
return
1
;
}
pinMode
(
enablePin
,
OUTPUT
);// set mode for the pin
pinMode
(
motoRPin1
,
OUTPUT
);
pinMode
(
motoRPin2
,
OUTPUT
);
softPwmCreate
(
enablePin
,
0
,
100
);// define PWM pin
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 ...