-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbot.ino
188 lines (170 loc) · 4.48 KB
/
lightbot.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
#include <MotorShield.h>
#include <Ultrasonic.h>
// A0 : Motor Current A
// A1 : Motor Current B
// A2 : LDR Right
// A3 : LDR Central
// A4 : LDR Left
// A5 :
// D0 : Serial Tx
// D1 : Serial Rx
// D2 : Switch
// D3~ : Motor PWM A
// D4 : Red LED
// D5~ : Green LED
// D6~ : Ultrasound Echo
// D7 : Ultrasound Trigger
// D8 : Motor Brake B
// D9~ : Motor Brake A
// D10~ : Yellow LED
// D11~ : Motor PWM B
// D12 : Motor Dir A
// D13 : Motor Dir B
//Light Sensor Parameters
int leftValue = 0;
int centralValue = 0;
int rightValue = 0;
int rightPin = A5;
int centralPin = A4;
int leftPin = A3;
int sensitivity = 100; //Only follow single LDR if above this value, else follow two
int threshold = 0; //Lowest light level it will follow
//Ultrasonic Parameters
int trigPin = 7;
int echoPin = 6;
long distance = 0;
long minDistance = 10;
Ultrasonic ultrasonic(trigPin,echoPin);
//Motor Parameters
int maxSpeed = 255;
MS_DCMotor motorR(MOTOR_A);
MS_DCMotor motorL(MOTOR_B);
//LED Parameters
int redPin = 4;
int greenPin = 5;
int yellowPin = 10;
long yellowBlinkRate = 250;
long yellowPrevious = 0;
unsigned long yellowCurrent = 0;
int yellowState = HIGH;
//Switch Parameters
int switchPin = 2;
void setup() {
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, yellowState);
motorL.run(BRAKE);
motorL.setSpeed(maxSpeed);
motorR.run(BRAKE);
motorR.setSpeed(maxSpeed);
Serial.begin(9600);
}
void loop() {
if(digitalRead(switchPin) == HIGH){
motorL.run(BRAKE);
motorR.run(BRAKE);
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
yellowCurrent = millis();
if(yellowCurrent - yellowPrevious > yellowBlinkRate) {
if(yellowState == HIGH){
yellowState = LOW;
}
else{
yellowState = HIGH;
}
yellowPrevious = yellowCurrent;
digitalWrite(yellowPin,yellowState);
}
}
else{
yellowState = HIGH;
digitalWrite(yellowPin,yellowState);
//Collect Values
leftValue = analogRead(leftPin);
centralValue = analogRead(centralPin);
rightValue = analogRead(rightPin);
distance = ultrasonic.Ranging(CM);
//Adjust values
leftValue = 1023 - leftValue;
centralValue = 1023 - centralValue;
rightValue = 1023 - rightValue;
Serial.print(leftValue);
Serial.print(" - ");
Serial.print(centralValue);
Serial.print(" - ");
Serial.println(rightValue);
digitalWrite(yellowPin,LOW);
if(distance > minDistance){
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
if((leftValue > (centralValue+sensitivity)) & (leftValue > (rightValue+sensitivity))){
if(leftValue > threshold){
motorL.run(BACKWARD|RELEASE);
motorR.run(FORWARD|RELEASE);
}
else{
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
else if((centralValue > (leftValue+sensitivity)) & (centralValue > (rightValue+sensitivity))){
if(centralValue > threshold){
motorL.run(FORWARD|RELEASE);
motorR.run(FORWARD|RELEASE);
}
else{
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
else if((rightValue > (centralValue+sensitivity)) & (rightValue > (leftValue+sensitivity))){
if(rightValue > threshold){;
motorL.run(FORWARD|RELEASE);
motorR.run(BACKWARD|RELEASE);
}
else{
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
else{
if((leftValue + centralValue)>(rightValue + centralValue)){
if((leftValue + centralValue) > (threshold*2)){;
motorL.run(BACKWARD|RELEASE);
motorR.run(FORWARD|RELEASE);
}
else{
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
else if((rightValue + centralValue)>(leftValue + centralValue)){
if((leftValue + centralValue) > (threshold*2)){;
motorL.run(FORWARD|RELEASE);
motorR.run(BACKWARD|RELEASE);
}
else{
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
else{
//All three are the same. Do nothing
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
}
else{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
motorL.run(BRAKE);
motorR.run(BRAKE);
}
}
//delay(10);
}