-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobocar.c
129 lines (116 loc) · 2.53 KB
/
robocar.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#define OFF 0
#define BOTH 1
#define LEFT 2
#define RIGHT 3
#define FORWARD 4
#define REVERSE 5
// connect motor controller pins to Arduino digital pins
// motor one
const int enA = 10;
const int in1 = 9;
const int in2 = 8;
// motor two
const int enB = 5;
const int in3 = 7;
const int in4 = 6;
void robocar_motor_speed(int motor, int speed)
{
switch(motor) {
case BOTH:
analogWrite(enA, speed);
analogWrite(enB, speed);
break;
case LEFT:
analogWrite(enA, speed);
break;
case RIGHT:
analogWrite(enB, speed);
break;
default:
Serial.println("Error: invalid motor specified!");
return;
}
}
void robocar_motor_control(int motor, int state)
{
int pin1;
int pin2;
int pin3;
int pin4;
int seta;
int setb;
switch(motor) {
case BOTH:
pin1 = in1;
pin2 = in2;
pin3 = in3;
pin4 = in4;
break;
case LEFT:
pin1 = pin3 = in1;
pin2 = pin4 = in2;
break;
case RIGHT:
pin1 = pin3 = in4;
pin2 = pin4 = in3;
break;
default:
Serial.println("Error: invalid motor specified!");
return;
}
switch(state) {
/* all motors shut off regardless of direction */
case OFF:
seta = LOW;
setb = LOW;
break;
case FORWARD:
seta = LOW;
setb = HIGH;
break;
case REVERSE:
seta = HIGH;
setb = LOW;
break;
}
digitalWrite(pin1, seta);
digitalWrite(pin2, setb);
digitalWrite(pin3, seta);
digitalWrite(pin4, setb);
}
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop()
{
robocar_motor_speed(BOTH, OFF);
robocar_motor_control(BOTH, OFF);
delay(2000);
/* clockwise */
robocar_motor_speed(BOTH, 200);
robocar_motor_control(LEFT, FORWARD);
robocar_motor_control(RIGHT, REVERSE);
delay(5000);
/* counter clockwise */
robocar_motor_speed(BOTH, 200);
robocar_motor_control(LEFT, REVERSE);
robocar_motor_control(RIGHT, FORWARD);
delay(5000);
/* forward */
robocar_motor_speed(BOTH, 200);
robocar_motor_control(LEFT, FORWARD);
robocar_motor_control(RIGHT, FORWARD);
delay(3000);
/* forward */
robocar_motor_speed(BOTH, 200);
robocar_motor_control(LEFT, REVERSE);
robocar_motor_control(RIGHT, REVERSE);
delay(3000);
}