Laser Tag S02 – LED Light Strip

Love Jesus, Love Code

Time 30-60mins.

Each tutorial will build on the next as we get to know how to program the different components of the gun. We will start with the OTA demo and build on that. This will allow us to keep our laser tag guns intact.

If you haven’t already, make sure you have completed the previous tutorial.

In this tutorial, we will change the lights on the LED light Strip

What the LED light strip does

The LED light strip requires 3 lines to work, the positive line, the ground line and the data line. The data line lets each light be programmed individually.

Each light has a DI – Digital In, and a DO Digital Out. The lights are in series, and each light in the series is given an addressable number starting at 0, and can be issued Red, Green and Blue values from 0 to 255.

Program building blocks

Programs usually follow the following format:

  1. Variables
  2. Libraries
  3. setup function
  4. void loop
  5. functions and sub-routines.

Variables and Libraries can often be swapped around. Many programs put their libraries first. I put variables first because you are likely to want to change these values quickly.

// Variables and Libraries

// run once before the main loop
void setup(){

}

// runs continuously
void loop(){

}

// functions and subroutines

Comments

Comments help explain what different parts of your program are doing. They don’t have any impact on your program. To create a one line comment, use “//”. Everything after the double slash will be a comment until the next line

// this is a comment

To create a multi line comment, it starts with “/*” and ends with “*/” for examle:

/*
This is a 
Multi line
Comment
*/

Variables

Variables store data and are assigned a label. In C++ you need to declare the type of data is being stored, for example, is it a String, character, integer or float (number). This is extremely important for our laser tag game as we need to store important information such as the playerID, teamID, your health, the damage your gun does to other players, and so on.

For the LED strip, we need a variable for the TeamID. This will be an integer, being a number from 0-4. We also need another variable to store the pin number.

int teamID = 4; // 0-3  0 = red, 1 = blue, 2 = yellow, 3 = green, 4 = unspecified
int ledStripPin = 2;// pin2 or D4 - LED strip

Libraries

Libraries are a collection of pre-written code usually to solve a specific problem, and reduces software development time. Instead of needing to write all the code necessary to interface with particular devices, library are written to make coding easier. For the laser tag gun we will import libraries for each of the sensors, the lights, and for other more complex programming parts.

To get the LED strip working, we need to install the Adafruit NeoPixel Library

  • On the Arduino IDE go to Tools –> Manage Libraries…
  • Search for NeoPixel
  • Make sure Adafruit NeoPixel library is installed
  • Then add the following lines to the libraries section of the program
// NeoPixel libraries and variables
#include <Adafruit_NeoPixel.h>
#define PIN ledStripPin //shares the same pin as the LED hit pin - seems to work ok.
#define NUM_LEDS 80 // change this to the number of lights you will be using
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup()

This subroutine is used to set up various components of your program. We will need to setup the serial debugging and the NeoPixel strip.

Serial debugging

If things do not work out as planned, it is often good to have a way to debug your program. You can print comments to the serial monitor in the Arduino IDE that can help work out how far the program got. When I am modifying programs for my laser tag guns, I usually have an additional setup for prototyping. This allows me to use the serial monitor to debug my programs. Once I am happy with any updates, it can then be upload to the laser tag gun using OTA.

// Serial communication set up to help with debugging.
Serial.begin(74880);
Serial.println("Serial Communication is set up");

NeoPixel strip

The second thing we will set up is the NeoPixel strip, this is done by the command “strip.begin()”. We also want to call the function teamColour(); to change the colour of the strip. This function will be created later later.

// set up team colour NeoPixel
strip.begin();
teamColour(); // subroutine to change colour of strip.
Serial.println("NeoPixel Strip LED is now operational");
My Laser Tag prototype – easy to connect to USB and change circuits
Serial Monitor on the Arduino IDE

void loop()

This were the main program runs, usually you will call different subroutines from this loop. For our guns we might have a sub-routine to check to see if we have been hit, another if we pull the trigger button, and another to keep the Wi-Fi alive.

In this program, we won’t need to put anything in the main loop. We are simply making the LED strip display a certain colour.

Functions and Sub-routines

Functions and Sub-routines are quite similar. The only difference is that a function returns a value. We will mostly be using sub-routines.

We will create a subroutine called teamColour(). This subroutine will check the teamID and then tell the subroutine setAll to colour all the pixels a certain colour. and another subroutine called setAll.

//teamColour sub-routine

void teamColour(){
  strip.show(); // Initialize all pixels to 'off'
  switch (teamID){
    case 0: // red team
      setAll(10,0,0);
      break;
    case 1: // blue team
      setAll(0,0,10);
      break;
    case 2: // yellow team
      setAll(10,10,0);
      break;
    case 3: // green team
      setAll(0,10,0);
      break;
    case 4: // unspecified team
      setAll(10,10,10);
      break;
  }
}

// sets the colour of every pixel to be the same
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    strip.setPixelColor(i, strip.Color(red, green, blue));
  }
  strip.show();
}


void hitFlash() {
  strip.begin();
  setAll(0xFF,0x00,0x00);
  strip.show(); // Initialize all pixels to 'off'
  teamColour(); // change back to team colour
}

void flash() {
  strip.begin();
  setAll(0xFF,0xFF,0xFF);
  strip.show(); // Initialize all pixels to 'off'
  teamColour(); // change back to team colour
}

void flashOn(){
  strip.begin();
  setAll(0xFF,0xFF,0xFF);
}

void flashOff(){
  strip.show(); // Initialize all pixels to 'off'
  setAll(0x00,0x00,0x00);
}

There are a number of subroutines that could possibly be called during a laser tag game, such as flashOn(), flashOff(), and (flash).

