Arduino sketch for a simplified "DuinoTag"

Home Forums Laser Tag Project Arduino sketch for a simplified "DuinoTag"

Tagged: , , ,

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #270
    Brenton Rowland
    Keymaster

    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); }

    #271
    Brenton Rowland
    Keymaster

    Copy and Paste the code into a new Arduino sketch.

    It is based on the DuinoTag Instructable here.

    It uses pins:
    2 for IR receive sensor input,
    3 for IR LED sender output,
    4 for the trigger button input,
    12 for a buzzer output – to make simple beep noises
    13 for an LED to show activity

    Should be enough to get something working.
    Then modify the code to use a sound module/amplifier/1000W subwoofer, add game modes, scoring etc..

    #401
    Brenton Rowland
    Keymaster

    This was good demo code to test the basics of Infra Red tagging. It’s now 4 months on in Sept 2017 and Sccience Club LaserTag has new code in the File Downloads. Have a look in Projects to see our progress.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.