Dec 16

Sketch 8: Controlling a Seven Segment Display

You will be writing a program to control a seven segment display. You can refer to Lab 8 so that you know what pins the segments on the display are connected to. That way, you will turn on the correct LED's in the seven segment display when you try to display a number.

Again, copy below the /*************/ into the arduino programming environment.

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

//Sketch_8
/*
This program will tell the arduino how to display numbers on a seven
segment display. It goes with the laser tag course.
This code only comes with comments. It is intended to help the
reader understand what they need to tell the arduino to do
(Aka, what code to write)
Remember to add comments!
*/

// Variables (nicknames for pins)
/*
We will need a variable for each of the pins that we connect to the
seven segment display so that we can turn that pin on or off
depending on what number we want to display.
All of our variables will be integers, meaning they will start with
int (variable name) = (pin number);
with the stuff in () replaced with the variable name and pin number.
*/

void setup() {
/*
Now we need to tell the arduino if the pins that we are using are
inputs or outputs. We will be sending a signal out to light up
each of the pins of the seven segment display. You use the
pinMode() function to set whether a pin is an input or output.
pinMode() accepts two inputs, the pin number and whether it is an
INPUT or OUTPUT.
for example: pinMode(pin_3, OUTPUT);
the command above would set pin 3 to be an output (assuming pin_3 is
a variable that you set to 3)
*/
}

//now for the main loop da loop
void loop() {
/*
we've told our arduino what pins we are using and whether those
pins are inputs or outputs. Now we need to tell it what to display.
Just for fun, we want to have the display switch between two numbers,
0 and 1. You will should program the arduino to turn off all of the
segments (maybe by writing a function to do that, since you will need
to do that twice...), and then turn on the segments that correstpond
to either 0 or 1. You can have it waid for 1 second between the digits.
Be sure to add comments to your code so that you know what it is doing.
*/

}

/*
Now we want to write a function that turns off all of the digits. Use
this function in the loop above so that you don't have to copy and
paste it every time you want to turn the segments off before
turning them on to display a new digit.
*/

//Tlia. Xlex aew e psx sj gshmrk. Asyphr'x mx fi kviex mj csy hmhr'x lezi xs hs epp xlex asvo xs hmwtpec e ryqfiv?

 

Dec 15

Lesson 8: 7-segment LED's

One of the current problems with the laser tag unit is that you don't know how much health you have left. Wouldn't it be great if you could have the arduino tell you how much health you had left? There are a few ways to do this. The one we are going to go over is a seven segment LED. A seven segment LED is a group of LED's that are arranged to look like a figure 8. By lighting up different LED's, you can make numbers 0-9 (and a few other symbols, if you want).

There are seven LED's that light up each straight part of the of the figure 8 in the display (that's why it's called a 7-segment LED. There are 7 segments that light up.). If we had to wire every LED like the one that we put on our board, we would have to put 14 wires (one for the + and one for the - side of every LED) to wire our 7-segment LED. That's a lot of wires. Instead of doing that, the people that manufactured the 7-segment LED that we will be using wired all of the grounds (negative) for the LED's together to one pin. That way, you only need to connect that 1 wire to ground to have every single segment on the LED connect to ground. Nice of them, huh?

A seven segment LED looks like this:

7-seg LED picture

What we need to do is connect each one of the pins that controls the lights in the seven segment display to our arduino. After that, we can program the arduino to turn on whichever segments we want in order to display a certain number. So, let's get wiring (on to Lab 8)!

Jul 11

Lab 6

We want to add a transistor to our circuit in order to make the infrared LED shine brighter. That way, we will be able to tag people from further away. As we said in Lesson 6, we need to use a transistor, or electrical switch, in order to have more current flow through the infrared LED. The more current that goes through the LED, the brighter it will be.

