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.

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!