Hack the Code Rocket!
No in-depth explanations to be found here! This is a fun program that uses all of the code concepts you saw in the projects, plus more! If you’re up to the challenge, dive into this program and try to decode what it does and how it works, using just the code itself and the comments throughout. Enjoy!
/* A fun and complex program that makes the Code Rocket fun to play with. No in-depth explanations here; this
* program is designed for you to take apart and 'hack' into something new!
*/
int pressCount = 0; // an integer variable counting up each time a button is pressed
void setup(){
pinMode(3,OUTPUT); //set each LED as an OUTPUT
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,INPUT_PULLUP); //set the buttons as INPUT_PULLUP
pinMode(12,INPUT_PULLUP);
pinMode(2,OUTPUT); //set the speaker as an OUTPUT
}
void loop(){
while(digitalRead(11)==LOW && digitalRead(12)==LOW){ //if both buttons 11 and 12 are pressed at the same time
digitalWrite(7,LOW); //turn off the blaster LEDs if they are on.
digitalWrite(8,LOW);
for(int i=0;i<255;i++){ //fade up the booster LEDs and fade up a tune
analogWrite(9,i);
analogWrite(10,i);
tone(2,i*5);
delay(7);
}
//after the LEDs and tune 'fade up', play a rumbling/static tone as long as the 11 button is still held
while(digitalRead(11)==LOW){
tone(2,150); //tones close to each other in hertz value played really quickly will sound like static
delay(1);
tone(2,50);
delay(1);
}
digitalWrite(9,LOW); //turn off the booster LEDs when button 11 is released
digitalWrite(10,LOW);
}
if(digitalRead(11)==LOW){ //if button 11 is pressed
digitalWrite(7,HIGH); //turn on LED 7
pressCount++; //increase pressCount by one
//A fast falling tone makes a better laser sound (like the laser 'bullet' is getting further away) than a single tone does.
for(int i=800;i>500;i--){ //Starting at 800 and decreasing to 500
tone(2,i); //play the tone with pitch equal to variable 'i'
delay(1); //delay 1 millisecond before changing the pitch and playing again
}
}
else if(digitalRead(12)==LOW){ //if button 12 is pressed
digitalWrite(8,HIGH); // turn on LED 8
pressCount++; //increase pressCount by one
// This tone falls from 1000 to 700, playing a different tone every 1 millsecond
for(int i=1000;i>700;i--){ // Starting at 1000 and decreasing to 700
tone(2,i); //play the tone with pitch equal to variable 'i'
delay(1); //delay 1 millsecond
}
}
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
}
if(pressCount==50){ //if pressCount reaches 50, turn off the blaster LED and play 'ascend to charge' and play a light pattern
digitalWrite(8,LOW);//turn off the LED that may be on
digitalWrite(7,LOW);//turn off the LED that may be on
tone(2,523); //C
delay(100); //Play the note for 150 milliseconds
tone(2,659); //E
delay(100);
tone(2,784); //G
delay(100);
tone(2,880); //A
delay(200); //double delay on this note
tone(2,659); //E
delay(100);
tone(2,784); //G
delay(400); //quadruple delay on the last note
noTone(2);
pressCount=0; //reset pressCount to 0
for(int i=3;i<=6;i++){ //Turn on each LED in the circle in order and play a corresponding tune
digitalWrite(i,HIGH);
tone(2,i*100);
delay(75);
digitalWrite(i,LOW);
}// end of the for loop
}//end of the 'pressCount=50' loop
}//end of the void loop
// (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
Heads up! You need our Chrome App to run this code.
Success!
Compile errors:
Serial port monitor:
Input: