EasyManua.ls Logo

Freenove Ultimate Starter Kit

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
147
Chapter 13 Motor & Driver
ā–ˆ www.freenove.com
support@freenove.com ā–ˆ
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 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 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

Table of Contents