We're going to need to adjust our circuit in order to put the transistor in the circuit. What we want to happen is to have the electricity flow from positive voltage, through a resistor, through our infrared LED, and then through our transistor to ground. Our transistor will act like a switch or button, that our arduino will "press" or turn on by applying a high voltage to it. Based on that description, you should have an idea about how to wire the transistor (remember, we are building a pipe for the electrons to flow through).

But what resistance to use? Well, we know that we want to put more current through the LED. But how much is too much? The infrared LED's data sheet says that it can take up to 1 amp of current if we pulse it on and off (which we are doing 38,000 times a second, remember?). However, a 9 Volt battery (which we may be using to power the project) can't really output 1 Amp of current. That's too much for it. So we will stick to a lower current for this project. If we were using bigger batteries, we could use a different resistor and have up to 1 Amp go through the infrared LED. So, with our voltage equal to 5 volts, and current equal to 0.25 Amps, what is the resistance?

The answer is 20 Ohms. Be careful to only use resistors that have this low of a resistance with transistors. Otherwise you may burn up your arduino.

We're going to wire the transistor in place like the picture below. After that, try playing laser tag again. How much farther can your signal travel?

Lab6_bb

Jul 07

Lesson 6

One of the next things that comes up when trying to make the laser tag unit better is increasing the distance that you can send tags. What we want to do is to send the light coming out of our infrared LED further. There are two methods we can use to shine the light further. We can make our LED shine brighter, or we can use a lens to focus the light.

Adding a Transistor

We want to make the infrared LED shine brighter. In order to do that, we need to have more current (remember that from Lesson 1?). However, our arduino pin can only output 0.04 Amperes of current. We want to output 0.25 Amperes of current. That way, our light will be over 10 times brighter. But if we have that much current go through an arduino pin, that pin will be toast. Fortunately, there is something we can use to fix this problem. It is called a transistor. A transistor is an electrical switch. It turns on when you apply a voltage to it, and connects two pins. You can purchase transistors that can have much higher currents flow through them than an arduino pin.

We will add a transistor to our circuit in Lab 6 to make the infrared LED send a signal farther. To do that, we will go to Lab 6.

Jul 06

Sketch 5

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

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

//Sketch_5
/* This program goes with Lab 5 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.
We add a piezo speaker to this code to make sounds when you send a
tag, receive a tag, can't tag, and are out.
*/

#include <TimerOne.h>
//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;
int piezo_pin = 2;

//you start with 5 health.
int health = 5;

void setup() {
//first, start up TimerOne. 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);
pinMode(piezo_pin, OUTPUT);
}

