-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser.cpp
208 lines (187 loc) · 5.67 KB
/
laser.cpp
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
#include "VarSpeedServo.h"
#include "laser.h"
#include "config.h"
#include <Arduino.h>
// Global variables and defines
// object initialization
VarSpeedServo servo9g1;
VarSpeedServo servo9g2;
int S1_value1, S1_value2;
int S2_value1, S2_value2;
bool autoplayState = 1;
bool laserState = 1;
const int servoSpeed = 10;
const int seqIntervalDelta = 50;
const int manualyServoMinStep = 5;
const int manualyServoMaxStep = 20;
const int minimalRangeSize = 10;
// Change these parameters to define the rectangular play area
int servo1Min = 75;
int servo1Max = 115;
int servo2Min = 15;
int servo2Max = 55;
int servo1pos = (servo1Min + servo1Max) / 2;
int servo2pos = (servo2Min + servo2Max) / 2;
int delayVal = 200;
bool check_autoplay()
{
return autoplayState;
}
void pin_init()
{
servo9g1.attach(SERVO9G1_PIN_SIG); // 1. attach the servo to correct pin to control it.
servo9g2.attach(SERVO9G2_PIN_SIG); // 1. attach the servo to correct pin to control it.
pinMode(LASER_PIN_S, OUTPUT);
// start with laser off
digitalWrite(LASER_PIN_S, laserState);
servo9g2.write(servo2pos , servoSpeed);
servo9g1.write(servo1pos , servoSpeed);
}
bool checkCommand(String cmd)
{
bool res = false;
if(cmd.indexOf("/cmd_toggle") > 0) { toggleLaser(); res = true; }
if(cmd.indexOf("/cmd_speed_up") > 0) { speedUp(); res = true; }
if(cmd.indexOf("/cmd_speed_down") > 0) { speedDown(); res = true; }
if(cmd.indexOf("/cmd_autoplay") > 0) { autoplay(); res = true; }
//if(cmd.indexOf("/cmd_autoplay_random_pos") > 0) autoplay_randomPos();
if(cmd.indexOf("/cmd_extend_area") > 0) { extend_area(); res = true; }
if(cmd.indexOf("/cmd_shrink_area") > 0) { shrink_area(); res = true; }
if(!check_autoplay()) if(cmd.indexOf("/cmd_manual_up") > 0) { manual_up(); res = true; }
if(!check_autoplay()) if(cmd.indexOf("/cmd_manual_down") > 0) { manual_down(); res = true; }
if(!check_autoplay()) if(cmd.indexOf("/cmd_manual_left") > 0) { manual_left(); res = true; }
if(!check_autoplay()) if(cmd.indexOf("/cmd_manual_right") > 0) { manual_right(); res = true; }
return res;
}
void toggleLaser()
{
Serial.println("toggleLaser");
laserState = !laserState;
digitalWrite(LASER_PIN_S, laserState);
Serial.print("Laser: ");
Serial.println(laserState);
}
void speedUp()
{
Serial.println("speedUp");
if(delayVal > 0) delayVal -= seqIntervalDelta;
Serial.print("delay: ");
Serial.println(delayVal);
}
void speedDown()
{
Serial.println("speedDown");
if(delayVal < 1000) delayVal += seqIntervalDelta;
Serial.print("delay: ");
Serial.println(delayVal);
}
void autoplay()
{
Serial.println("autoplay");
autoplayState = !autoplayState;
Serial.print("AutoPlay: ");
Serial.println(autoplayState);
if(autoplayState == 0)
{
servo9g1.detach();
servo9g2.detach();
}
else
{
servo9g1.attach(SERVO9G1_PIN_SIG); // 1. attach the servo to correct pin to control it.
servo9g2.attach(SERVO9G2_PIN_SIG); // 1. attach the servo to correct pin to control it.
}
}
// Functionality
void autoplay_randomPos()
{
Serial.println("autoplay_randomPos");
servo9g1.write(random(servo1Min, servo1Max), servoSpeed);
servo9g2.write(random(servo2Min, servo2Max), servoSpeed);
servo9g1.wait();
servo9g2.wait();
delay(random(delayVal, delayVal * 5));
}
void extend_area()
{
servo1Min -= 5;
servo1Max += 5;
servo2Min -= 5;
servo2Max += 5;
servo1Min = constrain(servo1Min, 0, servo1Max);
servo1Max = constrain(servo1Max, servo1Min, 180);
servo2Min = constrain(servo2Min, 0, servo2Max);
servo2Max = constrain(servo2Max, servo2Min, 180);
Serial.print("Area +: ");
char buf[20];
sprintf(buf, "%d,%d,%d,%d", servo1Min, servo1Max, servo2Min, servo2Max);
Serial.println(buf);
}
// tighten the servo margins
void shrink_area()
{
if (servo1Max - servo1Min > minimalRangeSize)
{
servo1Min += 5;
servo1Max -= 5;
}
if (servo2Max - servo2Min > minimalRangeSize)
{
servo2Min += 5;
servo2Max -= 5;
}
Serial.print("Area -: ");
char buf[20];
sprintf(buf, "%d,%d,%d,%d", servo1Min, servo1Max, servo2Min, servo2Max);
Serial.println(buf);
}
void manual_up()
{
servo9g2.attach(SERVO9G2_PIN_SIG);
delay(300);
Serial.println("autoplay_up");
servo2pos += random(manualyServoMinStep, manualyServoMaxStep);
servo2pos = constrain(servo2pos, servo2Min, servo2Max);
servo9g2.write(servo2pos, servoSpeed);
servo9g2.wait();
Serial.println(servo2pos);
delay(300);
servo9g2.detach();
}
void manual_down()
{
servo9g2.attach(SERVO9G2_PIN_SIG);
delay(300);
Serial.println("autoplay_down");
servo2pos -= random(manualyServoMinStep, manualyServoMaxStep);
servo2pos = constrain(servo2pos, servo2Min, servo2Max);
servo9g2.write(servo2pos, servoSpeed);
servo9g2.wait();
Serial.println(servo2pos);
delay(300);
servo9g2.detach();
}
void manual_left()
{
servo9g1.attach(SERVO9G1_PIN_SIG);
delay(300);
Serial.println("autoplay_left");
servo1pos += random(manualyServoMinStep, manualyServoMaxStep);
servo1pos = constrain(servo1pos, servo1Min, servo1Max);
servo9g1.write(servo1pos, servoSpeed);
servo9g1.wait();
delay(300);
servo9g1.detach();
}
void manual_right()
{
servo9g1.attach(SERVO9G1_PIN_SIG);
delay(300);
Serial.println("autoplay_right");
servo1pos -= random(manualyServoMinStep, manualyServoMaxStep);
servo1pos = constrain(servo1pos, servo1Min, servo1Max);
servo9g1.write(servo1pos, servoSpeed);
servo9g1.wait();
delay(300);
servo9g1.detach();
}