-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1.ino
180 lines (147 loc) · 3 KB
/
P1.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Made by Francis Bui
#include <Stepper.h>
char t;
// Sets steps per revolution and actuation
const int stepsPerRevolution = 2048;
const int actuate = 600;
// Stepper flag
int stepflag = 0;
// H-BRIDGE MOTOR, DRIVE
#define IN1 5
#define IN2 4
#define IN3 3
#define IN4 2
// RELAY, INGITION
#define RPIN 6
// STEPPER MOTOR, GAS TANK ACTUATION
#define STEP1 7
#define STEP2 8
#define STEP3 9
#define STEP4 10
// Creates stepper object
Stepper myStepper = Stepper(stepsPerRevolution, 7, 9, 8, 10);
void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(RPIN,OUTPUT); // Relay
pinMode(STEP1, OUTPUT);
pinMode(STEP2, OUTPUT);
pinMode(STEP3, OUTPUT);
pinMode(STEP4, OUTPUT);
// Set stepper speed
myStepper.setSpeed(15);
Serial.begin(9600);
}
// Reads bluetooth serial input
void loop() {
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
if(t == 'w'){ // Move forward (all motors rotate in forward direction)
forward();
}
else if(t == 's'){ //Move reverse (all motors rotate in reverse direction)
reverse();
}
else if(t == 'a'){ //Turn right (left side motors rotate in forward direction, right side motors reverse)
left();
}
else if(t == 'd'){ //Turn left (right side motors rotate in forward direction, left side motors reverse)
right();
}
else if(t == 'r'){ // Turn relay on
relayon();
}
else if(t == 'f'){ // Turn relay off
relayoff();
}
else if(t == 't' and stepflag == 0){ // Turn stepper forward
steppergo();
relayon();
stepflag += 1;
}
else if(t == 'y' and stepflag == 1){ // Turn stepper reverse
stepperstop();
relayoff();
stepflag -= 1;
}
else if (t == 'z'){ // Stops robot (all motors stop)
stoprobot();
}
else if (t == '1') { // Autonomous
simpleautopath();
}
delay(50);
}
void forward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void reverse()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void left()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void right()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void stoprobot()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void relayon()
{
digitalWrite(RPIN, HIGH);
}
void relayoff()
{
digitalWrite(RPIN, LOW);
}
void steppergo()
{
myStepper.step(actuate);
}
void stepperstop()
{
myStepper.step(-actuate);
}
void simpleautopath()
{
stoprobot();
forward();
delay(2000);
left();
delay(50);
right();
delay(50);
forward();
delay(200);
relayon();
steppergo();
delay(150);
stepperstop();
relayoff();
}