-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjepImperialMelodyDischarger.ino
256 lines (204 loc) · 7.67 KB
/
jepImperialMelodyDischarger.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
//Star Wars Legion
//Storm Tropper Helmet
//by John Edgar Park jpixl.net
// this code controls the Storm Trooper helmet/music box art project I created named Imperial Melody Discharger.
//some code samples thanks to:
// Sweep - BARRAGAN <http://barraganstudio.com>
//and
// http://adafruit.com/forums/viewtopic.php?f=25&p=170965
#include <Servo.h>
const int ledGreenPin = 11; // number of the green LED pin
const int ledRedPin = 12; // number of the red LED pin
const int ledPowerPin = 13; // number of the chin LED pin
int speakerPin = 6; // number of the piezo buzzer/speaker pin
int powerOffPin = 3; // number of the power switch power off pin
Servo myservoR; // create servo object to control right one on storm trooper face
Servo myservoL; // create servo object to control left on on storm trooper face
int sRclosed = 148; // variable to store closed position value of servo on right
int sRopened = 120; // variable to store opened position value of servo on right
int sLclosed = 18; // variable to store closed position value of servo on left
int sLopened = 50; // variable to store closed position value of servo on left
int posR = sRclosed; // variable to store the servo position initially, the closed position for Storm Trooper
int posL = sLclosed; // variable to store the servo position initially, the closed position for Storm Trooper
const int openDelay=25; // speed of the servos when opening
const int closeDelay=15; // speed of the servos when closing
//tone frequencies http://home.(m-eye-t).bme.hu/~bako/tonecalc/tonecalc.htm
#define c 261
#define d 294
#define e 329
#define f 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880
///////////////////////////// end includes
void setup() {
pinMode(ledGreenPin, OUTPUT);
pinMode(ledRedPin, OUTPUT);
pinMode(ledPowerPin, OUTPUT);
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledRedPin, HIGH);
pinMode(speakerPin, OUTPUT);
pinMode(powerOffPin, OUTPUT);
myservoR.attach(9); // attaches the servo on pin 9 to the servo object
myservoL.attach(10); // attaches the servo on pin 10 to the servo object
myservoR.write(sRclosed); //closes the servo
myservoL.write(sLclosed); //closes the servo
}
///////////////////////////// end void setup
void loop() {
//turn on power chin LED and green LED
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledRedPin, LOW);
digitalWrite(ledPowerPin, HIGH);
delay(1000);
//OPEN UP Right
for(posR = sRclosed; posR >= sRopened; posR -= 1) // goes from closed to opened
{ // in steps of 1 degree
myservoR.write(posR); // tell servo to go to position in variable 'pos'
delay(openDelay); // waits 15ms for the servo to reach the position
}
delay(1500); //wait a moment
digitalWrite(ledGreenPin, LOW);
digitalWrite(ledRedPin, HIGH);
//OPEN UP Left
for(posL = sLclosed; posL <= sLopened; posL += 1) // goes from closed to open
{ // in steps of 1 degree
myservoL.write(posL); // tell servo to go to position in variable 'pos'
delay(openDelay); // waits 15ms for the servo to reach the position
}
delay(1500); //wait a moment
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledRedPin, HIGH);
//PLAY MUSIC
march();
//CLOSE UP Right
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledRedPin, LOW);
for(posR = sRopened; posR <= sRclosed; posR+=1) // goes from opened to closed
{
myservoR.write(posR); // tell servo to go to position in variable 'pos'
delay(closeDelay); // waits 15ms for the servo to reach the position
}
delay(1500); //wait a moment
digitalWrite(ledGreenPin, LOW);
digitalWrite(ledRedPin, HIGH);
//Close Up Left
for(posL = sLopened; posL >= sLclosed; posL-=1) // goes from opened to closed
{
myservoL.write(posL); // tell servo to go to position in variable 'pos'
delay(closeDelay); // waits 15ms for the servo to reach the position
}
digitalWrite(ledGreenPin, LOW);
digitalWrite(ledRedPin, LOW);
delay(1500);
//turn off the Arduino
digitalWrite(powerOffPin, HIGH);
}
//////////////////////////////END void loop
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
//delay to separate notes
delay(20);
}
/////////////////////////////END void beep
void march()
{
//Sheet music http://www.musicnotes.com/sheetmusic/mtd.asp?ppn=MN0016254
//time in ms
//500 ms for a quart note
beep(speakerPin, a, 500);
beep(speakerPin, a, 500);
beep(speakerPin, a, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 1000);
beep(speakerPin, eH, 500);
beep(speakerPin, eH, 500);
beep(speakerPin, eH, 500);
beep(speakerPin, fH, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 350);
beep(speakerPin, cH, 150);
beep(speakerPin, a, 1000);
/* uncomment if you would like the whole song
beep(speakerPin, aH, 500);
beep(speakerPin, a, 350);
beep(speakerPin, a, 150);
beep(speakerPin, aH, 500);
beep(speakerPin, gSH, 250);
beep(speakerPin, gH, 250);
beep(speakerPin, fSH, 125);
beep(speakerPin, fH, 125);
beep(speakerPin, fSH, 250);
delay(250);
beep(speakerPin, aS, 250);
beep(speakerPin, dSH, 500);
beep(speakerPin, dH, 250);
beep(speakerPin, cSH, 250);
beep(speakerPin, cH, 125);
beep(speakerPin, b, 125);
beep(speakerPin, cH, 250);
delay(250);
beep(speakerPin, f, 125);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 375);
beep(speakerPin, a, 125);
beep(speakerPin, cH, 500);
beep(speakerPin, a, 375);
beep(speakerPin, cH, 125);
beep(speakerPin, eH, 1000);
beep(speakerPin, aH, 500);
beep(speakerPin, a, 350);
beep(speakerPin, a, 150);
beep(speakerPin, aH, 500);
beep(speakerPin, gSH, 250);
beep(speakerPin, gH, 250);
beep(speakerPin, fSH, 125);
beep(speakerPin, fH, 125);
beep(speakerPin, fSH, 250);
delay(250);
beep(speakerPin, aS, 250);
beep(speakerPin, dSH, 500);
beep(speakerPin, dH, 250);
beep(speakerPin, cSH, 250);
beep(speakerPin, cH, 125);
beep(speakerPin, b, 125);
beep(speakerPin, cH, 250);
delay(250);
beep(speakerPin, f, 250);
beep(speakerPin, gS, 500);
beep(speakerPin, f, 375);
beep(speakerPin, cH, 125);
beep(speakerPin, a, 500);
beep(speakerPin, f, 375);
beep(speakerPin, c, 125);
beep(speakerPin, a, 1000);
*/
}
/////////////////////////////END void march