Duino Tag

by Daren Wamsley and Jon Sheppard

For our project we made infrared laser tag. We used an infrared LED for the laser and shot it using a push button. We used an infrared sensor to sense the hits. We cased our two guns in remote controls and used PVC tubes with lenses to increase the range.

The code is very simple. It is designed so that when you push the push button it will light up the infrared LED. There is an infrared sensor that registers hits and plays a sound. If you fire too many shots, you will self-destruct. Every action has a corresponding sound that will play when you shoot, get hit, self-destruct, etc.

The 2 laser guns

The IR sensor

The barrel and piezo taped to the bottom of the gun

The lens that magnifies the IR laser

Arduino Code:

int sensorPin = 2;

int senderPin = 3;

int triggerPin = 4;

int speakerPin = 12;

int blinkPin = 13;

int startBit = 2000;

int endBit = 3000;

int one = 1000;

int zero = 400;

int trigger;

boolean fired =false;

int ret[2];

int waitTime = 300;

int playerLine = 14;

int myCode = 1;

int myLevel = 1;

int maxShots = 6;

int maxHits = 6;

int myShots = 0;

int myHits = 0;

int maxLevel = 9;

int minLevel = 0;

int refPromote = 0;

int refDemote = 1;

int refReset = 2;

int refRevive = 3;

int replySucc = 14;

int replyFail = 15;

void setup() {

pinMode (blinkPin, OUTPUT);

pinMode (speakerPin, OUTPUT);

pinMode (senderPin, OUTPUT);

pinMode (triggerPin, INPUT);

pinMode (sensorPin, INPUT);

randomSeed (analogRead(0));

for (int i = 1;i < 4;i++) {

digitalWrite(blinkPin, HIGH);

tone2(900*i, 200);

digitalWrite(blinkPin, LOW);

delay(200);

}

Serial.begin(9600);

Serial.println("Ready: ");

}

void loop() {

senseFire();

senseIR();

if (ret[0] != -1) {

tone2(1000, 50);

Serial.print("Who: ");

Serial.print(ret[0]);

Serial.print(" What: ");

Serial.println(ret[1]);

if (ret[0] >= playerLine) {

if (ret[1] == refPromote) { //This promotes you when you hit someone

if (myLevel < maxLevel) {

Serial.println("PROMOTED!");

myLevel++;

tone2(900, 50);

tone2(1800, 50);

tone2(2700, 50);

}

}else if (ret[1] == refDemote) { //This demotes you if you get hit

if (myLevel > minLevel) {

Serial.println("DEMOTED!");

myLevel--;

}

tone2(2700, 50);

tone2(1800, 50);

tone2(900, 50);

}else if (ret[1] == refReset) { //This resets your ammo if you fire too many shots

Serial.println("AMMO RESET!");

myShots = maxShots;

tone2(900, 50);

tone2(450, 50);

tone2(900, 50);

tone2(450, 50);

tone2(900, 50);

tone2(450, 50);

}else if (ret[1] == refRevive) { //This revives you if you are hit

Serial.println("REVIVED!");

myShots = 0;

myHits = 0;

myLevel = 1;

tone2(900, 50);

tone2(1800, 50);

tone2(900, 50);

tone2(1800, 50);

tone2(900, 50);

tone2(800, 50);

}

}else {

if (ret[1] == replySucc) {

tone2(9000, 50);

tone2(450, 50);

tone2(9000, 50);

Serial.println("SUCCESS!");

}else if (ret[1] == replyFail) {

tone2(450, 50);

tone2(9000, 50);

tone2(450, 50);

Serial.println("FAILED!");

}

if (ret[1] <= maxLevel && ret[1] >= myLevel && myHits <= maxHits) { //This registers that you hit someone

Serial.println("HIT!");

myHits--;

tone2(9000, 50);

tone2(900, 50);

tone2(9000, 50);

tone2(900, 50);

}

}

}

}

