movable_sd_speaker: SDspeaker_Esplora.ino

File SDspeaker_Esplora.ino, 6.7 KB (added by soumura, 7 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 //x_joy += x_trim;
111 //y_joy += y_trim;
112 x_stick = (x_stick * 3 + x_joy)/4;
113 y_stick = (y_stick * 3 + y_joy)/4;
114
115 if(x_stick > 85 && x_stick < 95) x_stick = 90;
116 if(y_stick > 85 && y_stick < 95) y_stick = 90;
117
118 x_temp = x_temp + (x_stick - 90) / 50.0;
119 y_temp = y_temp + (y_stick - 90) / 50.0;
120
121 if((x_temp + 90) >= 180) x_temp = 90.0;
122 else if((x_temp + 90) <= 0) x_temp = -90.0;
123 if((y_temp + 90) >= 180) y_temp = 90.0;
124 else if((y_temp + 90) <= 0) y_temp = -90.0;
125
126 x_stick2 = x_temp + 90;
127 y_stick2 = y_temp + 90;
128
129 //Serial.print(x_stick2);
130 //Serial.print(",");
131 //Serial.println(y_stick2);
132 //delay(20);
133
134
135
136 mySerial.print("x");
137 Serial.print("x");
138 if(x_stick2 < 10){
139 mySerial.print("00");
140 mySerial.print(x_stick2);
141 Serial.print("00");
142 Serial.print(x_stick2);
143 }
144 else if(x_stick2 < 100){
145 mySerial.print("0");
146 mySerial.print(x_stick2);
147 Serial.print("0");
148 Serial.print(x_stick2);
149 }
150 else{
151 mySerial.print(x_stick2);
152 Serial.print(x_stick2);
153 }
154 mySerial.print("y");
155 Serial.print("y");
156 if(y_stick2 < 10){
157 mySerial.print("00");
158 mySerial.println(y_stick2);
159 Serial.print("00");
160 Serial.println(y_stick2);
161 }
162 else if(y_stick2 < 100){
163 mySerial.print("0");
164 mySerial.println(y_stick2);
165 Serial.print("0");
166 Serial.println(y_stick2);
167 }
168 else{
169 mySerial.println(y_stick2);
170 Serial.println(y_stick2);
171 }
172 delay(20);
173
174 }
175
176 else{
177
178
179 if(x_demo >= 150) add = -1;
180 else if(x_demo <= 50) add = 1;
181
182 x_demo += add;
183 delay(demo_speed);
184 //Serial.print(x_demo);
185 //Serial.print(",");
186 if(uButton == LOW){
187 while(1){
188 uButton = Esplora.readButton(SWITCH_UP);
189 if(uButton == HIGH) break;
190 }
191 y_demo += 5;
192 if(y_demo >= 180) y_demo = 180;
193 }
194 if(dButton == LOW){
195 while(1){
196 dButton = Esplora.readButton(SWITCH_DOWN);
197 if(dButton == HIGH) break;
198 }
199 y_demo -= 5;
200 if(y_demo <= 0) y_demo = 0;
201 }
202 if(rButton == LOW){
203 while(1){
204 rButton = Esplora.readButton(SWITCH_RIGHT);
205 if(rButton == HIGH) break;
206 }
207 demo_speed += 10;
208 if(demo_speed >= 200) demo_speed = 200;
209
210 }
211 if(lButton == LOW){
212 while(1){
213 lButton = Esplora.readButton(SWITCH_LEFT);
214 if(lButton == HIGH) break;
215 }
216 demo_speed -= 10;
217 if(demo_speed <= 20) demo_speed = 20;
218 }
219 //Serial.println(y_demo);
220 mySerial.print("x");
221 Serial.print("x");
222 if(x_demo < 10){
223 mySerial.print("00");
224 mySerial.print(x_demo);
225 Serial.print("00");
226 Serial.print(x_demo);
227
228 }
229 else if(x_demo < 100){
230 mySerial.print("0");
231 mySerial.print(x_demo);
232 Serial.print("0");
233 Serial.print(x_demo);
234 }
235 else{
236 mySerial.print(x_demo);
237 Serial.print(x_demo);
238 }
239 mySerial.print("y");
240 Serial.print("y");
241 if(y_demo < 10){
242 mySerial.print("00");
243 mySerial.println(y_demo);
244 Serial.print("00");
245 Serial.println(y_demo);
246 }
247 else if(y_demo < 100){
248 mySerial.print("0");
249 mySerial.println(y_demo);
250 Serial.print("0");
251 Serial.println(y_demo);
252 }
253 else{
254 mySerial.println(y_demo);
255 Serial.println(y_demo);
256 }
257 delay(20);
258
259
260
261 }
262
263}
264
265