-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorinput.pde
111 lines (96 loc) · 2.28 KB
/
motorinput.pde
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
//LeftWheel pins are 6 and 8
int leftWheelSpeedPin = 6;
int leftWheelDirectionPin = 8;
//RightWheel pins are 7 and 9
int rightWheelSpeedPin = 9;
int rightWheelDirectionPin = 7;
int fwd = LOW;
int back = HIGH;
void setup()
{
pinMode(leftWheelSpeedPin, OUTPUT);
pinMode(leftWheelDirectionPin, OUTPUT);
pinMode(rightWheelSpeedPin, OUTPUT);
pinMode(rightWheelDirectionPin, OUTPUT);
Serial.begin(9600);
}
boolean done = false;
int val;
void loop()
{
val = Serial.read();
if (val == 'f') {
forward();
}
if (val == 'b') {
backward();
}
if (val == 'l') {
left(45);
}
if (val == 'r') {
right(45);
}
if (val == 'a') {
left(90);
}
if (val == 'c') {
right(90);
}
if (val == 's') {
spinLeft();
}
if (val == 'd') {
spinLeft();
spinRight();
}
stop();
}
void stop(){
digitalWrite(leftWheelSpeedPin, LOW);
digitalWrite(rightWheelSpeedPin, LOW);
}
void forward() {
digitalWrite (leftWheelDirectionPin, fwd);
digitalWrite (rightWheelDirectionPin, fwd);
analogWrite (leftWheelSpeedPin, 255);
analogWrite (rightWheelSpeedPin, 255);
delay(200);
}
void backward() {
digitalWrite (leftWheelDirectionPin, back);
digitalWrite (rightWheelDirectionPin, back);
analogWrite (leftWheelSpeedPin, 255);
analogWrite (rightWheelSpeedPin, 255);
delay(200);
}
void spinLeft() {
digitalWrite (leftWheelDirectionPin, back);
analogWrite (leftWheelSpeedPin, 255);
digitalWrite (rightWheelDirectionPin, fwd);
analogWrite (rightWheelSpeedPin, 255);
delay(1000);
}
void spinRight() {
digitalWrite (rightWheelDirectionPin, back);
analogWrite (rightWheelSpeedPin, 255);
digitalWrite (lefttWheelDirectionPin, fwd);
analogWrite (leftWheelSpeedPin, 255);
delay(1000);
}
void left(int degree) {
turn(rightWheelSpeedPin, rightWheelDirectionPin, leftWheelSpeedPin, leftWheelDirectionPin, degree);
}
void right(int degree) {
turn(leftWheelSpeedPin, leftWheelDirectionPin, rightWheelSpeedPin, rightWheelDirectionPin, degree);
}
void turn(int backSpeed, int backDirection, int fwdSpeed, int fwdDirection, int degree) {
analogWrite (backSpeed, 0);
if (degree == 90) {
digitalWrite (backDirection, back);
analogWrite (backSpeed, 255);
}
digitalWrite (fwdDirection, fwd);
analogWrite (fwdSpeed, 255);
delay(200);
}