-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaserKit.ino
More file actions
148 lines (121 loc) · 3.12 KB
/
laserKit.ino
File metadata and controls
148 lines (121 loc) · 3.12 KB
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
#include <time.h>
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
Servo xservo;
Servo yservo;
char auth[] = "X";
char ssid[] = "X";
char pass[] = "X";
// Laser on/off switch
BLYNK_WRITE(V0){
// Laser off
if (param.asInt() == 0){
digitalWrite(4, HIGH);
}
// Laser on
else if (param.asInt() == 1){
digitalWrite(4, LOW);
}
}
// Y Servo
BLYNK_WRITE(V1){
digitalWrite(0, HIGH);
yservo.write(param.asInt());
delay(800);
digitalWrite(0, LOW);
}
// X Servo
BLYNK_WRITE(V2){
digitalWrite(2, HIGH);
xservo.write(param.asInt());
delay(800);
digitalWrite(2, LOW);
}
// Pattern A
BLYNK_WRITE(V3){
srand ( time(NULL) );
int xpos, rand_delay, elapsed_sec;
// X servo on, laser on
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
// Y servo can be within range 127-175
digitalWrite(0, HIGH);
yservo.write(127);
delay(800);
digitalWrite(0, LOW);
// Only run for 2 minute. (Function always runs twice for some reason)
time_t start = time(0);
elapsed_sec = difftime( time(0), start);
while( elapsed_sec <= 60 ) {
// X Servo can be within range 100-142
xpos = rand() % 43 + 100;
xservo.write(xpos);
// Change Y Servo every so often to keep things fresh
if( elapsed_sec % 3 == 0){
yservo.write(rand() % 5 + 120);
}
// Delay between 500ms and 4000ms
rand_delay = rand() % 4000 + 500;
delay(rand_delay);
// Update elapsed time
elapsed_sec = difftime( time(0), start);
}
// xservo off, laser off
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
}
// Pattern B
BLYNK_WRITE(V4){
// X Servo can be within range 100-142
// Y Servo can be within range 127-175 (restricted to 140)
srand ( time(NULL) );
int elapsed_sec;
int y_op = -1, x_op = 1;
int xpos = rand() % 43 + 100;
int ypos = rand() % 10 + 127;
// X and Y servo on, laser on
digitalWrite(0, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
// Only run for 2 minute. (Function always runs twice for some reason)
time_t start = time(0);
elapsed_sec = difftime( time(0), start);
while( elapsed_sec <= 60 ) {
// Does the movement, goes all the way to one direction and reverses
if( xpos <= 100 ){x_op = 1;} else if( xpos >= 142 ){x_op = -1;}
if( ypos <= 112 ){y_op = 1;} else if( ypos >= 135 ){y_op = -1;}
xpos = xpos + x_op;
ypos = ypos + y_op;
xservo.write(xpos);
yservo.write(ypos);
delay(70);
// Update elapsed time
elapsed_sec = difftime( time(0), start);
}
// X and Y servo off, laser off
delay(1000);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
}
void setup(){
// Debug output
Serial.begin(115200);
Serial.setDebugOutput(true);
// Start Blynk
Blynk.begin(auth, ssid, pass);
// Initialize and silence pins
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
yservo.attach(0);
xservo.attach(2);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
}
void loop() {
Blynk.run();
}