arduino‎ > ‎

lesson3laser

Laser Shooter Tutorial

In this tutorial you'll create the firing circuit for your laser shooting game! Below you'll find a diagram and link to the code.


WARNING Lasers can cause permanent damage to your eyes. DO NOT look directly into the laser when on or point it at another person's face.


Set up

  1. Open your Arduino software on your laptop

  2. Use a USB wire to connect your Arduino board to your computer

  3. Select the Arduino under Tools > Port


Schematic

When you setup the Arduino it's very important that all the wires go into the same spots as shown below. Also the laser is like the LED, it only works in one direction so if the laser isn't working try flipping it around.

In the schematic you'll see a lightbulb instead of a laser. Instead just plug in your laser using the wires on the left and right of the laser's circuit board (the outside wires).


Code

For this tutorial you already know everything you need to know to make a laser shooter! In fact you can use the buttons tutorial's code! But try writing the program yourself. Otherwise you can find the code below to program your arduino! Simply copy this file into your Arduino editor and upload it!





//                  LASER SHOOTER TUTORIAL                 //

// ******************************************************* //

//                                                         //

// Before you run this program make sure you have a laser  //

// connected to pin 7 of your Arduino and a button         //

// connected to pin 4.                                     //

//                                                         //

// ******************************************************* //

// LASER = laser (high density particle beam)              //

// BUTTON = 4-pin push button                              //

// ******************************************************* //

//                                                         //

//                                      +-----+ //

//         +----[PWR]-------------------| USB |--+         //

//         |                  +-----+ | //

//         | GND/RST2  [ ][ ] |        //

//         | MOSI2/SCK2  [ ][ ] A5/SCL[ ] | .       //

//         | 5V/MISO2 [ ][ ]  A4/SDA[ ] | . //

//         |                   AREF[ ] | //

//         |                    GND[ ] | //

//       . | [ ]N/C                    SCK/13[ ] | . //

//       . | [ ]v.ref                 MISO/12[ ] | . //

//       . | [ ]RST                   MOSI/11[ ]~| . //

//       . | [ ]3V3    +---+      10[ ]~| . //

//  BUTTON | [@]5v     | A |  9[ ]~| . //

//       . | [@]GND   -| R |-         8[ ] | . //

//       . | [ ]GND   -| D |-              | //

//       . | [ ]Vin   -| U |-         7[@] | LASER //

//         | -| I |-               6[ ]~| . //

//       . | [ ]A0    -| N |-         5[ ]~| . //

//       . | [ ]A1    -| O |-         4[@] | BUTTON //

//       . | [ ]A2     +---+  INT1/3[ ]~| .    //

//       . | [ ]A3                     INT0/2[ ] | . //

//       . | [ ]A4/SDA  RST SCK MISO  TX>1[ ] | . //

//       . | [ ]A5/SCL  [ ] [ ] [ ]  RX<0[ ] | . //

//         |  [ ] [ ] [ ]            | //

//         | UNO_R3  GND MOSI 5V ____________/         //

//          \_______________________/                      //

//                                                         //

// ******************************************************* //







void setup() {

// **************************************************** //

//                    SETUP FUNCTION            //

// **************************************************** //

// Put your setup code here, this code will only be run //

// once and is used to tell the Arduino where all the   //

// wires are connected.                                 //

// **************************************************** //



// In this line we tell the Arduino the pin 7 (our laser)

// is an output. We'll use this to toggle the laser.

pinMode(7, OUTPUT);


// In this line we tell the Arduino the pin 4 (our button)

// is an input. We'll use this to tell when the button is

// pressed.

pinMode(4, INPUT);


// This line is important. This line tells the Arduino

// how to listen to the new input. Without this line

// your program will not work.

digitalWrite(4, HIGH);

}



void loop() {

// **************************************************** //

//                     LOOP FUNCTION          //

// **************************************************** //

// Any code here will be run over and over again        //

// instantly unless we tell the program to delay itself //

// here we turn on the led delay turn it off and then   //

// delay again. Try it and see what happens!            //

// **************************************************** //



// Decides if the button is being pressed or not.

if (digitalRead(4) == LOW){

  // Turn on the laser

  digitalWrite(7, HIGH);

}else{

  // Turn off the laser

  digitalWrite(7, LOW);

}

}