lab2-5_1.c
875 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
#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) {
if (rotate == LEFT_ROTATE) {
digitalWrite(MOTOR_MT_P_PIN, LOW);
softPwmWrite(MOTOR_MT_N_PIN, 50);
}
else if (rotate == RIGHT_ROTATE) {
digitalWrite(MOTOR_MT_N_PIN, LOW);
softPwmWrite(MOTOR_MT_P_PIN, 50);
}
}
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, 100);
softPwmCreate(MOTOR_MT_P_PIN, 0, 100);
while (1){
MotorControl(LEFT_ROTATE);
delay(2000);
MotorStop();
delay(1000);
MotorControl(RIGHT_ROTATE);
delay(2000);
MotorStop();
delay(1000);
}
return 0;
}