-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_pwm_driver.ino
60 lines (49 loc) · 1.25 KB
/
serial_pwm_driver.ino
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
// Motor Control Pins
#define MOTOR_1_F 3
#define MOTOR_1_R 9
#define MOTOR_2_F 10
#define MOTOR_2_R 11
// Lidar PWM
#define LIDAR 5
// Servo PWM
#define SERVO 6
void setup() {
Serial.begin(115200);
pinMode(MOTOR_1_F, OUTPUT);
pinMode(MOTOR_1_R, OUTPUT);
pinMode(MOTOR_2_F, OUTPUT);
pinMode(MOTOR_2_R, OUTPUT);
}
void loop() {
// When there is serial data available
while (Serial.available() > 0) {
// Capture the key
int header = Serial.read();
delayMicroseconds(500);
int value = Serial.read();
// Classify according to key
if (header == 10) {
analogWrite(MOTOR_1_R, 0);
analogWrite(MOTOR_1_F, value);
Serial.write(value);// Actuate the value
}
else if (header == 15) {
analogWrite(MOTOR_1_F, 0);
analogWrite(MOTOR_1_R, value); // Actuate the value
}
else if (header == 20) {
analogWrite(MOTOR_2_R, 0);
analogWrite(MOTOR_2_F, value); // Actuate the value
}
else if (header == 25) {
analogWrite(MOTOR_2_F, 0);
analogWrite(MOTOR_2_R, value); // Actuate the value
}
else if (header == 30) {
analogWrite(LIDAR, value); // Actuate the value
}
else if (header == 40) {
analogWrite(SERVO, value); // Actuate the value
}
}
}