-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher_Final.ino
324 lines (250 loc) · 8.37 KB
/
Launcher_Final.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <Wire.h> // Include Wire if you're using I2C
#include <SPI.h> // Include SPI if you're using SPI
#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library
#include <Servo.h> // include Servo library
#include <Adafruit_NeoPixel.h> //include neopixel library
Servo feederServo;
Servo launcherServo;
//////////////////////////
// MicroOLED Definition //
//////////////////////////
#define PIN_RESET 8 // Connect RST to pin 9
#define PIN_DC 7 // Connect DC to pin 8
#define PIN_CS 9 // Connect CS to pin 10
#define DC_JUMPER 0
//////////////////////////////////
// MicroOLED Object Declaration //
//////////////////////////////////
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
//MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration
//////////////////////////////////
// Sonar Object Declaration //
//////////////////////////////////
#define echoPin 4 // Echo Pin
#define trigPin 5 // Trigger Pin
//////////////////////////////////
// Neopixel Declaration //
//////////////////////////////////
#define PIN 10
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
//#define PressureStatusLEDPin 10 // Pressure Status LED
//#define ObjectStatusLEDPin 12 // Object Status LED
#define MotorStatusLEDPin 6 // Motor Status LED used to drive relay
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
////////////////////////////////////////
// Pressure Sensor Object Declaration //
////////////////////////////////////////
long pressValue;
////////////////////////////////////////
// Counter Object Declaration //
////////////////////////////////////////
int countValue, feedValue, ratio;
////////////////////////////////////////
// Setup //
////////////////////////////////////////
void setup() {
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.display(); // Display what's in the buffer (splashscreen)
delay(1000); // Delay 1000 ms
oled.clear(PAGE); // Clear the buffer.
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//pinMode(PressureStatusLEDPin, OUTPUT); // Use LED indicator (if required)
//pinMode(ObjectStatusLEDPin, OUTPUT); // Use LED indicator (if required)
pinMode(MotorStatusLEDPin, OUTPUT); // Use LED indicator (if required)
countValue = 0;
feedValue = 0;
feederServo.attach(2); // servo control pin at D6
launcherServo.attach(3); // servo control pin at D6
// Start serial at 9600 baud
Serial.begin(9600);
//set servo default positions
launcherServo.write(0);
feederServo.write(0);
//set neopixel
strip.begin();
strip.setBrightness(30); //adjust brightness here
strip.show(); // Initialize all pixels to 'off'
}
////////////////////////////////////////
// Functions //
////////////////////////////////////////
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
//pressure is red
void redNeo(){
colorWipe(strip.Color(255, 0, 0), 50); // Red
strip.show();
}
//pressure is green
void greenNeo(){
colorWipe(strip.Color(0, 255, 0), 50); // Green
strip.show();
}
//pressure is blue
void blueNeo(){
colorWipe(strip.Color(0, 0, 255), 50); // Blue
strip.show();
}
void sonarValue(){
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer
to indicate "out of range" */
Serial.print("Distance: ");
Serial.println("-1");
}
else {
/* Send the distance to the computer using Serial protocol, and
indicate successful reading. */
Serial.print("Dist: ");
Serial.println(distance);
}
}
void pressureValue(){
// Read the input pressure on analog pin 0:
pressValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = pressValue * (5.0 / 1023.0);
Serial.print("Press: ");
Serial.println(pressValue);
}
void calculateValue(){
countValue ++;
Serial.print("Count: ");
Serial.println(countValue);
}
void updateDisplay()
{
// Demonstrate font 2. 10x16. Only numbers and '.' are defined.
// This font looks like 7-segment displays.
// Lets use this big-ish font to display readings from the
// analog pins.
for (int i=0; i<25; i++)
{
oled.clear(PAGE); // Clear the display
oled.setCursor(0, 0); // Set cursor to top-left
oled.setFontType(0); // Smallest font
oled.print("Press:"); // Print "A0"
oled.setFontType(0); // 7-segment font
oled.print(pressValue); // Print a0 reading
oled.setCursor(0, 10); // Set cursor to top-middle-left
oled.setFontType(0); // Repeat
oled.print("Dist:");
oled.setFontType(0);
oled.print(distance);
oled.setCursor(0, 20);
oled.setFontType(0);
oled.print("Count:");
oled.setFontType(0);
oled.print(countValue);
oled.setCursor(0, 30);
oled.setFontType(0);
oled.print("Feed:");
oled.setFontType(0);
oled.print(feedValue);
oled.display();
//delay(100);
}
}
void loop() {
// Read Sonar Value
sonarValue();
// Read Pressure Value
pressureValue();
//clear status LEDS
colorWipe(strip.Color(0, 0, 0), 50); // turn off Neo strip
//digitalWrite(PressureStatusLEDPin, LOW);
digitalWrite(MotorStatusLEDPin, LOW);
//digitalWrite(ObjectStatusLEDPin, LOW);
// check for ball
if (pressValue > 1020) {
greenNeo();
//digitalWrite(PressureStatusLEDPin, HIGH); //red
//ball is in device and ready to launch
//check for objects
if (distance < 10) {
redNeo();
//digitalWrite(ObjectStatusLEDPin, HIGH); //green
}
else {
//no objects and clear to launch
//digitalWrite(ObjectStatusLEDPin, LOW);
//turn on motors relay
blueNeo();
digitalWrite(MotorStatusLEDPin, HIGH); //drive motor relay
delay (2000);
//release servo
launcherServo.write(100);;
delay(1000);
launcherServo.write(0);
delay(1000);
//incriment counter
countValue ++;
//turn off motor
digitalWrite(MotorStatusLEDPin, LOW); //drive motor relay
colorWipe(strip.Color(0, 0, 0), 50); // turn off Neo strip
}
}
else {
colorWipe(strip.Color(0, 0, 0), 50); // turn off Neo strip
//digitalWrite(PressureStatusLEDPin, LOW);
}
// number of times ball must be laucnehd before feeder is activated
ratio = countValue / 2;
//if ratio is greater than previous feed count then activate feeder
if (feedValue < ratio) {
feederServo.write(50);
delay(1000);
feederServo.write(0);
}
Serial.print("Ratio: ");
Serial.println(ratio);
// set new feed value to last calculated ratio
feedValue = ratio;
// Update Display Text
updateDisplay();
}
// Center and print a small title
// This function is quick and dirty. Only works for titles one
// line long.
void printTitle(String title, int font)
{
int middleX = oled.getLCDWidth() / 2;
int middleY = oled.getLCDHeight() / 2;
oled.clear(PAGE);
oled.setFontType(font);
// Try to set the cursor in the middle of the screen
oled.setCursor(middleX - (oled.getFontWidth() * (title.length()/2)),
middleY - (oled.getFontWidth() / 2));
// Print the title:
oled.print(title);
oled.display();
delay(1500);
oled.clear(PAGE);
}