void senseIR() {

int who[4];

int what[4];

int endx;

if (pulseIn(sensorPin, LOW, 50) < startBit) {

digitalWrite(blinkPin, LOW);

ret[0] = -1;

return;

}

digitalWrite(blinkPin, HIGH);

who[0] =pulseIn(sensorPin, LOW);

who[1] =pulseIn(sensorPin, LOW);

who[2] =pulseIn(sensorPin, LOW);

who[3] =pulseIn(sensorPin, LOW);

what[0] =pulseIn(sensorPin, LOW);

what[1] =pulseIn(sensorPin, LOW);

what[2] =pulseIn(sensorPin, LOW);

what[3] =pulseIn(sensorPin, LOW);

endx =pulseIn(sensorPin, LOW);

if (endx <= endBit) {

Serial.print(endx);

Serial.println(" : bad end bit");

ret[0] = -1;

return;

}

Serial.println("---who---");

for(int i=0;i<=3;i++) {

Serial.println(who[i]);

if(who[i] > one) {

who [i] = 1;

}else if (who[i] > zero) {

who [i] = 0;

}else {

Serial.println("unknown player");

ret[0] = -1;

return;

}

}

ret[0]=convert(who);

Serial.println(ret[0]);

Serial.println("---what---");

for(int i=0;i<=3;i++) {

Serial.println(what[i]);

if(what[i] > one) {

what[i] = 1;

}else if (what[i] > zero) {

what[i] = 0;

}else {

Serial.println("unknown action");

ret[0] = -1;

return;

}

}

ret[1]=convert(what);

Serial.println(ret[1]);

return;

}

void tone2(int playTone, int duration) { //This enables the arduino to play a tone with each action

for (long i = 0; i < duration * 1000L; i += playTone * 2) {

digitalWrite(speakerPin, HIGH);

delayMicroseconds(playTone);

digitalWrite(speakerPin, LOW);

delayMicroseconds(playTone);

}

}

int convert(int bits[]) {

int result = 0;

int seed = 1;

for(int i=3;i>=0;i--) {

if(bits[i] == 1) {

result += seed;

}

seed = seed * 2;

}

return result;

}

void senseFire() { //This senses when you fire the arduino

trigger = digitalRead(triggerPin);

if (trigger == LOW && fired == false) {

Serial.println("Button Pressed");

fired = true;

myShots++;

if (myHits <= maxHits && myShots > maxShots && random(1, 20) <= myShots) { //If you fire too many shots, the arduino will "self-destruct", not allowing you to fire any more shots until you reset the arduino

Serial.println("SELF DESTRUCT");

selfDestruct();

} else if (myHits <= maxHits) {

Serial.print("Firing Shot : ");

Serial.println(myShots);

fireShot(myCode, myLevel);

}

} else if (trigger == HIGH) {

if (fired == true) {

Serial.println("Button Released");

}

fired =false;

}

}

void fireShot(int player, int level) { //This is the program that enables you to fire the shot

int encoded[8];

digitalWrite(blinkPin, HIGH);

for (int i=0; i<4; i++) {

encoded[i] = player>>i & B1;

}

for (int i=4; i<8; i++) {

encoded[i] = level>>i & B1;

}

oscillationWrite(senderPin, startBit);

digitalWrite(senderPin, HIGH);

delayMicroseconds(waitTime);

for (int i=7; i>=0; i--) {

if (encoded[i] == 0) {

oscillationWrite(senderPin, zero);

} else {

oscillationWrite(senderPin, one);

}

digitalWrite(senderPin, HIGH);

delayMicroseconds(waitTime);

}

oscillationWrite(senderPin, endBit);

tone2(100, 5);

digitalWrite(blinkPin, LOW);

}

void oscillationWrite(int pin, int time) { //This sends the string of data

for(int i = 0; i <= time/26; i++) {

digitalWrite(pin, HIGH);

delayMicroseconds(13);

digitalWrite(pin, LOW);

delayMicroseconds(13);

}

}

void selfDestruct() { //This makes the arduino self-destruct and force you to reset it

myHits = maxHits+1;

tone2(1000, 250);

tone2(750, 250);

tone2(500, 250);

tone2(250,250);

}

Problems: We had some typos in the code but we carefully went through it and fixed all the problems. Also, there were some problems with the wiring. We fixed them carefully and found all the problems.

If we had more time, we would have made a case for it and we would have found a way to increase the range even more.