Using Multiple tabs in Arduino IDE

Arduino allows us to have multiple tabs in Arduino. The tabs operate like you have only one file, but allows you to keep different sections of your code separate. This allows you to keep your coding tidy and easy to find sections. I want to create a tab for OTA and one for the LEDStrip.

To create a tab click the down arrow on the right hand side of the Arduino IDE, and seclect New Tab

how to make a new tab – click the down arrow on the right hand side

We want to move as much of the code from each component into those tabs as possible.

  • Under the OTA tab, copy the following code. Note that I have optimised the code, so it will continue if the wireless network is not found.
void setupOTA() {
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

//changed while to if and removed pause to prevent blocking code.
   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed!");
  }

Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

  AsyncElegantOTA.begin(&server);    // Start ElegantOTA
  server.begin();
  Serial.println("HTTP server started");
}
  • Under the LEDStrip tab we will put the LEDStrip subroutines and create a function for setup:
//setup LED Strip
void setupLEDStrip(){
  // set up team colour NeoPixel
  strip.begin();
  teamColour(); // subroutine to change colour of strip.
  Serial.println("NeoPixel Strip LED is now operational");
}

//teamColour sub-routine
void teamColour(){
  strip.show(); // Initialize all pixels to 'off'
  switch (teamID){
    case 0: // red team
      setAll(10,0,0);
      break;
    case 1: // blue team
      setAll(0,0,10);
      break;
    case 2: // yellow team
      setAll(10,10,0);
      break;
    case 3: // green team
      setAll(0,10,0);
      break;
    case 4: // unspecified team
      setAll(10,10,10);
      break;
  }
}

// sets the colour of every pixel to be the same
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    strip.setPixelColor(i, strip.Color(red, green, blue));
  }
  strip.show();
}
  • The main program (1st tab) will also change as we remove code into the tabs
  • You will notice the creation of subroutines setupOTA() and setupLEDStrip(), those subroutines are found in each of their respective tabs.
// pin declarations
int ledStripPin = 2;// pin2 or D4 - LED strip

// game variables
int teamID = 4; // 0-4  0 = red, 1 = blue, 2 = yellow, 3 = green, 4 = unspecified

// NeoPixel libraries and variables
#include <Adafruit_NeoPixel.h>
#define PIN ledStripPin 
#define NUM_LEDS 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// ElegantOTA libraries and variables
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
const char* ssid = "lasertag";
const char* password = "lasertag";
AsyncWebServer server(80);

void setup() {
  // Serial communication set up to help with debugging.
  Serial.begin(74880);
  Serial.println("Serial Communication is set up");
  
  setupLEDStrip();
  setupOTA();
}

void loop() {
   AsyncElegantOTA.loop();
}
  • You will notice the 3 tabs below:

Upload and test code

You can now upload and test code. You should be able to do this OTA or via the USB. Check the previous tutorial if you need instructions for how to do this. You may like to try changing the team colour to ensure it is working correctly.

Video of coding section:

Christian Content

In the laser tag game, it is important to be able to identify who is on your team. Without the coloured lights identifying your team colours, it would be difficult to know who is on your team, and who is trying to kill you.

Even with the coloured lights identifying your team colour, there may be some people on your team who are a little clueless, who may accidentally work against you. It can be extremely frustrating in a game because they might as well be on the other team. In fact, it’s as if the other team has infiltrated your ranks and is causing damage from within.

In Laser Tag, your colour identifies what team you are in, and generally that works. But Christians don’t have a coloured light that identifies if they are a Christian or not. Even though, the Bible gives us a number of descriptions of what it means to be a Christian, and how to identify them.

1. Christians Love one another

John 13:34–35 (HCSB)
“I give you a new command: Love one another. Just as I have loved you, you must also love one another. 35 By this all people will know that you are My disciples, if you have love for one another.”

The love Christians have for each other is the same that Jesus had for them. Jesus demonstrated his love by dying on the cross for those who did not deserve it. The Christian’s love goes beyond loving those that are going to love them, but to love their enemies (Luke 7:27).

2. Christians believe Jesus

John 10:26–28 (HCSB)
But you don’t believe because you are not My sheep. 27 My sheep hear My voice, I know them, and they follow Me. 28 I give them eternal life, and they will never perish —ever! No one will snatch them out of My hand.

The title of “Christian” simply means a disciple of Jesus. They are someone who follows Jesus. It wouldn’t make sense to be calling yourself a Christian, and yet not at least aim to follow the teachings of Jesus. Christians believe in Jesus and follow him.

3. Beware of fakes

2 Corinthians 11:13–15 (HCSB)
For such people are false apostles, deceitful workers, disguising themselves as apostles of Christ. 14 And no wonder! For Satan disguises himself as an angel of light. 15 So it is no great thing if his servants also disguise themselves as servants of righteousness. Their destiny will be according to their works. 

Just imagine that in a game of Laser Tag, in which your enemy took one of your team’s guns. They would look like they are on your team, but in fact their works demonstrate clearly they belong to the enemy. It’s kind of what is going on in these verses.

Some people may appear to be Christian, they may even become amazing Christian leaders. But just because someone becomes a Christian leader doesn’t make them a Christian. And this is possible because even Satan can disguise himself as an angel of light. But fortunately, they can easily be spotted. “Their destiny will be according to their works”. They won’t love one another, and they won’t believe and follow Jesus. These two things that are mentioned above.

A simple way of explaining what a Christian is, is someone who Trusts and Turns. They Trust Jesus and they Turn and follow him. This means they now live a life loving others, and loving God.

 

Leave a Reply