-
Notifications
You must be signed in to change notification settings - Fork 0
/
microcontroller.ino
211 lines (143 loc) · 6.96 KB
/
microcontroller.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
int array[4]; //initialise an array to hold the values of the 4 LDRs
byte index = 0; //set index as 0
const int FLpin = A5; //Set front left LDR pin
const int FRpin = A2; //Set front right LDR pin
const int BLpin = A3; //Set back left LDR pin
const int BRpin = A4; //Set back right LDR pin
int value = 0;
int largest = 0; //variables for finding which LDR is recieving the most light
void setup() {
// Setup code, to run once:
// Setup channel A (write pin 3 for speed)
pinMode(12, OUTPUT); // Initiates motor channel A pin
pinMode(9, OUTPUT); // Initiates brake channel A pin
//Setup channel B (write pin 11 for speed)
pinMode(13, OUTPUT); //Initiates motor channel B pin
pinMode(8, OUTPUT); //Initiates brake channel B pin
Serial.begin(9600);
pinMode(FLpin, INPUT); //Initiates front left LDR pin
pinMode(FRpin, INPUT); //Initiates front right LDR pin
pinMode(BLpin, INPUT); //Initiates back left LDR pin
pinMode(BRpin, INPUT); //Initiates back right LDR pin
}
void loop() {
Serial.println(123); //marker used for troubleshooting the code using the serial monitor
//Main code, to loop:
array[0] = analogRead(FLpin); //Reads front left LDR pin into array value x=8
Serial.println(array[0]);
array[1] = analogRead(FLpin); //Reads front right LDR pin into array value x=1
Serial.println(array[1]);
array[2] = analogRead(BLpin); //Reads back left LDR pin into array value x=2
Serial.println(array[2]);
array[3] = analogRead(BRpin); //Reads back right LDR pin into array value x=3
Serial.println(array[3]);
delay(100);
for (byte x = 0; x < 4; x++) { //Reads array to output x value of LDR recieving the most Light
value = array[x];
if (x == 0) {
largest = value;
index = 0;
}
if (value > largest) {
largest = value;
index = x;
}
}
}
largest = 0;
Serial.println(index); //print which LDR is recieving the most Light
delay(1000);
switch (index) {
case 0: //turn left 34 degrees
digitalWrite(12, LOW); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, HIGH); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(11, 255); //Spins the motor on channel B at full speed
delay(200):
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay (100);
//driving forward 1s
digitalWrite(12, HIGH); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, HIGH); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(11, 255); //Spins the motor on channel B at full speed
delay(1000);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay(100):
break;
case 1: //turn right 34 degrees
digitalWrite(12, HIGH); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, LOW); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(1l, 255); //Spins the motor on channel B at full speed
delay(200);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitelWrite(8, HIGH); //Engage the brake for channel B
delay(100);
//driving forward 1s
digitalWrite(12, HIGH); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, HIGH); //Eeteblishes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(11, 255); //Spins the sotor on channel B at full speed
delay(1000);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay(100);
break;
case 2: //turn left 158 degrees
digitalWrite(12, LOW); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, HIGH); //Establisnes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(1l, 255); //Spias the motor on channel B at full speed
deley(600);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay(100);
//driving forward 1s
digitalWrite(12, HIGH); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spine the motor on channel A at full speed
digitalWrite(13, HIGH); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengage the Bcake for channel B
analogWrite(11, 255); //Spins the motor on channel B at full speed
delay(1000);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay(100);
break;
case 3: //turn right 158 degrees
digitalWrite(12, HIGH); //Establishes direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, LOW); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengage the brake for channel B
analogWrite(11, 255); //Spins the motor on channel B at full speed
delay(600);
digitalWrite(9, HIGH); //Engage the brake for channel A
analogWrite(8, HIGH); //Engage the brake for channel B
delay(100);
//driving forward 1s
digitalWrite(12, HIGH); //Establishes direction of channel A
analogWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
digitalWrite(13, HIGH); //Establishes direction of channel B
digitalWrite(8, LOW); //Disengege the brake for channel B
analogWrite(11, 255); //Spins the motor on channel B at full speed
delay(1000);
digitalWrite(9, HIGH); //Engage the brake for channel A
digitalWrite(8, HIGH); //Engage the brake for channel B
delay(100);
break;
}