-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotor_code_only.c
More file actions
126 lines (105 loc) · 2.2 KB
/
Copy pathmotor_code_only.c
File metadata and controls
126 lines (105 loc) · 2.2 KB
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
/** NOTE: I have been uploading through the arduino IDE, unsure if certain
* headers need to be otherwise included
*
* use https://lastminuteengineers.com/l298n-dc-stepper-driver-arduino-tutorial/
* for info on how to use the L298 driver.
*/
// naming conventions as per L298 diver specs.
int enA = 5;
int enB = 6;
int in1 = 3;
int in2 = 4;
int in3 = 8;
int in4 = 7;
int power = 100;
void setup() {
// NOTE: enA, enB are connected to analog pins, can out anything from 0 to 1.
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// initialise motors, off.
}
void forwards() {
curve(2);
}
void backwards() {
analogWrite(enA, power);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enB, power);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void left() {
analogWrite(enA, 0);
// right wheel
analogWrite(enB, power - 30);
delay(100);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(100);
}
void right() {
analogWrite(enA, power - 30);
// right wheel
analogWrite(enB, 0);
delay(100);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(100);
}
void stop(int amount) {
analogWrite(enA, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enB, 0);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(amount);
}
/*
* @param amount, positive curve left, negative curve right.
*/
void curve(int amount) {
// left wheel
analogWrite(enA, power - amount);
// right wheel
analogWrite(enB, power + amount);
delay(100);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(100);
}
void leftLean(int amount) {
curve(12);
delay(amount);
forwards();
delay(5000);
}
void rightLean(int amount) {
curve(-10);
delay(amount);
forwards();
delay(5000);
}
void uTurn() {
curve(16);
delay(2800);
}
void loop() {
// rightLean(1000);
// stop(800);
// leftLean(1000);
// stop(800);
uTurn();
stop(800);
}