lab2-5-1.c
942 Bytes
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
#include <wiringPi.h>
#include <softPwm.h>
#define MOTOR_MT_N_PIN 17
#define MOTOR_MT_P_PIN 4
#define LEFT_ROTATE 1
#define RIGHT_ROTATE 2
void MotorStop() {
softPwmWrite(MOTOR_MT_N_PIN, 0);
softPwmWrite(MOTOR_MT_P_PIN, 0);
}
void MotorControl(int rotate, int speed) {
if (rotate == LEFT_ROTATE) {
digitalWrite(MOTOR_MT_P_PIN, LOW);
softPwmWrite(MOTOR_MT_N_PIN, speed);
}
else if (rotate == RIGHT_ROTATE) {
digitalWrite(MOTOR_MT_N_PIN, LOW);
softPwmWrite(MOTOR_MT_P_PIN, speed);
}
}
int main(void){
if (wiringPiSetupGpio() == -1)
return 1;
pinMode(MOTOR_MT_N_PIN, OUTPUT);
pinMode(MOTOR_MT_P_PIN, OUTPUT);
softPwmCreate(MOTOR_MT_N_PIN, 0, 128);
softPwmCreate(MOTOR_MT_P_PIN, 0, 128);
int i;
for(i=1;i<5;i++){
MotorControl(LEFT_ROTATE,i*32);
delay(2000);
MotorStop();
delay(2000);
}
for(i=4;i>0;i--){
MotorControl(RIGHT_ROTATE,i*32);
delay(2000);
MotorStop();
delay(2000);
}
return 0;
}