void loop() {
int button_state = digitalRead( button_pin );   //read button
if (button_state == HIGH) {           //if the button is pushed
if (health > 0){                    //if we have health
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 make a sound to indicate that we are
sending a tag. To tell the arduino to play a sound on the
piezo we attached to the piezo_pin, we use the tone function.
This function is build into arduino. The tone function
needs to know what pin the piezo is connected to and what
pitch the sound should be. The pitches can as high as
5,000 vibrations per second and as low as 31. You can
experiment with different values and choose the pitch
that sounds good to you. We are going to start with having
the arduino make a high pitched sound when you send a tag
*/
tone(piezo_pin, 5000);           //make a high pitched sound
delay(25);                       //wait for 0.025 seconds
noTone(piezo_pin);               //stop making sound
}
else{                              //if we don't have health
/********
So, if we don't have enough health, make a different sound
We can make a lower pitched sound to make it sound
different then the sending tag code. In fact, we can have
two different notes play to make it sound cooler
*/
tone(piezo_pin, 220);           //play first sound
delay(25);
noTone(piezo_pin);
delay(10);                      //add a delay between notes
tone(piezo_pin, 110);           //play second sound
delay(75);                      //play second sound longer
noTone(piezo_pin);
}
}

//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.
health = health - 1;
/*****
We also want to make a sound to indicate that we have
received a tag. Let's make it make three sounds this time.
*/
tone(piezo_pin, 1175);          //play first sound
delay(25);
noTone(piezo_pin);
delay(10);
tone(piezo_pin, 294);           //play second sound
delay(25);
noTone(piezo_pin);
delay(10);
tone(piezo_pin, 4699);          //play third sound
delay(25);
noTone(piezo_pin);              //stop playing sound

//turn on LED to also indicate that we were tagged
digitalWrite(led_pin, HIGH);    //turn LED on
delay(1000);                    //delay for 1 second
digitalWrite(led_pin, LOW);     //then turn LED off

/******
If we don't have any health left, we want to make another
sound to indicate that we don't have any health left
I want it to play 20 notes from across the pitches the
arduino can play. However, I don't
want to copy and paste the same code 20 times. Can't we write
some code that does the same thing over again a few times?
I mean, we have a mail loop, can't we have a smaller one?
Yes!
It's called a for loop. A for loop basically tells the arduino
do do something a certain number of times. You are telling the
arduino to count between two numbers, and every time it counts
it does a specific thing.
*/
if (health == 0){              //if we are out of health
/*
first, we need to tell the arduino what number to start
counting from. We are going to have the arduino count down
from 20. So to start, we create a variable for the arduino
to keep track of what number it counted to
Now that we know where the arduino should count to, we need
to tell it when to stop. We want to stop counting when the
count is 0. So, we will say to keep counting as long as the
count is greater than 0. This is the second part of the for
loop.
The final part is to tell the arduino if we are counting
up or counting down. To count up, we put two + simbols.
To count down, we put two - symbols.
when you put it all together, the command looks like this:
for(variable we count with; when to stop counting; count up or
count down)
So, here's the for loop, below
*/
for (count = 20; count > 0; count--){
//start with the highest note. Since I'm using 20 notes,
//I want to have each note go lower in pitch by
//5000/20 = 250 (since my highest note is 5000)
//I want to multiply my count by 250. To do that, I use
//the * symbol, which stands for multiplication in the
//arduino programming language.
tone(piezo_pin, count*250);
delay(25);
noTone(piezo_pin);
delay(10);
}
}
}
}
//Xyri mr ribx xmqi. Wii alex M hmh xlivi? Xyri.

 

Jul 06

Lab 5

This lab goes over how to add a piezo speaker to your arduino.

One important thing to note about piezo speakers is that they only work within a certain voltage range. If you go outside of the range, you risk breaking them or having them not work. One good thing about piezo speakers is that they only allow a certain amount of current to go through them, so you don't need to add a resistor. Nifty, huh?

So, how do we connect the piezo to the arduino? Depending on the type of piezo, one of the pins will have a + on it or will have a red wire sticking out of it. The side with the + or the red wire goes to arduino pin 2, and the other pin is connected to ground. Why pin 2? Why not pin 2? You can use any of the arduino's output pins. You just need to let the arduino know which one you use.

Lab5_bb

After you have the piezo hooked up to the arduino, it's time to program the arduino so that we can tell it what we want it to do with the piezo speaker. To the sketch!

Jul 06

Lesson 5

At this point, we have a pretty basic game of laser tag. From here on, we will be adding onto the code to make the tag unit do more things.

Sound

One way that we can add onto the laser tag unit is to add sound. To do that, we will add a piezo electric speaker. A piezo is a crystal that makes sound when you apply voltage to it. However, in order to make a sound, you need to send pulses of electricity to the piezo speaker (similar to how we send pulses to the infrared LED). Fortunately, there is a library for sending sounds to an arduino, called the tone library. Tone is already included in the arduino code, so we don't need to download it and tell our computer where the code is.

There are a few elements to sound. The one that we will focus on is pitch. Pitch is how high or low a sound is. You control the pitch of a sound by how fast you vibrate the piezo speaker. The faster you send electrical pulses to it, the faster it will vibrate and the pitch will sound higher. We are going to change what pitch of a sound the arduino makes when you send a tag, receive a tag, can not tag and are out.

And now, on to Lab 5, where we will learn how to hook up a piezo to the arduino.

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!).

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.