movable_sd_speaker: SDspeaker_Esplora.2.ino

File SDspeaker_Esplora.2.ino, 6.4 KB (added by soumura, 6 years ago)
Line 
1#include <Esplora.h>
2#include <SoftwareSerial.h>
3
4SoftwareSerial mySerial(11,3);
5
6unsigned long x_angle = 90;
7unsigned long y_angle = 90;
8int x_stick = 90;
9int y_stick = 90;
10int x_trim = 20;
11int y_trim = 0;
12double x_temp = 0.0;
13double y_temp = 0.0;
14int x_stick2 = 90;
15int y_stick2 = 90;
16int x_demo = 90;
17int y_demo = 90;
18int add = 1;
19int demo_speed = 100;
20void setup()
21{
22 Serial.begin(9600);
23 mySerial.begin(9600);
24}
25
26void loop()
27{
28 int i = 0;
29
30 int x_axis = Esplora.readAccelerometer(X_AXIS);
31 int y_axis = Esplora.readAccelerometer(Y_AXIS);
32 int value = Esplora.readSlider();
33 int x_joy = Esplora.readJoystickX();
34 int y_joy = Esplora.readJoystickY();
35 int uButton = Esplora.readButton(SWITCH_UP);
36 int dButton = Esplora.readButton(SWITCH_DOWN);
37 int rButton = Esplora.readButton(SWITCH_RIGHT);
38 int lButton = Esplora.readButton(SWITCH_LEFT);
39
40 if(value > 812){
41
42 x_axis = map(x_axis, -180,180,0,180);
43 y_axis = map(y_axis, -180,180,0,180);
44
45 x_angle = (x_angle * 3 + x_axis) / 4;
46 y_angle = (y_angle * 3 + y_axis) / 4;
47
48
49 Serial.print("x");
50 if(x_angle < 10){
51 Serial.print("00");
52 Serial.print(x_angle);
53 }
54 else if(x_angle < 100){
55 Serial.print("0");
56 Serial.print(x_angle);
57 }
58 else{
59 Serial.print(x_angle);
60 }
61 Serial.print("y");
62 if(y_angle < 10){
63 Serial.print("00");
64 Serial.println(y_angle);
65 }
66 else if(y_angle < 100){
67 Serial.print("0");
68 Serial.println(y_angle);
69 }
70 else{
71 Serial.println(y_angle);
72 }
73
74 mySerial.print("x");
75 if(x_angle < 10){
76 mySerial.print("00");
77 mySerial.print(x_angle);
78 }
79 else if(x_angle < 100){
80 mySerial.print("0");
81 mySerial.print(x_angle);
82 }
83 else{
84 mySerial.print(x_angle);
85 }
86 mySerial.print(",");
87
88
89 mySerial.print("y");
90
91 if(y_angle < 10){
92 mySerial.print("00");
93 mySerial.println(y_angle);
94 }
95 else if(y_angle < 100){
96 mySerial.print("0");
97 mySerial.println(y_angle);
98 }
99 else{
100 mySerial.println(y_angle);
101 }
102 mySerial.print(",");
103 delay(20);
104 }
105
106
107 else if(value < 212){
108 x_joy = map(x_joy,-512,511,180,0);
109 y_joy = map(y_joy,-512,511,180,0);
110
111 x_stick = (x_stick * 3 + x_joy)/4;
112 y_stick = (y_stick * 3 + y_joy)/4;
113
114 if(x_stick > 85 && x_stick < 95) x_stick = 90;
115 if(y_stick > 85 && y_stick < 95) y_stick = 90;
116
117 x_temp = x_temp + (x_stick - 90) / 50.0;
118 y_temp = y_temp + (y_stick - 90) / 50.0;
119
120 if((x_temp + 90) >= 180) x_temp = 90.0;
121 else if((x_temp + 90) <= 0) x_temp = -90.0;
122 if((y_temp + 90) >= 180) y_temp = 90.0;
123 else if((y_temp + 90) <= 0) y_temp = -90.0;
124
125 x_stick2 = x_temp + 90;
126 y_stick2 = y_temp + 90;
127
128 mySerial.print("x");
129 Serial.print("x");
130 if(x_stick2 < 10){
131 mySerial.print("00");
132 mySerial.print(x_stick2);
133 Serial.print("00");
134 Serial.print(x_stick2);
135 }
136 else if(x_stick2 < 100){
137 mySerial.print("0");
138 mySerial.print(x_stick2);
139 Serial.print("0");
140 Serial.print(x_stick2);
141 }
142 else{
143 mySerial.print(x_stick2);
144 Serial.print(x_stick2);
145 }
146 mySerial.print("y");
147 Serial.print("y");
148 if(y_stick2 < 10){
149 mySerial.print("00");
150 mySerial.println(y_stick2);
151 Serial.print("00");
152 Serial.println(y_stick2);
153 }
154 else if(y_stick2 < 100){
155 mySerial.print("0");
156 mySerial.println(y_stick2);
157 Serial.print("0");
158 Serial.println(y_stick2);
159 }
160 else{
161 mySerial.println(y_stick2);
162 Serial.println(y_stick2);
163 }
164 delay(20);
165
166 }
167
168 else{
169
170
171 if(x_demo >= 150) add = -1;
172 else if(x_demo <= 50) add = 1;
173
174 x_demo += add;
175 delay(demo_speed);
176
177 if(uButton == LOW){
178 while(1){
179 uButton = Esplora.readButton(SWITCH_UP);
180 if(uButton == HIGH) break;
181 }
182 y_demo += 5;
183 if(y_demo >= 180) y_demo = 180;
184 }
185 if(dButton == LOW){
186 while(1){
187 dButton = Esplora.readButton(SWITCH_DOWN);
188 if(dButton == HIGH) break;
189 }
190 y_demo -= 5;
191 if(y_demo <= 0) y_demo = 0;
192 }
193 if(rButton == LOW){
194 while(1){
195 rButton = Esplora.readButton(SWITCH_RIGHT);
196 if(rButton == HIGH) break;
197 }
198 demo_speed += 10;
199 if(demo_speed >= 200) demo_speed = 200;
200
201 }
202 if(lButton == LOW){
203 while(1){
204 lButton = Esplora.readButton(SWITCH_LEFT);
205 if(lButton == HIGH) break;
206 }
207 demo_speed -= 10;
208 if(demo_speed <= 20) demo_speed = 20;
209 }
210 mySerial.print("x");
211 Serial.print("x");
212 if(x_demo < 10){
213 mySerial.print("00");
214 mySerial.print(x_demo);
215 Serial.print("00");
216 Serial.print(x_demo);
217
218 }
219 else if(x_demo < 100){
220 mySerial.print("0");
221 mySerial.print(x_demo);
222 Serial.print("0");
223 Serial.print(x_demo);
224 }
225 else{
226 mySerial.print(x_demo);
227 Serial.print(x_demo);
228 }
229 mySerial.print("y");
230 Serial.print("y");
231 if(y_demo < 10){
232 mySerial.print("00");
233 mySerial.println(y_demo);
234 Serial.print("00");
235 Serial.println(y_demo);
236 }
237 else if(y_demo < 100){
238 mySerial.print("0");
239 mySerial.println(y_demo);
240 Serial.print("0");
241 Serial.println(y_demo);
242 }
243 else{
244 mySerial.println(y_demo);
245 Serial.println(y_demo);
246 }
247 delay(20);
248 }
249}
250
251