Components and supplies
Arduino UNO
Lego Star Wars Shootable Cannon
Jumper wires (generic)
SG90 Micro-servo motor
Breadboard (generic)
Tools and machines
Tape, Electrical
Apps and platforms
Arduino IDE
Project description
Code
Lego Gun Shooter Code
c_cpp
Paste this into Arduino IDE
1#include <Servo.h> //Including the library for the Servos 2 3Servo firstCannon; //Defining the First Cannon as a Servo 4Servo secondCannon; //Defining the Second Cannon as a Servo 5Servo thirdCannon; //Defining the Third Cannon as a Servo 6Servo fourthCannon; //Defining the Fourth Cannon as a Servo 7 8void setup() { //Setup code, or code that is only run once 9 10 Serial.begin(9600); //Start the Serial communication 11 12 Serial.println("Defend the Clones! Set up the defenses!") //Print something in the Serial Monitor 13 14 firstCannon.attach(7); //Attach Digital Pin 7 to First Cannon 15 secondCannon.attach(8); //Attach Digital Pin 8 to Second Cannon 16 thirdCannon.attach(6); //Attach Digital Pin 6 to Third Cannon 17 fourthCannon.attach(9); //Attach Digital Pin 9 to Fourth Cannon 18 19} 20 21void loop() { //Loop code, or code that is run an infinite amount of times 22 23 Serial.println("Beginning firing sequence") //Print something in the Serial Monitor 24 25 firstCannon.write(180); //Swerve the first servo 180 degrees, setting off the first cannon 26 delay(100); //Wait for 1/10 of a second 27 firstCannon.write(0); //Swerve the Servo back to original position in preparation for reload 28 delay(1000); //Wait for 1 second 29 30 secondCannon.write(180); //Swerve the second servo 180 degrees, setting off the second cannon 31 delay(100); //Wait for 1/10 of a second 32 secondCannon.write(0); //Swerve the Servo back to original position in preparation for reload 33 delay(1000); //Wait for 1 second 34 35 thirdCannon.write(180); //Swerve the third servo 180 degrees, setting off the third cannon 36 delay(100); //Wait for 1/10 of a second 37 thirdCannon.write(0); //Swerve the Servo back to original position in preparation for reload 38 delay(1000); //Wait for 1 second 39 40 fourthCannon.write(180); //Swerve the first fourth 180 degrees, setting off the fourth cannon 41 delay(100); //Wait for 1/10 of a second 42 fourthCannon.write(0); //Swerve the Servo back to original position in preparation for reload 43 delay(1000); //Wait for 1 second 44 45 Serial.println("All cannons fired"); 46 Serial.println("Prepare for reload"); 47 48 delay(10000); 49 50 Serial.println("Reloading Complete. Preparing to fire again") 51 52}
Lego Gun Shooter Code
c_cpp
Paste this into Arduino IDE
1#include <Servo.h> //Including the library for the Servos 2 3Servo 4 firstCannon; //Defining the First Cannon as a Servo 5Servo secondCannon; //Defining 6 the Second Cannon as a Servo 7Servo thirdCannon; //Defining the Third Cannon 8 as a Servo 9Servo fourthCannon; //Defining the Fourth Cannon as a Servo 10 11void 12 setup() { //Setup code, or code that is only run once 13 14 Serial.begin(9600); 15 //Start the Serial communication 16 17 Serial.println("Defend the Clones! 18 Set up the defenses!") //Print something in the Serial Monitor 19 20 firstCannon.attach(7); 21 //Attach Digital Pin 7 to First Cannon 22 secondCannon.attach(8); //Attach 23 Digital Pin 8 to Second Cannon 24 thirdCannon.attach(6); //Attach Digital Pin 25 6 to Third Cannon 26 fourthCannon.attach(9); //Attach Digital Pin 9 to Fourth 27 Cannon 28 29} 30 31void loop() { //Loop code, or code that is run an infinite 32 amount of times 33 34 Serial.println("Beginning firing sequence") //Print 35 something in the Serial Monitor 36 37 firstCannon.write(180); //Swerve the first 38 servo 180 degrees, setting off the first cannon 39 delay(100); //Wait for 1/10 40 of a second 41 firstCannon.write(0); //Swerve the Servo back to original position 42 in preparation for reload 43 delay(1000); //Wait for 1 second 44 45 secondCannon.write(180); 46 //Swerve the second servo 180 degrees, setting off the second cannon 47 delay(100); 48 //Wait for 1/10 of a second 49 secondCannon.write(0); //Swerve the Servo back 50 to original position in preparation for reload 51 delay(1000); //Wait for 1 second 52 53 54 thirdCannon.write(180); //Swerve the third servo 180 degrees, setting off the 55 third cannon 56 delay(100); //Wait for 1/10 of a second 57 thirdCannon.write(0); 58 //Swerve the Servo back to original position in preparation for reload 59 delay(1000); 60 //Wait for 1 second 61 62 fourthCannon.write(180); //Swerve the first fourth 63 180 degrees, setting off the fourth cannon 64 delay(100); //Wait for 1/10 of 65 a second 66 fourthCannon.write(0); //Swerve the Servo back to original position 67 in preparation for reload 68 delay(1000); //Wait for 1 second 69 70 Serial.println("All 71 cannons fired"); 72 Serial.println("Prepare for reload"); 73 74 delay(10000); 75 76 77 Serial.println("Reloading Complete. Preparing to fire again") 78 79}
Downloadable files
Lego Shootable Cannon Servo Diagram
Lego Shootable Cannon Servo Diagram
Comments
Only logged in users can leave comments
ShreyanR
0 Followers
•0 Projects
Table of contents
Intro
7
0