/* Fire laser 7 when you press button 11.
* Fire laser 8 when you press button 12.
* Use the speaker to add a tone when the laser fires.
*/
int i;
int hz;
void setup() {
pinMode(2,OUTPUT); //speaker
pinMode(7,OUTPUT); //blaster LED 7
pinMode(8,OUTPUT); //blaster LED 8
pinMode(11,INPUT_PULLUP); //button 11
pinMode(12,INPUT_PULLUP); //button 12
}
void loop() {
if(digitalRead(11)==LOW){ //if button 11 is pressed
digitalWrite(7,HIGH); //turn on LED 7
//A fast falling tone makes a better laser sound (like the laser 'bullet' is getting further away) than a single tone does.
// Whoop up
for(int hz = 440; hz < 1000; hz++){
tone(2, hz, 50);
delay(5);
}
noTone(2);
// Whoop down
for(int hz = 1000; hz > 440; hz--){
tone(2, hz, 50);
delay(5);
}
noTone(2);
}
else if(digitalRead(12)==LOW){ //if button 12 is pressed
digitalWrite(8,HIGH); // turn on LED 8
// This tone falls from 1000 to 700, playing a different tone every 1 millsecond
for (i = 912; i > 200; i--) {
digitalWrite(2, HIGH);
delayMicroseconds(i);
digitalWrite(2, LOW);
delayMicroseconds(i);
}
for (i = 200; i < 912; i++) {
digitalWrite(2, HIGH);
delayMicroseconds(i);
digitalWrite(2, LOW);
delayMicroseconds(i);
}
}
else{ //Otherwise if no buttons are pressed
digitalWrite(7,LOW); //turn off the LEDs
digitalWrite(8,LOW);
noTone(2); //stop the tone on the speaker
}
}