This repository has been archived by the owner on Nov 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_bot_serial.ino
138 lines (113 loc) · 3.61 KB
/
move_bot_serial.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
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
130
131
132
133
134
135
136
137
138
#define L_MOTOR_POSITIVE 9
#define L_MOTOR_NEGATIVE 10
#define L_MOTOR_ENABLE 3
#define R_MOTOR_POSITIVE 6
#define R_MOTOR_NEGATIVE 11
#define R_MOTOR_ENABLE 5
#define LED_PIN 13
#define MAX_SPEED_R 240
#define MAX_SPEED_L 180
/*
* Movements:
* W := forward
* S := stop
* D := right
* A := left
* R := extreme right
* L := extreme left
* X := reverse
*/
void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
int incomingByte = Serial.read();
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("I received: ");
Serial.println(incomingByte);
if (incomingByte == 'A') {
digitalWrite(L_MOTOR_POSITIVE, LOW);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
digitalWrite(R_MOTOR_POSITIVE, HIGH);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'D') {
digitalWrite(L_MOTOR_POSITIVE, HIGH);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'W') {
digitalWrite(L_MOTOR_POSITIVE, HIGH);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
digitalWrite(R_MOTOR_POSITIVE, HIGH);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'S') {
digitalWrite(L_MOTOR_POSITIVE, LOW);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'R') {
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, HIGH);
digitalWrite(L_MOTOR_POSITIVE, HIGH);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'L') {
digitalWrite(R_MOTOR_POSITIVE, HIGH);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
digitalWrite(L_MOTOR_POSITIVE, LOW);
digitalWrite(L_MOTOR_NEGATIVE, HIGH);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'X') {
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, HIGH);
digitalWrite(L_MOTOR_POSITIVE, LOW);
digitalWrite(L_MOTOR_NEGATIVE, HIGH);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'V') {
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, HIGH);
digitalWrite(L_MOTOR_POSITIVE, HIGH);
digitalWrite(L_MOTOR_NEGATIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
delay(4000);
digitalWrite(R_MOTOR_POSITIVE, LOW);
digitalWrite(R_MOTOR_NEGATIVE, LOW);
digitalWrite(L_MOTOR_POSITIVE, LOW);
digitalWrite(L_MOTOR_POSITIVE, LOW);
analogWrite(L_MOTOR_ENABLE, MAX_SPEED_L);
analogWrite(R_MOTOR_ENABLE, MAX_SPEED_R);
}
else if (incomingByte == 'B') {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
}
}