Jul 05

Sketch 4

Copy and paste everything below the /****/ into a blank sketch.

/*****************************/
//Sketch_4
/* This program goes with Lab 4 of the OpenTag laser tag tutorial.
It plays a basic game of free for all laser tag. A player is out
if they get tagged 5 times.
*/

#include
//TimerOne will make it easy to pule an infrared LED 38,000 times
//a second

// Variables (nicknames for pins)
int button_pin = 12;
int led_pin = 11;
int ir_led_pin = 10;
int ir_receiver_pin = 9;

//you start with 5 health. This is the first time that we will use
//a variable to keep track of and change a number.
int health = 5;

void setup() {
//first, start up the TimerOne library. We want it to pulse
//38,000 times a second, which is about once every 26 milliseconds
Timer1.initialize(26);

//tell the arduino if a pin is input/output
pinMode( button_pin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(ir_led_pin, OUTPUT);
pinMode(ir_receiver_pin, INPUT);
}

void loop() {
int button_state = digitalRead( button_pin ); //read button
if (button_state == HIGH) { //if the button is pushed
/* If we are pushing the button, then we are trying to send
a tag. However, we can only tag if we have more than 0 health.
We can use the if statement and a comparative function called
greather than, which uses the > symbol. Greather than will
be true (the code in the if statement will be run) if the
number on the left is greather than the number on the right.
We want to send a tag if health is greater than 0. In code,
that is written as:
*/
if (health > 0){ //if we have health
//so, we know that we are pushing the button and have health
//now we can send a tag.
//first, pulse the infrared LED with the TimerOne code
Timer1.pwm(ir_led_pin, 512); //send pulse
delay(10); //delay for 10 milliseconds
Timer1.disablePwm(ir_led_pin); //turn off Timer One
}
}

//now, we want to see if we are getting tagged
int ir_receiver_state = digitalRead(ir_receiver_pin);
//if the receiver sees something, it will send a LOW or 0 signal
if (ir_receiver_state == 0){
//when we are tagged, we lose 1 health.
/* In order to tell the arduino to subtract 1 health, we
need to tell it what health should now be. To do this, we
will say that the health I should have is equal to the health
that I had minus 1. That line of code is written below.
*/
health = health - 1;
//now that we see something, indicate it by turning the LED
//on for 1 second
digitalWrite(led_pin, HIGH);
delay(1000); //a delay of 1000 is 1 second
digitalWrite(led_pin, LOW); //then turn LED off
}
}

//Tia tia tia!

Jul 05

Lesson 4

Now that we have an arduino that can both send and receive signals using infrared light, we can program it to play a basic game of laser tag.

Basic Laser Tag

We will start with a simple game of elimination. Each player has a tag unit, and the objective is to tag the other players 5 times. If you get tagged 5 times, you are out. The person who is left when everyone else is out is the winner.

So, how do we program the arduino to do this? We are nearly there. We already have code that sends in infrared signal (light pulsed 38,000 times a second) that our infrared receiver can receive. We also have code that checks to see if our receiver is receiving anything. What we need to do is add health. If you are out of health, then you can not send tags. Every time you receive a tag, your health goes down by one. You start with 5 health.

That's all there is to building a simple game of laser tag. From here, we are going to start adding bells and whistles to make the game more fun to play. But after you have completed this lesson and lab, you can play laser tag. Go for it. Write down what you liked and didn't like. If you want to change something, you can do it. This is your game of laser tag, and you can build it to do whatever you want. But first, let's make the code. To the code!

Jul 04

Sketch 3

This is the code for Sketch 3. Copy and paste everything below the /*************/ into a new arduino sketch.

/************************************************/
//Sketch_3
/* This program goes with Lab 3 of the OpenTag laser tag tutorial.
It sends a pulsed IR signal (38,000 times a second) when a button
is pressed and turns on an LED if a signal is seen
*/

/*
first, let's add the TimerOne library. To add a library, you
type #include and then the library name inside <>
*/

#include
//TimerOne will make it easy to pule an infrared LED 38,000 times
//a second

// Variables (nicknames for pins)
int button_pin = 12;
int led_pin = 11;
int ir_led_pin = 10;
int ir_receiver_pin = 9;

void setup() {
//first, start up the TimerOne library. We want it to pulse
//38,000 times a second, which is about once every 26 milliseconds
Timer1.initialize(26);

//tell the arduino if a pin is input/output
pinMode( button_pin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(ir_led_pin, OUTPUT);
pinMode(ir_receiver_pin, INPUT);
}

void loop() {
int button_state = digitalRead( button_pin ); //read button
if (button_state == HIGH) {
/*so, this is the first change. What we want to do is have
the arduino turn on the infrared LED on and off at 38,000
times a second.
we already know that digitalWrite is the command to tell the
arduino to set a pin to HIGH or LOW voltage. We want to
do that 38,000 times a second. That means we need to
turn the LED off then on about every 0.026 seconds.
(1/38,000 = 0.02632)
We could use digitialWrite to turn the infrared LED on and off,
or, we could use the TimerOne code to turn the infrared LED
on and off at precisely the time we want. We just need to tell
the arduino how long to pulse the LED. For that, we use the
delay function. The delay function tells the arduino to pause
for a set number of microseconds, or 1/1000th's of a second.
For our code, we will have the infrared LED pulse for 10
milliseconds every time we push the button
*/
//first, pulse the infrared LED with the TimerOne code
Timer1.pwm(ir_led_pin, 512); //send pulse
delay(10); //delay for 10 milliseconds
Timer1.disablePwm(ir_led_pin); //turn off Timer One
}
/*we don't need to do anything if the button is low
else {
digitalWrite(led_pin, LOW);
}
*/
//now, we want to see if we are getting tagged
int ir_receiver_state = digitalRead(ir_receiver_pin);
//if the receiver sees something, it will send a LOW or 0 signal
if (ir_receiver_state == 0){
//now that we see something, indicate it by turning the LED
//on for 1 second
digitalWrite(led_pin, HIGH);
delay(1000); //a delay of 1000 is 1 second
digitalWrite(led_pin, LOW); //then turn LED off
}
}
//Mj csy ger xyvr sr er PIH, csy ger hs ercxlmrk!

Jun 23

Lab 3

We are going to add an infrared (IR) LED and an infrared receiver to the arduino. We already know how to add an LED, and we have chosen to put it on pin 10. The new part is the infrared receiver. This outputs whether or not it sees an infrared signal turning on and off 38,000 times a second. We need to give this sensor power, ground, and connect its output wire to our arduino so that our arduino can sense the signal (high or low voltage) that the infrared sensor gives as an output. Below is a circuit diagram of what we need to add.

Figure 1: Picture of connections. (Click on the picture to make it bigger).

Lab3_bb
Note: I couldn't find an IR sensor in the software I use to draw circuits, so my IR sensor doesn't look exaclty like an IR sensor.

So, now that we have a red LED, infrared LED, infrared sensor and button, how do we want to program them? So, for laser tag, we want to push a button and send a signal out of our infrared LED (to send a tag). When we receive a tag on our infrared receiver, we want to indicate that we have been tagged with our red LED. So, we need to modify our code to follow those instructions.

We are going to use other people's code to make building laser tag easier. We call other people's code libraries. It is sort of like checking out a book at the library, only you are checking out other people's code and using it in your own code.

The first library that we are going to use will turn on and off our infrared LED 38,000 times a second. That library is called TimerOne. You can download the library here (note, download the file TimerOne-r11.zip for the latest version) and read about how it works with the TimerOne Tutorial (the tutorial on how it works is very advanced. I can go over it later if you would like. You do not need to know the details on how it works to use it properly).

Once you have the file downloaded, you need to tell the arduino programming software where the library is so that you can use it in your code. The arduino software has a folder where it stores all of the libraries it knows about. To move the library to a place where the arduino software can find it, you can follow the tutorials from either the arduino library installation or the sparkfun library tutorial. Once you have the TimerOne library installed, it's time to modify the code to make a rudimentary laser tag unit. To the code!

Jun 22

Lesson 3

We are going to use infrared light (the same kind TV remote controls use) to communicate between two arduinos so that they can play laser tag. This lesson goes over how to do that.

Now that you can get your arduino to respond to a button press, the next thing to do is make it send a signal using infrared light when you push a button. We need an emitter (an infrared LED to create the light) and a receiver to sense the light. We already know how to light up an LED, and if we use an LED that emits Infrared light, then the emitter is done.

So, if we wanted in infrared receiver, how would we build one? We could make one that turns on when it sees infrared light(remember, the arduino only communicates with voltage. When the receiver sends a signal saying that it sees something, it will do that by changing the voltage on one of its pins). However, there are a lot of sources of infrared light naturally. The sun, your body, fire - anything that gives off heat also emits infrared light. So, how can we make sure that the infrared light that our detector sees is coming from our infrared LED?

Well, the people who made the receiver made it only respond to infrared light that blinks. Really fast. For the receiver we will use, whenever the receiver gets hit with 38,000 pulses of light per second, then the receiver will say that it is seeing our infrared LED (and one of its pins will change from high to low voltage). Since our arduino can make the LED blink really fast (to create an output for the receiver), and our arduino can read high or low voltage from the receiver, our arduino can use the infrared LED and receiver to communicate. So voila! A way to take an infrared signal and make it control the arduino.

So, now that we have an idea of what we want to do, how do we do it? We're going to need to write a piece of code that pulses an LED on and off 38,000 times a second. We will also have to have the arduino check the infrared receiver to see if the infrared receiver is receiving the pulsed infrared light. This is the basics of communicating between arduinos. So let's build it. To the Lab!

Bonus Knowledge

The infrared reciever we will use is active low. That means when it activates [sees a infrared signal pulsing 38,000 times a second], it makes its output pin low. Why does it make the pin low and not high when it senses a signal? That has to do with power consumption, industry standards, etc. We won't go into that. If you really want to know, ask me. I'll be glad to try to answer.

There is a lovely tutorial at adafruit that goes into how to use IR (infrared) sensors, which you can find here if you want more information. For now, it's on to the circuit diagram and the code. (well commented, of course!).

May 04

Laser Tag Update 5/3/15

My deliverables from last month were to:
1. Build a receiver unit. (IR receivers with indicator LED)
2. Adjust code for first game played

1. I arranged IR receivers in a circle,and they all read. I soldered them onto a spare piece of board and built two. After the fact, I decided to add an additional LED onto the receivers so that you would have LED indication on the lens tube when you are tagged or when you send a tag.
2. I changed how my LED's (for indication, not for tagging) turn on and off. I wrote a function that handles all of the indication LED's. With some modification, I could have it run RGB LED's and change their colors without using the built in PWM. The color may change a little bit when you send or receive a tag, but other than that, it should be able to control them pretty easily (during the time that you send or receive a tag, you would not be turning the LED on or off, so the color may be a little different.)

Note: I could change my code that sends and receives tags to send/receive a pulse and then go back into the main loop until I need to send/receive a pulse again. I'm not sure that I'll need to do this, but it is a possibility if I want to run through my main loop faster (and not have to wait for a tag send/receive to complete before going through the main loop).

At this point, I have two tag units, they both are running the current code, and I am ready to actually test playing laser tag. During that test, I should figure out what I should add to make the game play better. I have some other items that I would like to add, and in no particular order, here are some of them:

To add Later:
1. Telegraphing
2. Health bars
3. Shields
4. Bases

Based on trying to make a game type similar to Star Wars Battlefront, I should make spawn bases next. I can work on telegraphing and shields after I have places to respawn from. I can add allowing players to capture spawn points later. (I could add a certain number of respawns for a game type). So, here are my next steps:

Do Next:
1. Test the game with the current tag units: get feedback on what I should add to the devices
2. Begin work on spawn bases (unless telegraphing, health, shields, etc. comes back as something people want more)

Apr 05

Laser Tag Update 4/5/15

My deliverables from two weeks ago were:
1. Rewrite the bill of materials for the tag unit
2. Purchase three additional units
3. (Optional): test tag distances with different protocol timing.

I sorta updated my bill of materials. I have purchased most things from mouser.com, so I have a record of everything that I've purchased and will be able to purchase more if I need to.
I have now purchased materials to build four additional units. Why four? That will give me enough materials to have 5 units. So I could have some asymmetric game play (4v1, 3v2, etc.) or just have up to five people test the things. Or one will break. Who knows.

Do Next:
It will take a week or so for the parts to ship to me, and in the meantime I should prepare for the first game. The only next step I can think of is:
1. Build a receiver unit. It should be able to receive tags from a wide angle (360 degrees is nice, but not necessary). It should have an indicator LED on it.
2. Adjust the code for the first game played.
I am trying to build the minimum viable product. That means the minimum that I can do to make this work is what I should do.

For the first part, I basically need an array of IR receivers that are looking in different directions for a tag. I'm planning on using around 8 right now, because each of the ones that I'm using has a 45 degree field of vision. With 8, I get 360 degrees. I will have to mount that on the tag unit so that you can be tagged from any direction.
I should put an indicator LED on this part. This should go on top of the lens tube. In fact, I could build this in two parts so that you could still see down the center of the tube in order to aim.

For the second part, I need to make the code react to getting tagged in some way so that I can play a game with it. What is the most basic game that I want to play? Standard death match. Each player can get hit a certain number of times. They are out after hit a certain number of times. I need to add some code to keep track of the number of times someone has been hit, and if that counter reaches 0, they are out (I could have the indicator LED stay on or something.
If I want to make the game last for a certain amount of time, I could use a timer on my phone.

For future work, I can get stuff laser cut here:
http://www.cherrylaser.com/
http://www.lasersoverlosangeles.com/

Mar 23

Big List of Do Later's

So, I am following one method of project management for this project. There are two types of tasks:
1. Do now
2. Do later

There are a lot of things that I want to do, but I can't do them all. So, of all the things I want to build, each week or two, I pick a few manageable tasks that I can accomplish in the next week or to and try to accomplish them. Those are do now. Everything else is do later. I don't have to worry about that. I'll do it later. This is a list of all the things I want to do, in not really any order. I may try to order it later.

1. Shields (Inspiration, Halo)
Adding this mechanic will change gameplay. It will allow people to get tagged and then hide (unless you fire fast enough). This will change gameplay by making it harder to tag people and get them out if they are behind cover. That may encourage maneuvering.

2. Respawn base (Inspiration: Zombie mods, multiplayer games, capture the flag, etc.)
This will help with tracking tags, number of respawns, capturing areas, etc.
You can add temporary invulnerability to discourage spawn camping.

3. Classes (inspiration: Team fortress 2, star wars: battlefront 2, Zombie mod)
1. Standard
2. Sniper
3. Shotty
4. Medic
5. Spy/Ninja
6. Heavy
TF2 Game type: You can spawn as any unit from the beginning. You can only change when respawning.
Star Wars Battlefront Game type:
You all spawn as one of the standard classes in the beginning
You can respawn as a hihger leveled player based on tags (would require respawn points to talk for counting tags)
Playing the same character and getting tags unlocks perks (ex: burst fire for standard gun, like in star wars)
Zombie mod:
Same respawning principles as Star Wars Game type, but with asymetric teams (Humans vs Zombies)
No human respawns
Only zombie base

4. Runes (Inspiration: Dota2)
Adding rune spawns will encourage people to meet at specific areas during designated times and encourage team fights.
1. Invisibility - no lights, purple
2. Invulnerability - white
3. Extended magazine (requires magazines)
4. Rapid fire
5. Regeneration?
6. Double damage
7. Experience (this could level you up. Giving better stats, upgrades, spawn types, etc.)
8. Overshields

5. Active Reloading (Inspiration: Gears of War)
This would make reloading a little more fun to master. It could also help control fire rates for different classes of tag units
You could use an LED bar for this, along with a shift register.

6. Sounds (Castle Crashers, 8 bit games)

7. Abilities (Dota 2, Borderlands, Left 4 Dead)
Corrosive attacks
Electric attacks
Fire attacks
Slag?
Stuns?
Disarms? (Zombie mod: smoker has a disarm?)
MANA?
Lifesteal?
Zombie mod - corrosive attack on death? (Boomer in L4D)

8. Items (Halo, Gears of War (Horde Mode items), Dota 2)
Regen?
Auto Turret
Linken's?
BKB?
Mech suit
Manned Turrets
Grenade

9. Zombie Mode (L4D)
There are a lot of technical issues I will have to overcome before I get to this, but I can add elements of other things to it. Aim for the head.
Smoker (with disarm?)
Spawner (which grants level up to the person who tags it)
Boomer (on death, 'tags' with caustic goo)
Tank
Stealth Zombie

Mar 23

Laser Tag Update 3/22/15

I seem to be updating every two weeks. But hey, I'm making progress.

My deliverables from two weeks ago were:

1. Distance tests
2. Adjust code to use multiple receivers and test them.

I completed the distance test for the first version of the tag unit.

Specifications:

Voltage for LED: 5 V
Resistance for LED: Seven (x7) 33 Ohm resistors in parallel, resulting in 4.7 Ohm total resistance.
Current for LED: 1.06 (max is 1A)
Lens Diameter: 3.49"
Lens Focal Length: 9.5" +/- 0.125"
Max Transmission distance: 339 yards.

So, 339 yards is far. I tested my unit, and I could make a light blink one city block away. Which is great. And also very over powered. I'm not sure people will like using a 3.5 inch diameter lens, but heck, it works really well. I had to send the tag multiple times for the receiver to see anything at 339 yards. I could hit the receiver effectively (could aim and hit in 1 or 2 shots consistently) at about 235 yards. These measurements are estimates based on my gate (I measured how far I walk normally in 10 steps, then counted steps back to the receiver). However, despite the inherent inaccuracy of the measurement, I am very pleased with this transmission distance. It is a big improvement over the 90 feet that I got with the 1 inch lens.

The other change that I made was to the code. I got github to work on all of my operating systems (Windows, Linux and Mac), so now I can work on the most recent version of the code no matter what OS I decide to use at the time. I am tracking the changes I make on github, which is a great way to update code and track changes. I can now have the tag unit read in a tag from an IR receiver on any of my digital input pins. I also changed how my timing function works, since I wasn't accurately reading in the times. Also, I'm not sure if I decided on using 750 microseconds as the protocol duration or 600 microseconds. Either will work, but I should pick one. Or experiment to find out which is better.

So, deliverables for next week or two:

1. Rewrite the bill of materials for the tag unit
2. Purchase three additional units

If I have additional time

3. Test tag distances with a 600 and 750 microsecond protocol duration. See if there is a difference. (or, test 500 microseconds versus 1 millisecond to see a more drastic version of how this breakdown effects tag distance).

Side notes:

I came across a video by Extra Credits on tangential learning. I think there are already great resources to teach people the basics of circuitry, electronics, etc. I don't have to make those. I could use laser tag as a method of giving people exposure to electronics and letting them dive into the details if they want to. I can't force someone to like this or to learn it. For now, I think I should put the tools out there and let people who want to find more dig into those tools and resources to explore more themselves.

Also, I want to turn this into more of a video game. There are things about video games that are great, and I feel that they would add to the enjoyment of this. Experience, shields, unlockables, overshields, mech suits, manned turrets, auto-turrets, grenades, sounds, music, mines, abilities, character selection, etc. I could pick a game and start porting content.I should probably keep a repository of all these ideas somewhere I can find them.

Mar 09

Laser tag update 3/8/15

I missed last week's update, but I still worked on the laser tag project. I just didn't post. Well, here's to make up for lost time:

My deliverables from last week were:

1. Change IR LED current to 1 Ampere. Done.
2. Get distance tests for 1 A with 4.5" lens and a 1" lens. Not done.

So, I did upgrade the board a bit. I was using a solderless breadboard for my prototyping, but it was difficult to hold the darn thing together while aiming at my receiver board. Since I need to make a prototype anyway, I built a version on perfboard that had a breadboard layout on it. That way, my prototype would be sturdy enough that I could walk around with it and test it. I changed the current draw up to about 1 Amp as well. I am not using the 4.5" lens, and instead am using a 3.49" lens that is a standalone lens (the 4.5" lens is encased).

Specifications:

Voltage for LED: 5 V
Resistance for LED: Seven (x7) 33 Ohm resistors in parallel, resulting in 4.7 Ohm total resistance.
Current for LED: 1.06 (max is 1A)
Lens Diameter: 3.49"
Lens Focal Length: 9.5" +/- 0.125"
Max Transmission distance: ?? (not tested)

The purpose of this prototype was to build a unit that could be carried around. I used cardboard as my building material, as it is easy to cut, I have a bunch of it after purchasing stuff, and I can build things with it really quickly. I don't care about making it look pretty at the moment. I just need to see if it will work.

This is the first laser tag prototype

This is the first laser tag prototype

I will do distance tests eventually. However, currently, I have my receiver (which is basically another tag unit) turn on an LED when it is tagged. However, I can't see the little LED very well when I am standing 90 feet away, so I'll need to hook up my laptop and walk away, tag the receiver, then walk back. I'll do it eventually. Not enough time these past two weeks.

Another fun fact about using a larger lens. I empirically found the focal length by focusing the light of the sun (which is as close to a collimated source as I can get. If you have a better solution than focusing your light on something 92 million miles away, please let me know). When I did that, I noticed that the spot is larger than the spot of light I got when using a smaller lens. This is beneficial because the LED is not a point source, but is 5 mm in diameter. Using a larger lens will allow the focal point to have a larger diameter, so I will be able to collimate more light.

I will also mention to check for floating inputs. I didn't solder on the receiver initially, as I was pressed for time. That meant that the input was left floating (I didn't turn on internal pull-up resistors, since originally, there was a receiver there). My tag unit would stop tagging for some reason. I thought it was a power supply issue, code issue, and eventually tracked down that I had a floating input. Oops. I soldered the receiver in, and everything worked fine. If I'd taken the time to solder that last bit on, I wouldn't have been frustrated with the unit upon building it. Let that be a lesson.

Eventually, this needs to be turned into a tag unit that I can run around with and send/receive tags. Here's a short list of work that I will need to do eventually:

1. Build an enclosure - should be done after prototype done
2. Look at multiple IR receivers - (simplest way may be to add multiple receivers to the unit. Current ones don't have 360 degree field of view)
3. Test using 1 resistor instead of 7.
4. Determine what connection you want between your LED and tag unit. (maybe for the final unit, you have a red LED next to the IR)
5. Build multiple devices and test them in the field!

Future work (after first game):

1. Add LCD
2. Add RGB LED?
3. Add 58 kHz receiver for other types of tags
4. Make different lens combinations with different resistors on them with different tag rates

So, based on those things, I should probably test adding multiple IR receivers to my project and make it work with multiple receivers as my next step.

Next Steps:

1. Test multiple receivers. I have a few extra, so I shouldn't have to buy any more to do these tests.