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

Lab 8: 7-Segment LED Wiring

You are going to need to take the piezo speaker off of your breadboard in order to complete this lab. We are going to put the seven segment LED on the bottom of the breadboard and connect it to pins 2-9 on the arduino. You will also have to take the infrared sensor out of pin 9. Here is the wiring diagram for the seven segment LED (with all other pieces taken out to make it easier to focus on how to wire the seven segment display, which is sometimes written 7-segment display).

Lab8_bb

Once we have the 7-segment display wired to the arduino, we are going to need to program the arduino to turn on each of the segments in the 7-segment display in order to display the number that we want it to. There is one more thing that will be helpful to program the 7-segment LED, and that is which segments are controlled by which arduino pins.

Below is a schematic drawing of the 7-segment LED. Each of the triangles with a line and two arrows represents an LED. The direction of the triangle shows which way the electricity can flow.

It's coding time! To sketch 8.

Note: If you want more of a challenge, don't look at the schematic above to program the seven segment LED. Determine what pins control which segments by trial and error.

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

Nov 22

Sketch 7

This sketch goes over changing the code so that the unit sends one tag when you push the button and not when the button is held down.
Copy and paste all the lines below the /*******/ into an arduino sketch

/**********************************/
//Sketch_7
/* This program goes with Lab 7 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 will change the unit so that it only sends a tag when the button is pressed.
*/

#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() {
//start timer1 at 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);

//startup noise
int count = 0;
for (count = 1; count < 20; count++){ tone(piezo_pin, count*250); delay(25); noTone(piezo_pin); delay(10); } } void loop() { int button_state = digitalRead( button_pin ); //read button /* Now that we know what state the button is in, we want to know if the button has gone from high to low. To do this, we are going to use a function. A function is a piece of code that you write and give a name. As the code you write gets more complicated, you can write functions to break the code up into smaller pieces that perform a specific function. A function can have inputs and outputs. You put functions after void loop(){ code. (go to the bottom of the code to see the change_state_checker function We are going to write the change_state_checker function to output a 0 if the button hasn't been pressed, and a 1 if the button is pressed. */ int button_changed = change_state_checker( button_state ); if (button_changed == 1) { //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

tone(piezo_pin, 5000); //make a high pitched sound
delay(25); //wait for 0.025 seconds
noTone(piezo_pin); //stop making sound
delay(25); //wait for a little bit
}
else{ //if we don't have health
//if we don't have helth, make a different sound
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;

//turn on LED to also indicate that we were tagged
digitalWrite(led_pin, HIGH); //turn LED on

/*****
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, 500); //play first sound
delay(50);
noTone(piezo_pin);
delay(10);
tone(piezo_pin, 3500); //play second sound
delay(50);
noTone(piezo_pin);
delay(10);
tone(piezo_pin, 500); //play third sound
delay(100);
noTone(piezo_pin); //stop playing sound
delay(10);

digitalWrite(led_pin, LOW); //then turn LED off

if (health == 0){ //if we are out of health
//if we are out of healh, make the I'm out sound
int count = 0; //create a variable
for (count = 20; count > 0; count--){ //play sounds from high to low pitch
tone(piezo_pin, count*250);
delay(25);
noTone(piezo_pin);
delay(10);
}
}
}
}

/*
below is the function change_state_checker. It outputs an integer (int), which
is why there is int in front of the name of the function. All of the variables
that we use will be integers, so whenever a function outputs a number or has
a number as an input, we will have it be an int.
The name of the function is:
change_state_checker. Name functions something that describes what they do
so that you remember what they do. This one checks if the state of the button
has changed. It takes in the current_state of the button, which is also an
integer, which is why there is int in front of it.
I put a /**** in front of and behind the function description so that I can find
it easily in the code. You should always have a description of what the function
does above it so that you know what it does. It will also help you write the
function if you know what it is supposed to take as an input and output.
*/

/**********
change_state_checker
checks to see if the button has already been pressed
Outputs 1 if button changes from 0 to 1
outputs 0 otherwise
***********/
int change_state_checker(int current_state){
//in order to know whether the button was just pressed, we need to know the
//last state of the button (whether or not the button was pressed the last time
//that we checked the state of the button.
static int last_state;
/*you need to tell the arduino to remember the variable last_state. If you make
it a static int, then the arduino will remember it every time the code calls
this function.
we want to check if the current state of the button is not equal to the state
the button was in before (the last state). To check if something is not equal,
you use != instead of ==.
*/
if (current_state != last_state){ //if the state of the button has changed
/*
Now we want to check to see if the button's last state was low. This means that
the button went from unpressed to pressed
*/
if (last_state == 0){ //if the last state of the button was not pressed
//before I output something out of the function, I need to set the
//last_state of the button to the current_state. That means that the next
//time that we call this function, it will say that the last state is
//the state that the button is now
last_state = current_state; //save the change in the current state
return 1; //if the button changed from 0 to 1, output 1
}
}
/*
If the button did not go from high to low, we want to return a 0. First, though,
we need to tell the arduino to set the last state to the current state so that
the next time we call the function, the arduino will remember the state that the
button is now
*/
last_state = current_state; //reset last_state to current_state
return 0; //return 0 if I didn't get a change from 0 to 1
}

//Srpc sri tia tiv tywl

Nov 22

Lesson 7

One more change that will improve the tag unit is to change the code so that the unit only sends a tag when you push the button. In order to do that, we don't want to have the arduino send a tag if the button is currently pressed. We can tell the arduino to keep track of whether or not the button was pressed the last time we checked and compare that to whether or not the button is currently pressed. That way, when we press the button, we will only send one tag.

This is mainly a change to the code. So, let's go to the code.

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.