-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_apr11a.ino
executable file
·212 lines (179 loc) · 4.37 KB
/
sketch_apr11a.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//Pins
int switchPin1 = 2;
int switchPin2 = 4;
int switchPin3 = 7;
int buttonPin = 8;
int greenLedPin = 12;
int redLedPin = 13;
int engineForwardPin = 6;
int engineBackwardsPin = 5;
int turnLeftPin = 11;
int turnRightPin = 10;
//Always false at start, sets to true if serial output is enabled.
boolean serialRun = false;
boolean lastActive[2];
void setup() {
//All the pins being set up
pinMode(switchPin1, OUTPUT);
pinMode(switchPin2, OUTPUT);
pinMode(switchPin3, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(engineForwardPin, OUTPUT);
digitalWrite(engineForwardPin, LOW);
pinMode(engineBackwardsPin, OUTPUT);
digitalWrite(engineBackwardsPin, LOW);
pinMode(turnLeftPin, OUTPUT);
digitalWrite(turnLeftPin, LOW);
pinMode(turnRightPin, OUTPUT);
digitalWrite(turnRightPin, LOW);
//Doing the start red LED, and wait for button
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
boolean buttonPress = true;
while(buttonPress) {
if(digitalRead(buttonPin) == HIGH) {
buttonPress = false;
delay(2000);
if(digitalRead(buttonPin) == HIGH) {
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
delay(2000);
loop2();
}
}
}
//When button pressed, turns on green LED
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
}
void loop() {
//Initializing the output string for the serial output
boolean input[2];
//For loop that changes sensor and reads from the sensor that has the LED turned on.
for(int i = 0; i < 2; i++){
//Changes sensor
changeSensor(i);
//The sensors need a delay before reading, because the LEDs are a bit slow to get turned on. Thus this here delay beloweth!
delay(1);
//Getting the reading from the analog port
int reading = analogRead(A4 - i);
if(reading > 85) {
input[i] = true;
} else {
input[i] = false;
}
delay(1);
}
//Printing the input. Outcomment the next method call to diable all serial activity!
//printToSerial(input);
followLine(input);
}
void loop2(){
while(true) {
turn(true);
digitalWrite(engineForwardPin, HIGH);
delay(5000);
}
}
void followLine(boolean input[]) {
if(input[0]) {
if(input[1]){
lastActive[0] = true;
lastActive[1] = true;
driveStraight();
changeDirection(true);
} else {
lastActive[0] = true;
lastActive[1] = false;
driveStraight();
changeDirection(true);
}
} else if(input[1]) {
lastActive[0] = false;
lastActive[1] = true;
driveStraight();
changeDirection(true);
} else {
if(lastActive[0]) {
if(lastActive[1]){
driveStraight();
changeDirection(false);
} else {
turn(true);
}
} else if(lastActive[1]) {
turn(false);
}
}
}
//Method that uses a switch to change which sensor has its LED turned on
void changeSensor(int sensNum) {
digitalWrite(switchPin1, LOW);
digitalWrite(switchPin2, LOW);
digitalWrite(switchPin3, LOW);
switch(sensNum) {
case 1:
digitalWrite(switchPin1, HIGH);
break;
case 2:
digitalWrite(switchPin2, HIGH);
break;
case 3:
digitalWrite(switchPin1, HIGH);
digitalWrite(switchPin2, HIGH);
break;
case 4:
digitalWrite(switchPin3, HIGH);
break;
default:
break;
}
}
void turn(boolean dir) {
digitalWrite(turnLeftPin, LOW);
digitalWrite(turnRightPin, LOW);
delay(3);
if(dir) {
digitalWrite(turnLeftPin, HIGH);
} else {
digitalWrite(turnRightPin, HIGH);
}
}
void driveStraight() {
digitalWrite(turnLeftPin, LOW);
digitalWrite(turnRightPin, LOW);
}
void changeDirection(boolean dir) {
digitalWrite(engineForwardPin, LOW);
digitalWrite(engineBackwardsPin, LOW);
if(dir) {
analogWrite(engineForwardPin, 140);
} else {
analogWrite(engineBackwardsPin, 120);
}
}
void fullBreak() {
digitalWrite(engineForwardPin, LOW);
digitalWrite(engineBackwardsPin, LOW);
digitalWrite(turnLeftPin, LOW);
digitalWrite(turnRightPin, LOW);
delay(100);
}
void printToSerial(boolean input[]) {
if(!serialRun) {
//initializing the serial output
Serial.begin(9600);
serialRun = true;
}
String output = "";
for(int i = 0; i < 2; i++){
if (input[i]) {
output += "|";
} else {
output += "_";
}
}
Serial.println(output);
}