-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_backup.ino
111 lines (87 loc) · 2.16 KB
/
arduino_backup.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
/* Something we wrote 5 mins before our training competetion as our ESP32 fired */
const int motor1Pin1 = 9;
const int motor1Pin2 = 8;
const int motor1Enable = 10;
const int motor2Pin1 = 7;
const int motor2Pin2 = 6;
const int motor2Enable = 11;
void setup() {
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Enable, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2Enable, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
lastCommand = command;
}
switch (lastCommand) {
case 'w':
moveForward();
break;
case 's':
moveBackward();
break;
case 'a':
turnRight();
break;
case 'd':
turnLeft();
break;
case 'x':
stopMotors();
break;
default:
stopMotors();
break;
}
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor1Enable, motorSpeed);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(motor2Enable, motorSpeed);
delay(500);
}
void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
analogWrite(motor1Enable, motorSpeed);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(motor2Enable, motorSpeed);
delay(300);
}
void turnLeft() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor1Enable, motorSpeed);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(motor2Enable, motorSpeed);
delay(300);
}
void turnRight() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
analogWrite(motor1Enable, motorSpeed);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(motor2Enable, motorSpeed);
delay(300);
}
void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor1Enable, 0);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
analogWrite(motor2Enable, 0);
delay(300);
}